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, BlockStatus, ResultList, ServerBlock } from '@shared/models'
7 import { environment } from '../../../environments/environment'
8 import { Account } from '../shared-main'
9 import { AccountBlock } from './account-block.model'
11 export enum BlocklistComponentType { Account, Instance }
14 export class BlocklistService {
15 static BASE_BLOCKLIST_URL = environment.apiUrl + '/api/v1/blocklist'
16 static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist'
17 static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist'
20 private authHttp: HttpClient,
21 private restExtractor: RestExtractor,
22 private restService: RestService
25 /** ********************* Blocklist status ***********************/
31 const { accounts, hosts } = options
33 let params = new HttpParams()
35 if (accounts) params = this.restService.addArrayParams(params, 'accounts', accounts)
36 if (hosts) params = this.restService.addArrayParams(params, 'hosts', hosts)
38 return this.authHttp.get<BlockStatus>(BlocklistService.BASE_BLOCKLIST_URL + '/status', { params })
39 .pipe(catchError(err => this.restExtractor.handleError(err)))
42 /** ********************* User -> Account blocklist ***********************/
44 getUserAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
45 const { pagination, sort, search } = options
47 let params = new HttpParams()
48 params = this.restService.addRestGetParams(params, pagination, sort)
50 if (search) params = params.append('search', search)
52 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params })
54 map(res => this.restExtractor.convertResultListDateToHuman(res)),
55 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
56 catchError(err => this.restExtractor.handleError(err))
60 blockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
61 const body = { accountName: account.nameWithHost }
63 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', body)
64 .pipe(catchError(err => this.restExtractor.handleError(err)))
67 unblockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
68 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
70 return this.authHttp.delete(path)
71 .pipe(catchError(err => this.restExtractor.handleError(err)))
74 /** ********************* User -> Server blocklist ***********************/
76 getUserServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
77 const { pagination, sort, search } = options
79 let params = new HttpParams()
80 params = this.restService.addRestGetParams(params, pagination, sort)
82 if (search) params = params.append('search', search)
84 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', { params })
86 map(res => this.restExtractor.convertResultListDateToHuman(res)),
87 catchError(err => this.restExtractor.handleError(err))
91 blockServerByUser (host: string) {
94 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body)
95 .pipe(catchError(err => this.restExtractor.handleError(err)))
98 unblockServerByUser (host: string) {
99 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host
101 return this.authHttp.delete(path)
102 .pipe(catchError(err => this.restExtractor.handleError(err)))
105 /** ********************* Instance -> Account blocklist ***********************/
107 getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
108 const { pagination, sort, search } = options
110 let params = new HttpParams()
111 params = this.restService.addRestGetParams(params, pagination, sort)
113 if (search) params = params.append('search', search)
115 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { params })
117 map(res => this.restExtractor.convertResultListDateToHuman(res)),
118 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
119 catchError(err => this.restExtractor.handleError(err))
123 blockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
124 const body = { accountName: account.nameWithHost }
126 return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', body)
127 .pipe(catchError(err => this.restExtractor.handleError(err)))
130 unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
131 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
133 return this.authHttp.delete(path)
134 .pipe(catchError(err => this.restExtractor.handleError(err)))
137 /** ********************* Instance -> Server blocklist ***********************/
139 getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
140 const { pagination, sort, search } = options
142 let params = new HttpParams()
143 params = this.restService.addRestGetParams(params, pagination, sort)
145 if (search) params = params.append('search', search)
147 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', { params })
149 map(res => this.restExtractor.convertResultListDateToHuman(res)),
150 catchError(err => this.restExtractor.handleError(err))
154 blockServerByInstance (host: string) {
155 const body = { host }
157 return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', body)
158 .pipe(catchError(err => this.restExtractor.handleError(err)))
161 unblockServerByInstance (host: string) {
162 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers/' + host
164 return this.authHttp.delete(path)
165 .pipe(catchError(err => this.restExtractor.handleError(err)))
168 private formatAccountBlock (accountBlock: AccountBlockServer) {
169 return new AccountBlock(accountBlock)