]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-moderation/blocklist.service.ts
Fix angular build, again
[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 { arrayify } from '@shared/core-utils'
8 import { AccountBlock as AccountBlockServer, BlockStatus, ResultList, ServerBlock } from '@shared/models'
9 import { environment } from '../../../environments/environment'
10 import { Account } from '../shared-main'
11 import { AccountBlock } from './account-block.model'
12
13 export enum BlocklistComponentType { Account, Instance }
14
15 @Injectable()
16 export class BlocklistService {
17 static BASE_BLOCKLIST_URL = environment.apiUrl + '/api/v1/blocklist'
18 static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist'
19 static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist'
20
21 constructor (
22 private authHttp: HttpClient,
23 private restExtractor: RestExtractor,
24 private restService: RestService
25 ) { }
26
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
44 /** ********************* User -> Account blocklist ***********************/
45
46 getUserAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
47 const { pagination, sort, search } = options
48
49 let params = new HttpParams()
50 params = this.restService.addRestGetParams(params, pagination, sort)
51
52 if (search) params = params.append('search', search)
53
54 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params })
55 .pipe(
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(catchError(err => this.restExtractor.handleError(err)))
87 }
88
89 blockServerByUser (host: string) {
90 const body = { host }
91
92 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body)
93 .pipe(catchError(err => this.restExtractor.handleError(err)))
94 }
95
96 unblockServerByUser (host: string) {
97 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host
98
99 return this.authHttp.delete(path)
100 .pipe(catchError(err => this.restExtractor.handleError(err)))
101 }
102
103 /** ********************* Instance -> Account blocklist ***********************/
104
105 getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
106 const { pagination, sort, search } = options
107
108 let params = new HttpParams()
109 params = this.restService.addRestGetParams(params, pagination, sort)
110
111 if (search) params = params.append('search', search)
112
113 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { params })
114 .pipe(
115 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
116 catchError(err => this.restExtractor.handleError(err))
117 )
118 }
119
120 blockAccountByInstance (accountsArg: Pick<Account, 'nameWithHost'> | Pick<Account, 'nameWithHost'>[]) {
121 const accounts = arrayify(accountsArg)
122
123 return from(accounts)
124 .pipe(
125 concatMap(a => this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { accountName: a.nameWithHost })),
126 toArray(),
127 catchError(err => this.restExtractor.handleError(err))
128 )
129 }
130
131 unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
132 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
133
134 return this.authHttp.delete(path)
135 .pipe(catchError(err => this.restExtractor.handleError(err)))
136 }
137
138 /** ********************* Instance -> Server blocklist ***********************/
139
140 getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
141 const { pagination, sort, search } = options
142
143 let params = new HttpParams()
144 params = this.restService.addRestGetParams(params, pagination, sort)
145
146 if (search) params = params.append('search', search)
147
148 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', { params })
149 .pipe(catchError(err => this.restExtractor.handleError(err)))
150 }
151
152 blockServerByInstance (host: string) {
153 const body = { host }
154
155 return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', body)
156 .pipe(catchError(err => this.restExtractor.handleError(err)))
157 }
158
159 unblockServerByInstance (host: string) {
160 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers/' + host
161
162 return this.authHttp.delete(path)
163 .pipe(catchError(err => this.restExtractor.handleError(err)))
164 }
165
166 private formatAccountBlock (accountBlock: AccountBlockServer) {
167 return new AccountBlock(accountBlock)
168 }
169 }