]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-moderation/blocklist.service.ts
Remove unnecessary onPage event on admin tables
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / blocklist.service.ts
CommitLineData
af5767ff
C
1import { SortMeta } from 'primeng/api'
2import { catchError, map } from 'rxjs/operators'
67ed6552
C
3import { HttpClient, HttpParams } from '@angular/common/http'
4import { Injectable } from '@angular/core'
5import { RestExtractor, RestPagination, RestService } from '@app/core'
80badf49 6import { AccountBlock as AccountBlockServer, BlockStatus, ResultList, ServerBlock } from '@shared/models'
67ed6552
C
7import { environment } from '../../../environments/environment'
8import { Account } from '../shared-main'
9import { AccountBlock } from './account-block.model'
af5767ff 10
22839330
RK
11export enum BlocklistComponentType { Account, Instance }
12
af5767ff
C
13@Injectable()
14export class BlocklistService {
80badf49 15 static BASE_BLOCKLIST_URL = environment.apiUrl + '/api/v1/blocklist'
af5767ff 16 static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist'
65b21c96 17 static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist'
af5767ff
C
18
19 constructor (
20 private authHttp: HttpClient,
21 private restExtractor: RestExtractor,
22 private restService: RestService
23 ) { }
24
80badf49
C
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
9df52d66 42 /** ********************* User -> Account blocklist ***********************/
af5767ff 43
22839330
RK
44 getUserAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
45 const { pagination, sort, search } = options
46
af5767ff
C
47 let params = new HttpParams()
48 params = this.restService.addRestGetParams(params, pagination, sort)
49
22839330
RK
50 if (search) params = params.append('search', search)
51
af5767ff
C
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
d473fd94 60 blockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
af5767ff
C
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
d473fd94 67 unblockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
af5767ff
C
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
9df52d66 74 /** ********************* User -> Server blocklist ***********************/
af5767ff 75
22839330
RK
76 getUserServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
77 const { pagination, sort, search } = options
78
af5767ff
C
79 let params = new HttpParams()
80 params = this.restService.addRestGetParams(params, pagination, sort)
81
22839330
RK
82 if (search) params = params.append('search', search)
83
af5767ff
C
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
9df52d66 105 /** ********************* Instance -> Account blocklist ***********************/
65b21c96 106
22839330 107 getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
e0a92917
RK
108 const { pagination, sort, search } = options
109
65b21c96
C
110 let params = new HttpParams()
111 params = this.restService.addRestGetParams(params, pagination, sort)
112
e0a92917
RK
113 if (search) params = params.append('search', search)
114
65b21c96
C
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
d473fd94 123 blockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
65b21c96
C
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
d473fd94 130 unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
65b21c96
C
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
9df52d66 137 /** ********************* Instance -> Server blocklist ***********************/
65b21c96 138
22839330 139 getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
e0a92917
RK
140 const { pagination, sort, search } = options
141
65b21c96
C
142 let params = new HttpParams()
143 params = this.restService.addRestGetParams(params, pagination, sort)
144
e0a92917
RK
145 if (search) params = params.append('search', search)
146
65b21c96
C
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
af5767ff
C
168 private formatAccountBlock (accountBlock: AccountBlockServer) {
169 return new AccountBlock(accountBlock)
170 }
171}