1 import { SortMeta } from 'primeng/api'
2 import { Directive, OnInit, ViewChild } from '@angular/core'
3 import { Notifier, RestPagination, RestTable } from '@app/core'
4 import { BatchDomainsModalComponent } from '@app/shared/shared-moderation/batch-domains-modal.component'
5 import { ServerBlock } from '@shared/models'
6 import { BlocklistComponentType, BlocklistService } from './blocklist.service'
9 // eslint-disable-next-line @angular-eslint/directive-class-suffix
10 export class GenericServerBlocklistComponent extends RestTable implements OnInit {
11 @ViewChild('batchDomainsModal') batchDomainsModal: BatchDomainsModalComponent
13 // @ts-expect-error: "Abstract methods can only appear within an abstract class"
14 public abstract mode: BlocklistComponentType
16 blockedServers: ServerBlock[] = []
18 sort: SortMeta = { field: 'createdAt', order: -1 }
19 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
22 protected notifier: Notifier,
23 protected blocklistService: BlocklistService
28 // @ts-expect-error: "Abstract methods can only appear within an abstract class"
29 public abstract getIdentifier (): string
35 unblockServer (serverBlock: ServerBlock) {
36 const operation = (host: string) => this.mode === BlocklistComponentType.Account
37 ? this.blocklistService.unblockServerByUser(host)
38 : this.blocklistService.unblockServerByInstance(host)
39 const host = serverBlock.blockedServer.host
41 operation(host).subscribe(
43 this.notifier.success(
44 this.mode === BlocklistComponentType.Account
45 ? $localize`Instance ${host} unmuted.`
46 : $localize`Instance ${host} unmuted by your instance.`
54 addServersToBlock () {
55 this.batchDomainsModal.openModal()
58 onDomainsToBlock (domains: string[]) {
59 const operation = (domain: string) => this.mode === BlocklistComponentType.Account
60 ? this.blocklistService.blockServerByUser(domain)
61 : this.blocklistService.blockServerByInstance(domain)
63 domains.forEach(domain => {
64 operation(domain).subscribe(
66 this.notifier.success(
67 this.mode === BlocklistComponentType.Account
68 ? $localize`Instance ${domain} muted.`
69 : $localize`Instance ${domain} muted by your instance.`
78 protected reloadDataInternal () {
79 const operation = this.mode === BlocklistComponentType.Account
80 ? this.blocklistService.getUserServerBlocklist({
81 pagination: this.pagination,
85 : this.blocklistService.getInstanceServerBlocklist({
86 pagination: this.pagination,
91 return operation.subscribe({
93 this.blockedServers = resultList.data
94 this.totalRecords = resultList.total
97 error: err => this.notifier.error(err.message)