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