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