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