import { SortMeta } from 'primeng/api'
-import { catchError, map } from 'rxjs/operators'
+import { from } from 'rxjs'
+import { catchError, concatMap, map, toArray } from 'rxjs/operators'
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { RestExtractor, RestPagination, RestService } from '@app/core'
-import { AccountBlock as AccountBlockServer, ResultList, ServerBlock } from '@shared/models'
+import { arrayify } from '@shared/core-utils'
+import { AccountBlock as AccountBlockServer, BlockStatus, ResultList, ServerBlock } from '@shared/models'
import { environment } from '../../../environments/environment'
import { Account } from '../shared-main'
import { AccountBlock } from './account-block.model'
@Injectable()
export class BlocklistService {
+ static BASE_BLOCKLIST_URL = environment.apiUrl + '/api/v1/blocklist'
static BASE_USER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/users/me/blocklist'
static BASE_SERVER_BLOCKLIST_URL = environment.apiUrl + '/api/v1/server/blocklist'
private restService: RestService
) { }
- /*********************** User -> Account blocklist ***********************/
+ /** ********************* 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)))
+ }
+
+ /** ********************* User -> Account blocklist ***********************/
getUserAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
const { pagination, sort, search } = options
return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/accounts', { params })
.pipe(
- map(res => this.restExtractor.convertResultListDateToHuman(res)),
map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
catchError(err => this.restExtractor.handleError(err))
)
.pipe(catchError(err => this.restExtractor.handleError(err)))
}
- /*********************** User -> Server blocklist ***********************/
+ /** ********************* User -> Server blocklist ***********************/
getUserServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
const { pagination, sort, search } = options
if (search) params = params.append('search', search)
return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_USER_BLOCKLIST_URL + '/servers', { params })
- .pipe(
- map(res => this.restExtractor.convertResultListDateToHuman(res)),
- catchError(err => this.restExtractor.handleError(err))
- )
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
blockServerByUser (host: string) {
.pipe(catchError(err => this.restExtractor.handleError(err)))
}
- /*********************** Instance -> Account blocklist ***********************/
+ /** ********************* Instance -> Account blocklist ***********************/
getInstanceAccountBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
const { pagination, sort, search } = options
return this.authHttp.get<ResultList<AccountBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', { params })
.pipe(
- map(res => this.restExtractor.convertResultListDateToHuman(res)),
map(res => this.restExtractor.applyToResultListData(res, this.formatAccountBlock.bind(this))),
catchError(err => this.restExtractor.handleError(err))
)
}
- blockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
- const body = { accountName: account.nameWithHost }
+ blockAccountByInstance (accountsArg: Pick<Account, 'nameWithHost'> | Pick<Account, 'nameWithHost'>[]) {
+ const accounts = arrayify(accountsArg)
- return this.authHttp.post(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/accounts', body)
- .pipe(catchError(err => this.restExtractor.handleError(err)))
+ 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))
+ )
}
unblockAccountByInstance (account: Pick<Account, 'nameWithHost'>) {
.pipe(catchError(err => this.restExtractor.handleError(err)))
}
- /*********************** Instance -> Server blocklist ***********************/
+ /** ********************* Instance -> Server blocklist ***********************/
getInstanceServerBlocklist (options: { pagination: RestPagination, sort: SortMeta, search?: string }) {
const { pagination, sort, search } = options
if (search) params = params.append('search', search)
return this.authHttp.get<ResultList<ServerBlock>>(BlocklistService.BASE_SERVER_BLOCKLIST_URL + '/servers', { params })
- .pipe(
- map(res => this.restExtractor.convertResultListDateToHuman(res)),
- catchError(err => this.restExtractor.handleError(err))
- )
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
blockServerByInstance (host: string) {