1 import { SortMeta } from 'primeng/api'
2 import { from } from 'rxjs'
3 import { catchError, concatMap, map, toArray } from 'rxjs/operators'
4 import { HttpClient, HttpParams } from '@angular/common/http'
5 import { Injectable } from '@angular/core'
6 import { RestExtractor, RestPagination, RestService } from '@app/core'
7 import { arrayify } from '@shared/core-utils'
8 import { AccountBlock as AccountBlockServer, BlockStatus, ResultList, ServerBlock } from '@shared/models'
9 import { environment } from '../../../environments/environment'
10 import { Account } from '../shared-main'
11 import { AccountBlock } from './account-block.model'
13 export enum BlocklistComponentType { Account, Instance }
16 export class BlocklistService {
17 static BASE_BLOCKLIST_URL = environment.apiUrl + '/api/v1/blocklist'
18 static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist'
19 static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist'
22 private authHttp: HttpClient,
23 private restExtractor: RestExtractor,
24 private restService: RestService
27 /** ********************* Blocklist status ***********************/
33 const { accounts, hosts } = options
35 let params = new HttpParams()
37 if (accounts) params = this.restService.addArrayParams(params, 'accounts', accounts)
38 if (hosts) params = this.restService.addArrayParams(params, 'hosts', hosts)
40 return this.authHttp.get<BlockStatus>(BlocklistService.BASE_BLOCKLIST_URL + '/status', { params })
41 .pipe(catchError(err => this.restExtractor.handleError(err)))
44 /** ********************* User -> Account blocklist ***********************/
46 getUserAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
47 const { pagination, sort, search } = options
49 let params = new HttpParams()
50 params = this.restService.addRestGetParams(params, pagination, sort)
52 if (search) params = params.append('search', search)
54 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params })
56 map(res => this.restExtractor.convertResultListDateToHuman(res)),
57 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
58 catchError(err => this.restExtractor.handleError(err))
62 blockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
63 const body = { accountName: account.nameWithHost }
65 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', body)
66 .pipe(catchError(err => this.restExtractor.handleError(err)))
69 unblockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
70 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
72 return this.authHttp.delete(path)
73 .pipe(catchError(err => this.restExtractor.handleError(err)))
76 /** ********************* User -> Server blocklist ***********************/
78 getUserServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
79 const { pagination, sort, search } = options
81 let params = new HttpParams()
82 params = this.restService.addRestGetParams(params, pagination, sort)
84 if (search) params = params.append('search', search)
86 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', { params })
88 map(res => this.restExtractor.convertResultListDateToHuman(res)),
89 catchError(err => this.restExtractor.handleError(err))
93 blockServerByUser (host: string) {
96 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body)
97 .pipe(catchError(err => this.restExtractor.handleError(err)))
100 unblockServerByUser (host: string) {
101 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host
103 return this.authHttp.delete(path)
104 .pipe(catchError(err => this.restExtractor.handleError(err)))
107 /** ********************* Instance -> Account blocklist ***********************/
109 getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
110 const { pagination, sort, search } = options
112 let params = new HttpParams()
113 params = this.restService.addRestGetParams(params, pagination, sort)
115 if (search) params = params.append('search', search)
117 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { params })
119 map(res => this.restExtractor.convertResultListDateToHuman(res)),
120 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
121 catchError(err => this.restExtractor.handleError(err))
125 blockAccountByInstance (accountsArg: Pick<Account, 'nameWithHost'> | Pick<Account, 'nameWithHost'>[]) {
126 const accounts = arrayify(accountsArg)
128 return from(accounts)
130 concatMap(a => this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { accountName: a.nameWithHost })),
132 catchError(err => this.restExtractor.handleError(err))
136 unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
137 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
139 return this.authHttp.delete(path)
140 .pipe(catchError(err => this.restExtractor.handleError(err)))
143 /** ********************* Instance -> Server blocklist ***********************/
145 getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
146 const { pagination, sort, search } = options
148 let params = new HttpParams()
149 params = this.restService.addRestGetParams(params, pagination, sort)
151 if (search) params = params.append('search', search)
153 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', { params })
155 map(res => this.restExtractor.convertResultListDateToHuman(res)),
156 catchError(err => this.restExtractor.handleError(err))
160 blockServerByInstance (host: string) {
161 const body = { host }
163 return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', body)
164 .pipe(catchError(err => this.restExtractor.handleError(err)))
167 unblockServerByInstance (host: string) {
168 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers/' + host
170 return this.authHttp.delete(path)
171 .pipe(catchError(err => this.restExtractor.handleError(err)))
174 private formatAccountBlock (accountBlock: AccountBlockServer) {
175 return new AccountBlock(accountBlock)