]>
Commit | Line | Data |
---|---|---|
af5767ff | 1 | import { SortMeta } from 'primeng/api' |
a282e4d8 C |
2 | import { from } from 'rxjs' |
3 | import { catchError, concatMap, map, toArray } from 'rxjs/operators' | |
67ed6552 C |
4 | import { HttpClient, HttpParams } from '@angular/common/http' |
5 | import { Injectable } from '@angular/core' | |
6 | import { RestExtractor, RestPagination, RestService } from '@app/core' | |
e3d6c643 | 7 | import { arrayify } from '@shared/core-utils' |
80badf49 | 8 | import { AccountBlock as AccountBlockServer, BlockStatus, ResultList, ServerBlock } from '@shared/models' |
67ed6552 C |
9 | import { environment } from '../../../environments/environment' |
10 | import { Account } from '../shared-main' | |
11 | import { AccountBlock } from './account-block.model' | |
af5767ff | 12 | |
22839330 RK |
13 | export enum BlocklistComponentType { Account, Instance } |
14 | ||
af5767ff C |
15 | @Injectable() |
16 | export class BlocklistService { | |
80badf49 | 17 | static BASE_BLOCKLIST_URL = environment.apiUrl + '/api/v1/blocklist' |
af5767ff | 18 | static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist' |
65b21c96 | 19 | static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist' |
af5767ff C |
20 | |
21 | constructor ( | |
22 | private authHttp: HttpClient, | |
23 | private restExtractor: RestExtractor, | |
24 | private restService: RestService | |
25 | ) { } | |
26 | ||
80badf49 C |
27 | /** ********************* Blocklist status ***********************/ |
28 | ||
29 | getStatus (options: { | |
30 | accounts?: string[] | |
31 | hosts?: string[] | |
32 | }) { | |
33 | const { accounts, hosts } = options | |
34 | ||
35 | let params = new HttpParams() | |
36 | ||
37 | if (accounts) params = this.restService.addArrayParams(params, 'accounts', accounts) | |
38 | if (hosts) params = this.restService.addArrayParams(params, 'hosts', hosts) | |
39 | ||
40 | return this.authHttp.get<BlockStatus>(BlocklistService.BASE_BLOCKLIST_URL + '/status', { params }) | |
41 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
42 | } | |
43 | ||
9df52d66 | 44 | /** ********************* User -> Account blocklist ***********************/ |
af5767ff | 45 | |
22839330 RK |
46 | getUserAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) { |
47 | const { pagination, sort, search } = options | |
48 | ||
af5767ff C |
49 | let params = new HttpParams() |
50 | params = this.restService.addRestGetParams(params, pagination, sort) | |
51 | ||
22839330 RK |
52 | if (search) params = params.append('search', search) |
53 | ||
af5767ff C |
54 | return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params }) |
55 | .pipe( | |
af5767ff C |
56 | map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))), |
57 | catchError(err => this.restExtractor.handleError(err)) | |
58 | ) | |
59 | } | |
60 | ||
d473fd94 | 61 | blockAccountByUser (account: Pick<Account, 'nameWithHost'>) { |
af5767ff C |
62 | const body = { accountName: account.nameWithHost } |
63 | ||
64 | return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', body) | |
65 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
66 | } | |
67 | ||
d473fd94 | 68 | unblockAccountByUser (account: Pick<Account, 'nameWithHost'>) { |
af5767ff C |
69 | const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost |
70 | ||
71 | return this.authHttp.delete(path) | |
72 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
73 | } | |
74 | ||
9df52d66 | 75 | /** ********************* User -> Server blocklist ***********************/ |
af5767ff | 76 | |
22839330 RK |
77 | getUserServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) { |
78 | const { pagination, sort, search } = options | |
79 | ||
af5767ff C |
80 | let params = new HttpParams() |
81 | params = this.restService.addRestGetParams(params, pagination, sort) | |
82 | ||
22839330 RK |
83 | if (search) params = params.append('search', search) |
84 | ||
af5767ff | 85 | return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', { params }) |
3afe0ec3 | 86 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
af5767ff C |
87 | } |
88 | ||
89 | blockServerByUser (host: string) { | |
90 | const body = { host } | |
91 | ||
92 | return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body) | |
93 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
94 | } | |
95 | ||
96 | unblockServerByUser (host: string) { | |
97 | const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host | |
98 | ||
99 | return this.authHttp.delete(path) | |
100 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
101 | } | |
102 | ||
9df52d66 | 103 | /** ********************* Instance -> Account blocklist ***********************/ |
65b21c96 | 104 | |
22839330 | 105 | getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) { |
e0a92917 RK |
106 | const { pagination, sort, search } = options |
107 | ||
65b21c96 C |
108 | let params = new HttpParams() |
109 | params = this.restService.addRestGetParams(params, pagination, sort) | |
110 | ||
e0a92917 RK |
111 | if (search) params = params.append('search', search) |
112 | ||
65b21c96 C |
113 | return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { params }) |
114 | .pipe( | |
65b21c96 C |
115 | map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))), |
116 | catchError(err => this.restExtractor.handleError(err)) | |
117 | ) | |
118 | } | |
119 | ||
a282e4d8 | 120 | blockAccountByInstance (accountsArg: Pick<Account, 'nameWithHost'> | Pick<Account, 'nameWithHost'>[]) { |
e3d6c643 | 121 | const accounts = arrayify(accountsArg) |
65b21c96 | 122 | |
a282e4d8 C |
123 | return from(accounts) |
124 | .pipe( | |
125 | concatMap(a => this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { accountName: a.nameWithHost })), | |
126 | toArray(), | |
127 | catchError(err => this.restExtractor.handleError(err)) | |
128 | ) | |
65b21c96 C |
129 | } |
130 | ||
d473fd94 | 131 | unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) { |
65b21c96 C |
132 | const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost |
133 | ||
134 | return this.authHttp.delete(path) | |
135 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
136 | } | |
137 | ||
9df52d66 | 138 | /** ********************* Instance -> Server blocklist ***********************/ |
65b21c96 | 139 | |
22839330 | 140 | getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) { |
e0a92917 RK |
141 | const { pagination, sort, search } = options |
142 | ||
65b21c96 C |
143 | let params = new HttpParams() |
144 | params = this.restService.addRestGetParams(params, pagination, sort) | |
145 | ||
e0a92917 RK |
146 | if (search) params = params.append('search', search) |
147 | ||
65b21c96 | 148 | return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', { params }) |
3afe0ec3 | 149 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
65b21c96 C |
150 | } |
151 | ||
152 | blockServerByInstance (host: string) { | |
153 | const body = { host } | |
154 | ||
155 | return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', body) | |
156 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
157 | } | |
158 | ||
159 | unblockServerByInstance (host: string) { | |
160 | const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers/' + host | |
161 | ||
162 | return this.authHttp.delete(path) | |
163 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
164 | } | |
165 | ||
af5767ff C |
166 | private formatAccountBlock (accountBlock: AccountBlockServer) { |
167 | return new AccountBlock(accountBlock) | |
168 | } | |
169 | } |