2018-10-12 15:26:40 +00:00
|
|
|
import { SortMeta } from 'primeng/api'
|
2022-02-28 09:41:23 +00:00
|
|
|
import { from } from 'rxjs'
|
|
|
|
import { catchError, concatMap, map, toArray } from 'rxjs/operators'
|
2020-06-23 12:10:17 +00:00
|
|
|
import { HttpClient, HttpParams } from '@angular/common/http'
|
|
|
|
import { Injectable } from '@angular/core'
|
|
|
|
import { RestExtractor, RestPagination, RestService } from '@app/core'
|
2022-07-27 11:44:40 +00:00
|
|
|
import { arrayify } from '@shared/core-utils'
|
2021-12-06 15:53:00 +00:00
|
|
|
import { AccountBlock as AccountBlockServer, BlockStatus, ResultList, ServerBlock } from '@shared/models'
|
2020-06-23 12:10:17 +00:00
|
|
|
import { environment } from '../../../environments/environment'
|
|
|
|
import { Account } from '../shared-main'
|
|
|
|
import { AccountBlock } from './account-block.model'
|
2018-10-12 15:26:40 +00:00
|
|
|
|
2020-06-15 11:18:22 +00:00
|
|
|
export enum BlocklistComponentType { Account, Instance }
|
|
|
|
|
2018-10-12 15:26:40 +00:00
|
|
|
@Injectable()
|
|
|
|
export class BlocklistService {
|
2021-12-06 15:53:00 +00:00
|
|
|
static BASE_BLOCKLIST_URL = environment.apiUrl + '/api/v1/blocklist'
|
2018-10-12 15:26:40 +00:00
|
|
|
static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist'
|
2018-10-15 14:43:14 +00:00
|
|
|
static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist'
|
2018-10-12 15:26:40 +00:00
|
|
|
|
|
|
|
constructor (
|
|
|
|
private authHttp: HttpClient,
|
|
|
|
private restExtractor: RestExtractor,
|
|
|
|
private restService: RestService
|
|
|
|
) { }
|
|
|
|
|
2021-12-06 15:53:00 +00:00
|
|
|
/** ********************* Blocklist status ***********************/
|
|
|
|
|
|
|
|
getStatus (options: {
|
|
|
|
accounts?: string[]
|
|
|
|
hosts?: string[]
|
|
|
|
}) {
|
|
|
|
const { accounts, hosts } = options
|
|
|
|
|
|
|
|
let params = new HttpParams()
|
|
|
|
|
|
|
|
if (accounts) params = this.restService.addArrayParams(params, 'accounts', accounts)
|
|
|
|
if (hosts) params = this.restService.addArrayParams(params, 'hosts', hosts)
|
|
|
|
|
|
|
|
return this.authHttp.get<BlockStatus>(BlocklistService.BASE_BLOCKLIST_URL + '/status', { params })
|
|
|
|
.pipe(catchError(err => this.restExtractor.handleError(err)))
|
|
|
|
}
|
|
|
|
|
2021-08-17 12:42:53 +00:00
|
|
|
/** ********************* User -> Account blocklist ***********************/
|
2018-10-12 15:26:40 +00:00
|
|
|
|
2020-06-15 11:18:22 +00:00
|
|
|
getUserAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
|
|
|
|
const { pagination, sort, search } = options
|
|
|
|
|
2018-10-12 15:26:40 +00:00
|
|
|
let params = new HttpParams()
|
|
|
|
params = this.restService.addRestGetParams(params, pagination, sort)
|
|
|
|
|
2020-06-15 11:18:22 +00:00
|
|
|
if (search) params = params.append('search', search)
|
|
|
|
|
2018-10-12 15:26:40 +00:00
|
|
|
return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params })
|
|
|
|
.pipe(
|
|
|
|
map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
|
|
|
|
catchError(err => this.restExtractor.handleError(err))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-28 14:38:51 +00:00
|
|
|
blockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
|
2018-10-12 15:26:40 +00:00
|
|
|
const body = { accountName: account.nameWithHost }
|
|
|
|
|
|
|
|
return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', body)
|
|
|
|
.pipe(catchError(err => this.restExtractor.handleError(err)))
|
|
|
|
}
|
|
|
|
|
2020-06-28 14:38:51 +00:00
|
|
|
unblockAccountByUser (account: Pick<Account, 'nameWithHost'>) {
|
2018-10-12 15:26:40 +00:00
|
|
|
const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
|
|
|
|
|
|
|
|
return this.authHttp.delete(path)
|
|
|
|
.pipe(catchError(err => this.restExtractor.handleError(err)))
|
|
|
|
}
|
|
|
|
|
2021-08-17 12:42:53 +00:00
|
|
|
/** ********************* User -> Server blocklist ***********************/
|
2018-10-12 15:26:40 +00:00
|
|
|
|
2020-06-15 11:18:22 +00:00
|
|
|
getUserServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
|
|
|
|
const { pagination, sort, search } = options
|
|
|
|
|
2018-10-12 15:26:40 +00:00
|
|
|
let params = new HttpParams()
|
|
|
|
params = this.restService.addRestGetParams(params, pagination, sort)
|
|
|
|
|
2020-06-15 11:18:22 +00:00
|
|
|
if (search) params = params.append('search', search)
|
|
|
|
|
2018-10-12 15:26:40 +00:00
|
|
|
return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', { params })
|
2022-09-16 08:33:26 +00:00
|
|
|
.pipe(catchError(err => this.restExtractor.handleError(err)))
|
2018-10-12 15:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blockServerByUser (host: string) {
|
|
|
|
const body = { host }
|
|
|
|
|
|
|
|
return this.authHttp.post(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', body)
|
|
|
|
.pipe(catchError(err => this.restExtractor.handleError(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
unblockServerByUser (host: string) {
|
|
|
|
const path = BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers/' + host
|
|
|
|
|
|
|
|
return this.authHttp.delete(path)
|
|
|
|
.pipe(catchError(err => this.restExtractor.handleError(err)))
|
|
|
|
}
|
|
|
|
|
2021-08-17 12:42:53 +00:00
|
|
|
/** ********************* Instance -> Account blocklist ***********************/
|
2018-10-15 14:43:14 +00:00
|
|
|
|
2020-06-15 11:18:22 +00:00
|
|
|
getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
|
2020-04-19 12:11:40 +00:00
|
|
|
const { pagination, sort, search } = options
|
|
|
|
|
2018-10-15 14:43:14 +00:00
|
|
|
let params = new HttpParams()
|
|
|
|
params = this.restService.addRestGetParams(params, pagination, sort)
|
|
|
|
|
2020-04-19 12:11:40 +00:00
|
|
|
if (search) params = params.append('search', search)
|
|
|
|
|
2018-10-15 14:43:14 +00:00
|
|
|
return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { params })
|
|
|
|
.pipe(
|
|
|
|
map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
|
|
|
|
catchError(err => this.restExtractor.handleError(err))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:41:23 +00:00
|
|
|
blockAccountByInstance (accountsArg: Pick<Account, 'nameWithHost'> | Pick<Account, 'nameWithHost'>[]) {
|
2022-07-27 11:44:40 +00:00
|
|
|
const accounts = arrayify(accountsArg)
|
2018-10-15 14:43:14 +00:00
|
|
|
|
2022-02-28 09:41:23 +00:00
|
|
|
return from(accounts)
|
|
|
|
.pipe(
|
|
|
|
concatMap(a => this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { accountName: a.nameWithHost })),
|
|
|
|
toArray(),
|
|
|
|
catchError(err => this.restExtractor.handleError(err))
|
|
|
|
)
|
2018-10-15 14:43:14 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 14:38:51 +00:00
|
|
|
unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
|
2018-10-15 14:43:14 +00:00
|
|
|
const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts/' + account.nameWithHost
|
|
|
|
|
|
|
|
return this.authHttp.delete(path)
|
|
|
|
.pipe(catchError(err => this.restExtractor.handleError(err)))
|
|
|
|
}
|
|
|
|
|
2021-08-17 12:42:53 +00:00
|
|
|
/** ********************* Instance -> Server blocklist ***********************/
|
2018-10-15 14:43:14 +00:00
|
|
|
|
2020-06-15 11:18:22 +00:00
|
|
|
getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
|
2020-04-19 12:11:40 +00:00
|
|
|
const { pagination, sort, search } = options
|
|
|
|
|
2018-10-15 14:43:14 +00:00
|
|
|
let params = new HttpParams()
|
|
|
|
params = this.restService.addRestGetParams(params, pagination, sort)
|
|
|
|
|
2020-04-19 12:11:40 +00:00
|
|
|
if (search) params = params.append('search', search)
|
|
|
|
|
2018-10-15 14:43:14 +00:00
|
|
|
return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', { params })
|
2022-09-16 08:33:26 +00:00
|
|
|
.pipe(catchError(err => this.restExtractor.handleError(err)))
|
2018-10-15 14:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blockServerByInstance (host: string) {
|
|
|
|
const body = { host }
|
|
|
|
|
|
|
|
return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', body)
|
|
|
|
.pipe(catchError(err => this.restExtractor.handleError(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
unblockServerByInstance (host: string) {
|
|
|
|
const path = BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers/' + host
|
|
|
|
|
|
|
|
return this.authHttp.delete(path)
|
|
|
|
.pipe(catchError(err => this.restExtractor.handleError(err)))
|
|
|
|
}
|
|
|
|
|
2018-10-12 15:26:40 +00:00
|
|
|
private formatAccountBlock (accountBlock: AccountBlockServer) {
|
|
|
|
return new AccountBlock(accountBlock)
|
|
|
|
}
|
|
|
|
}
|