]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-moderation/blocklist.service.ts
Fix abuse tests
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / blocklist.service.ts
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 { AccountBlock as AccountBlockServer, BlockStatus, ResultList, ServerBlock } from '@shared/models'
8 import { environment } from '../../../environments/environment'
9 import { Account } from '../shared-main'
10 import { AccountBlock } from './account-block.model'
11
12 export enum BlocklistComponentType { Account, Instance }
13
14 @Injectable()
15 export class BlocklistService {
16 static BASE_BLOCKLIST_URL = environment.apiUrl + '/api/v1/blocklist'
17 static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist'
18 static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist'
19
20 constructor (
21 private authHttp: HttpClient,
22 private restExtractor: RestExtractor,
23 private restService: RestService
24 ) { }
25
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
43 /** ********************* User -> Account blocklist ***********************/
44
45 getUserAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
46 const { pagination, sort, search } = options
47
48 let params = new HttpParams()
49 params = this.restService.addRestGetParams(params, pagination, sort)
50
51 if (search) params = params.append('search', search)
52
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
61 blockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
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
68 unblockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
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
75 /** ********************* User -> Server blocklist ***********************/
76
77 getUserServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
78 const { pagination, sort, search } = options
79
80 let params = new HttpParams()
81 params = this.restService.addRestGetParams(params, pagination, sort)
82
83 if (search) params = params.append('search', search)
84
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
106 /** ********************* Instance -> Account blocklist ***********************/
107
108 getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
109 const { pagination, sort, search } = options
110
111 let params = new HttpParams()
112 params = this.restService.addRestGetParams(params, pagination, sort)
113
114 if (search) params = params.append('search', search)
115
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
124 blockAccountByInstance (accountsArg: Pick<Account, 'nameWithHost'> | Pick<Account, 'nameWithHost'>[]) {
125 const accounts = Array.isArray(accountsArg) ? accountsArg : [ accountsArg ]
126
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 )
133 }
134
135 unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
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
142 /** ********************* Instance -> Server blocklist ***********************/
143
144 getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
145 const { pagination, sort, search } = options
146
147 let params = new HttpParams()
148 params = this.restService.addRestGetParams(params, pagination, sort)
149
150 if (search) params = params.append('search', search)
151
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
173 private formatAccountBlock (accountBlock: AccountBlockServer) {
174 return new AccountBlock(accountBlock)
175 }
176 }