]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-moderation/blocklist.service.ts
Add mute status in account and channel pages
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / blocklist.service.ts
1 import { SortMeta } from 'primeng/api'
2 import { catchError, map } from 'rxjs/operators'
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, BlockStatus, ResultList, ServerBlock } from '@shared/models'
7 import { environment } from '../../../environments/environment'
8 import { Account } from '../shared-main'
9 import { AccountBlock } from './account-block.model'
10
11 export enum BlocklistComponentType { Account, Instance }
12
13 @Injectable()
14 export class BlocklistService {
15 static BASE_BLOCKLIST_URL = environment.apiUrl + '/api/v1/blocklist'
16 static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist'
17 static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist'
18
19 constructor (
20 private authHttp: HttpClient,
21 private restExtractor: RestExtractor,
22 private restService: RestService
23 ) { }
24
25 /** ********************* Blocklist status ***********************/
26
27 getStatus (options: {
28 accounts?: string[]
29 hosts?: string[]
30 }) {
31 const { accounts, hosts } = options
32
33 let params = new HttpParams()
34
35 if (accounts) params = this.restService.addArrayParams(params, 'accounts', accounts)
36 if (hosts) params = this.restService.addArrayParams(params, 'hosts', hosts)
37
38 return this.authHttp.get<BlockStatus>(BlocklistService.BASE_BLOCKLIST_URL + '/status', { params })
39 .pipe(catchError(err => this.restExtractor.handleError(err)))
40 }
41
42 /** ********************* User -> Account blocklist ***********************/
43
44 getUserAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
45 const { pagination, sort, search } = options
46
47 let params = new HttpParams()
48 params = this.restService.addRestGetParams(params, pagination, sort)
49
50 if (search) params = params.append('search', search)
51
52 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params })
53 .pipe(
54 map(res => this.restExtractor.convertResultListDateToHuman(res)),
55 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
56 catchError(err => this.restExtractor.handleError(err))
57 )
58 }
59
60 blockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
61 const body = { accountName: account.nameWithHost }
62
63 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', body)
64 .pipe(catchError(err => this.restExtractor.handleError(err)))
65 }
66
67 unblockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
68 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
69
70 return this.authHttp.delete(path)
71 .pipe(catchError(err => this.restExtractor.handleError(err)))
72 }
73
74 /** ********************* User -> Server blocklist ***********************/
75
76 getUserServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
77 const { pagination, sort, search } = options
78
79 let params = new HttpParams()
80 params = this.restService.addRestGetParams(params, pagination, sort)
81
82 if (search) params = params.append('search', search)
83
84 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', { params })
85 .pipe(
86 map(res => this.restExtractor.convertResultListDateToHuman(res)),
87 catchError(err => this.restExtractor.handleError(err))
88 )
89 }
90
91 blockServerByUser (host: string) {
92 const body = { host }
93
94 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body)
95 .pipe(catchError(err => this.restExtractor.handleError(err)))
96 }
97
98 unblockServerByUser (host: string) {
99 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host
100
101 return this.authHttp.delete(path)
102 .pipe(catchError(err => this.restExtractor.handleError(err)))
103 }
104
105 /** ********************* Instance -> Account blocklist ***********************/
106
107 getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
108 const { pagination, sort, search } = options
109
110 let params = new HttpParams()
111 params = this.restService.addRestGetParams(params, pagination, sort)
112
113 if (search) params = params.append('search', search)
114
115 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { params })
116 .pipe(
117 map(res => this.restExtractor.convertResultListDateToHuman(res)),
118 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
119 catchError(err => this.restExtractor.handleError(err))
120 )
121 }
122
123 blockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
124 const body = { accountName: account.nameWithHost }
125
126 return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', body)
127 .pipe(catchError(err => this.restExtractor.handleError(err)))
128 }
129
130 unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
131 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
132
133 return this.authHttp.delete(path)
134 .pipe(catchError(err => this.restExtractor.handleError(err)))
135 }
136
137 /** ********************* Instance -> Server blocklist ***********************/
138
139 getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
140 const { pagination, sort, search } = options
141
142 let params = new HttpParams()
143 params = this.restService.addRestGetParams(params, pagination, sort)
144
145 if (search) params = params.append('search', search)
146
147 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', { params })
148 .pipe(
149 map(res => this.restExtractor.convertResultListDateToHuman(res)),
150 catchError(err => this.restExtractor.handleError(err))
151 )
152 }
153
154 blockServerByInstance (host: string) {
155 const body = { host }
156
157 return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', body)
158 .pipe(catchError(err => this.restExtractor.handleError(err)))
159 }
160
161 unblockServerByInstance (host: string) {
162 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers/' + host
163
164 return this.authHttp.delete(path)
165 .pipe(catchError(err => this.restExtractor.handleError(err)))
166 }
167
168 private formatAccountBlock (accountBlock: AccountBlockServer) {
169 return new AccountBlock(accountBlock)
170 }
171 }