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