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.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 })
86 .pipe(catchError(err => this.restExtractor.handleError(err)))
89 blockServerByUser (host: string) {
92 return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body)
93 .pipe(catchError(err => this.restExtractor.handleError(err)))
96 unblockServerByUser (host: string) {
97 const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host
99 return this.authHttp.delete(path)
100 .pipe(catchError(err => this.restExtractor.handleError(err)))
103 /** ********************* Instance -> Account blocklist ***********************/
105 getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
106 const { pagination, sort, search } = options
108 let params = new HttpParams()
109 params = this.restService.addRestGetParams(params, pagination, sort)
111 if (search) params = params.append('search', search)
113 return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { params })
115 map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
116 catchError(err => this.restExtractor.handleError(err))
120 blockAccountByInstance (accountsArg: Pick<Account, 'nameWithHost'> | Pick<Account, 'nameWithHost'>[]) {
121 const accounts = arrayify(accountsArg)
123 return from(accounts)
125 concatMap(a => this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { accountName: a.nameWithHost })),
127 catchError(err => this.restExtractor.handleError(err))
131 unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
132 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
134 return this.authHttp.delete(path)
135 .pipe(catchError(err => this.restExtractor.handleError(err)))
138 /** ********************* Instance -> Server blocklist ***********************/
140 getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
141 const { pagination, sort, search } = options
143 let params = new HttpParams()
144 params = this.restService.addRestGetParams(params, pagination, sort)
146 if (search) params = params.append('search', search)
148 return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', { params })
149 .pipe(catchError(err => this.restExtractor.handleError(err)))
152 blockServerByInstance (host: string) {
153 const body = { host }
155 return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', body)
156 .pipe(catchError(err => this.restExtractor.handleError(err)))
159 unblockServerByInstance (host: string) {
160 const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers/' + host
162 return this.authHttp.delete(path)
163 .pipe(catchError(err => this.restExtractor.handleError(err)))
166 private formatAccountBlock (accountBlock: AccountBlockServer) {
167 return new AccountBlock(accountBlock)