Add user/instance block by users in the client
[oweals/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
15   constructor (
16     private authHttp: HttpClient,
17     private restExtractor: RestExtractor,
18     private restService: RestService
19   ) { }
20
21   /*********************** User -> Account blocklist ***********************/
22
23   getUserAccountBlocklist (pagination: RestPagination, sort: SortMeta) {
24     let params = new HttpParams()
25     params = this.restService.addRestGetParams(params, pagination, sort)
26
27     return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params })
28                .pipe(
29                  map(res => this.restExtractor.convertResultListDateToHuman(res)),
30                  map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
31                  catchError(err => this.restExtractor.handleError(err))
32                )
33   }
34
35   blockAccountByUser (account: Account) {
36     const body = { accountName: account.nameWithHost }
37
38     return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', body)
39                .pipe(catchError(err => this.restExtractor.handleError(err)))
40   }
41
42   unblockAccountByUser (account: Account) {
43     const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
44
45     return this.authHttp.delete(path)
46                .pipe(catchError(err => this.restExtractor.handleError(err)))
47   }
48
49   /*********************** User -> Server blocklist ***********************/
50
51   getUserServerBlocklist (pagination: RestPagination, sort: SortMeta) {
52     let params = new HttpParams()
53     params = this.restService.addRestGetParams(params, pagination, sort)
54
55     return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', { params })
56                .pipe(
57                  map(res => this.restExtractor.convertResultListDateToHuman(res)),
58                  catchError(err => this.restExtractor.handleError(err))
59                )
60   }
61
62   blockServerByUser (host: string) {
63     const body = { host }
64
65     return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body)
66                .pipe(catchError(err => this.restExtractor.handleError(err)))
67   }
68
69   unblockServerByUser (host: string) {
70     const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host
71
72     return this.authHttp.delete(path)
73                .pipe(catchError(err => this.restExtractor.handleError(err)))
74   }
75
76   private formatAccountBlock (accountBlock: AccountBlockServer) {
77     return new AccountBlock(accountBlock)
78   }
79 }