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'
14 static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist'
17 private authHttp: HttpClient,
18 private restExtractor: RestExtractor,
19 private restService: RestService
22 /*********************** User -> Account blocklist ***********************/
24 getUserAccountBlocklist (pagination: RestPagination, sort: SortMeta) {
25 let params = new HttpParams()
26 params = this.restService.addRestGetParams(params, pagination, sort)
28 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params })
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))
36 blockAccountByUser (account: Account) {
37 const body = { accountName: account.nameWithHost }
39 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', body)
40 .pipe(catchError(err => this.restExtractor.handleError(err)))
43 unblockAccountByUser (account: Account) {
44 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
46 return this.authHttp.delete(path)
47 .pipe(catchError(err => this.restExtractor.handleError(err)))
50 /*********************** User -> Server blocklist ***********************/
52 getUserServerBlocklist (pagination: RestPagination, sort: SortMeta) {
53 let params = new HttpParams()
54 params = this.restService.addRestGetParams(params, pagination, sort)
56 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', { params })
58 map(res => this.restExtractor.convertResultListDateToHuman(res)),
59 catchError(err => this.restExtractor.handleError(err))
63 blockServerByUser (host: string) {
66 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body)
67 .pipe(catchError(err => this.restExtractor.handleError(err)))
70 unblockServerByUser (host: string) {
71 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host
73 return this.authHttp.delete(path)
74 .pipe(catchError(err => this.restExtractor.handleError(err)))
77 /*********************** Instance -> Account blocklist ***********************/
79 getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search: string }) {
80 const { pagination, sort, search } = options
82 let params = new HttpParams()
83 params = this.restService.addRestGetParams(params, pagination, sort)
85 if (search) params = params.append('search', search)
87 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { params })
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))
95 blockAccountByInstance (account: Account) {
96 const body = { accountName: account.nameWithHost }
98 return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', body)
99 .pipe(catchError(err => this.restExtractor.handleError(err)))
102 unblockAccountByInstance (account: Account) {
103 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
105 return this.authHttp.delete(path)
106 .pipe(catchError(err => this.restExtractor.handleError(err)))
109 /*********************** Instance -> Server blocklist ***********************/
111 getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search: string }) {
112 const { pagination, sort, search } = options
114 let params = new HttpParams()
115 params = this.restService.addRestGetParams(params, pagination, sort)
117 if (search) params = params.append('search', search)
119 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', { params })
121 map(res => this.restExtractor.convertResultListDateToHuman(res)),
122 catchError(err => this.restExtractor.handleError(err))
126 blockServerByInstance (host: string) {
127 const body = { host }
129 return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', body)
130 .pipe(catchError(err => this.restExtractor.handleError(err)))
133 unblockServerByInstance (host: string) {
134 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers/' + host
136 return this.authHttp.delete(path)
137 .pipe(catchError(err => this.restExtractor.handleError(err)))
140 private formatAccountBlock (accountBlock: AccountBlockServer) {
141 return new AccountBlock(accountBlock)