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