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