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 { AccountBlock as AccountBlockServer, BlockStatus, ResultList, ServerBlock } from '@shared/models'
8 import { environment } from '../../../environments/environment'
9 import { Account } from '../shared-main'
10 import { AccountBlock } from './account-block.model'
12 export enum BlocklistComponentType { Account, Instance }
15 export class BlocklistService {
16 static BASE_BLOCKLIST_URL = environment.apiUrl + '/api/v1/blocklist'
17 static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist'
18 static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist'
21 private authHttp: HttpClient,
22 private restExtractor: RestExtractor,
23 private restService: RestService
26 /** ********************* Blocklist status ***********************/
32 const { accounts, hosts } = options
34 let params = new HttpParams()
36 if (accounts) params = this.restService.addArrayParams(params, 'accounts', accounts)
37 if (hosts) params = this.restService.addArrayParams(params, 'hosts', hosts)
39 return this.authHttp.get<BlockStatus>(BlocklistService.BASE_BLOCKLIST_URL + '/status', { params })
40 .pipe(catchError(err => this.restExtractor.handleError(err)))
43 /** ********************* User -> Account blocklist ***********************/
45 getUserAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
46 const { pagination, sort, search } = options
48 let params = new HttpParams()
49 params = this.restService.addRestGetParams(params, pagination, sort)
51 if (search) params = params.append('search', search)
53 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params })
55 map(res => this.restExtractor.convertResultListDateToHuman(res)),
56 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
57 catchError(err => this.restExtractor.handleError(err))
61 blockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
62 const body = { accountName: account.nameWithHost }
64 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', body)
65 .pipe(catchError(err => this.restExtractor.handleError(err)))
68 unblockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
69 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
71 return this.authHttp.delete(path)
72 .pipe(catchError(err => this.restExtractor.handleError(err)))
75 /** ********************* User -> Server blocklist ***********************/
77 getUserServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
78 const { pagination, sort, search } = options
80 let params = new HttpParams()
81 params = this.restService.addRestGetParams(params, pagination, sort)
83 if (search) params = params.append('search', search)
85 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', { params })
87 map(res => this.restExtractor.convertResultListDateToHuman(res)),
88 catchError(err => this.restExtractor.handleError(err))
92 blockServerByUser (host: string) {
95 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body)
96 .pipe(catchError(err => this.restExtractor.handleError(err)))
99 unblockServerByUser (host: string) {
100 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host
102 return this.authHttp.delete(path)
103 .pipe(catchError(err => this.restExtractor.handleError(err)))
106 /** ********************* Instance -> Account blocklist ***********************/
108 getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
109 const { pagination, sort, search } = options
111 let params = new HttpParams()
112 params = this.restService.addRestGetParams(params, pagination, sort)
114 if (search) params = params.append('search', search)
116 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { params })
118 map(res => this.restExtractor.convertResultListDateToHuman(res)),
119 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
120 catchError(err => this.restExtractor.handleError(err))
124 blockAccountByInstance (accountsArg: Pick<Account, 'nameWithHost'> | Pick<Account, 'nameWithHost'>[]) {
125 const accounts = Array.isArray(accountsArg) ? accountsArg : [ accountsArg ]
127 return from(accounts)
129 concatMap(a => this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { accountName: a.nameWithHost })),
131 catchError(err => this.restExtractor.handleError(err))
135 unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
136 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
138 return this.authHttp.delete(path)
139 .pipe(catchError(err => this.restExtractor.handleError(err)))
142 /** ********************* Instance -> Server blocklist ***********************/
144 getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
145 const { pagination, sort, search } = options
147 let params = new HttpParams()
148 params = this.restService.addRestGetParams(params, pagination, sort)
150 if (search) params = params.append('search', search)
152 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', { params })
154 map(res => this.restExtractor.convertResultListDateToHuman(res)),
155 catchError(err => this.restExtractor.handleError(err))
159 blockServerByInstance (host: string) {
160 const body = { host }
162 return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', body)
163 .pipe(catchError(err => this.restExtractor.handleError(err)))
166 unblockServerByInstance (host: string) {
167 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers/' + host
169 return this.authHttp.delete(path)
170 .pipe(catchError(err => this.restExtractor.handleError(err)))
173 private formatAccountBlock (accountBlock: AccountBlockServer) {
174 return new AccountBlock(accountBlock)