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'
12 export class BlocklistService {
13 static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist'
16 private authHttp: HttpClient,
17 private restExtractor: RestExtractor,
18 private restService: RestService
21 /*********************** User -> Account blocklist ***********************/
23 getUserAccountBlocklist (pagination: RestPagination, sort: SortMeta) {
24 let params = new HttpParams()
25 params = this.restService.addRestGetParams(params, pagination, sort)
27 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params })
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))
35 blockAccountByUser (account: Account) {
36 const body = { accountName: account.nameWithHost }
38 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', body)
39 .pipe(catchError(err => this.restExtractor.handleError(err)))
42 unblockAccountByUser (account: Account) {
43 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
45 return this.authHttp.delete(path)
46 .pipe(catchError(err => this.restExtractor.handleError(err)))
49 /*********************** User -> Server blocklist ***********************/
51 getUserServerBlocklist (pagination: RestPagination, sort: SortMeta) {
52 let params = new HttpParams()
53 params = this.restService.addRestGetParams(params, pagination, sort)
55 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', { params })
57 map(res => this.restExtractor.convertResultListDateToHuman(res)),
58 catchError(err => this.restExtractor.handleError(err))
62 blockServerByUser (host: string) {
65 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body)
66 .pipe(catchError(err => this.restExtractor.handleError(err)))
69 unblockServerByUser (host: string) {
70 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host
72 return this.authHttp.delete(path)
73 .pipe(catchError(err => this.restExtractor.handleError(err)))
76 private formatAccountBlock (accountBlock: AccountBlockServer) {
77 return new AccountBlock(accountBlock)