]>
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( | |
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 | ||
d473fd94 | 62 | blockAccountByUser (account: Pick<Account, 'nameWithHost'>) { |
af5767ff C |
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 | ||
d473fd94 | 69 | unblockAccountByUser (account: Pick<Account, 'nameWithHost'>) { |
af5767ff C |
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 | ||
9df52d66 | 76 | /** ********************* User -> Server blocklist ***********************/ |
af5767ff | 77 | |
22839330 RK |
78 | getUserServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) { |
79 | const { pagination, sort, search } = options | |
80 | ||
af5767ff C |
81 | let params = new HttpParams() |
82 | params = this.restService.addRestGetParams(params, pagination, sort) | |
83 | ||
22839330 RK |
84 | if (search) params = params.append('search', search) |
85 | ||
af5767ff C |
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 | ||
9df52d66 | 107 | /** ********************* Instance -> Account blocklist ***********************/ |
65b21c96 | 108 | |
22839330 | 109 | getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) { |
e0a92917 RK |
110 | const { pagination, sort, search } = options |
111 | ||
65b21c96 C |
112 | let params = new HttpParams() |
113 | params = this.restService.addRestGetParams(params, pagination, sort) | |
114 | ||
e0a92917 RK |
115 | if (search) params = params.append('search', search) |
116 | ||
65b21c96 C |
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 | ||
a282e4d8 | 125 | blockAccountByInstance (accountsArg: Pick<Account, 'nameWithHost'> | Pick<Account, 'nameWithHost'>[]) { |
e3d6c643 | 126 | const accounts = arrayify(accountsArg) |
65b21c96 | 127 | |
a282e4d8 C |
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 | ) | |
65b21c96 C |
134 | } |
135 | ||
d473fd94 | 136 | unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) { |
65b21c96 C |
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 | ||
9df52d66 | 143 | /** ********************* Instance -> Server blocklist ***********************/ |
65b21c96 | 144 | |
22839330 | 145 | getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) { |
e0a92917 RK |
146 | const { pagination, sort, search } = options |
147 | ||
65b21c96 C |
148 | let params = new HttpParams() |
149 | params = this.restService.addRestGetParams(params, pagination, sort) | |
150 | ||
e0a92917 RK |
151 | if (search) params = params.append('search', search) |
152 | ||
65b21c96 C |
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 | ||
af5767ff C |
174 | private formatAccountBlock (accountBlock: AccountBlockServer) { |
175 | return new AccountBlock(accountBlock) | |
176 | } | |
177 | } |