]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { SortMeta } from 'primeng/api' |
22839330 | 2 | import { OnInit, ViewChild } from '@angular/core' |
67ed6552 C |
3 | import { BatchDomainsModalComponent } from '@app/shared/shared-moderation/batch-domains-modal.component' |
4 | import { Notifier, RestPagination, RestTable } from '@app/core' | |
22839330 | 5 | import { I18n } from '@ngx-translate/i18n-polyfill' |
67ed6552 C |
6 | import { ServerBlock } from '@shared/models' |
7 | import { BlocklistComponentType, BlocklistService } from './blocklist.service' | |
22839330 RK |
8 | |
9 | export class GenericServerBlocklistComponent extends RestTable implements OnInit { | |
10 | @ViewChild('batchDomainsModal') batchDomainsModal: BatchDomainsModalComponent | |
11 | ||
12 | // @ts-ignore: "Abstract methods can only appear within an abstract class" | |
13 | public abstract mode: BlocklistComponentType | |
14 | ||
15 | blockedServers: ServerBlock[] = [] | |
16 | totalRecords = 0 | |
17 | sort: SortMeta = { field: 'createdAt', order: -1 } | |
18 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } | |
19 | ||
20 | constructor ( | |
21 | protected notifier: Notifier, | |
22 | protected blocklistService: BlocklistService, | |
23 | protected i18n: I18n | |
24 | ) { | |
25 | super() | |
26 | } | |
27 | ||
67ed6552 C |
28 | // @ts-ignore: "Abstract methods can only appear within an abstract class" |
29 | public abstract getIdentifier (): string | |
30 | ||
22839330 RK |
31 | ngOnInit () { |
32 | this.initialize() | |
33 | } | |
34 | ||
22839330 RK |
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 | |
40 | ||
41 | operation(host).subscribe( | |
42 | () => { | |
43 | this.notifier.success( | |
44 | this.mode === BlocklistComponentType.Account | |
45 | ? this.i18n('Instance {{host}} unmuted.', { host }) | |
46 | : this.i18n('Instance {{host}} unmuted by your instance.', { host }) | |
47 | ) | |
48 | ||
49 | this.loadData() | |
50 | } | |
51 | ) | |
52 | } | |
53 | ||
54 | addServersToBlock () { | |
55 | this.batchDomainsModal.openModal() | |
56 | } | |
57 | ||
58 | onDomainsToBlock (domains: string[]) { | |
59 | const operation = (domain: string) => this.mode === BlocklistComponentType.Account | |
60 | ? this.blocklistService.blockServerByUser(domain) | |
61 | : this.blocklistService.blockServerByInstance(domain) | |
62 | ||
63 | domains.forEach(domain => { | |
64 | operation(domain).subscribe( | |
65 | () => { | |
66 | this.notifier.success( | |
67 | this.mode === BlocklistComponentType.Account | |
68 | ? this.i18n('Instance {{domain}} muted.', { domain }) | |
69 | : this.i18n('Instance {{domain}} muted by your instance.', { domain }) | |
70 | ) | |
71 | ||
72 | this.loadData() | |
73 | } | |
74 | ) | |
75 | }) | |
76 | } | |
77 | ||
78 | protected loadData () { | |
79 | const operation = this.mode === BlocklistComponentType.Account | |
80 | ? this.blocklistService.getUserServerBlocklist({ | |
81 | pagination: this.pagination, | |
82 | sort: this.sort, | |
83 | search: this.search | |
84 | }) | |
85 | : this.blocklistService.getInstanceServerBlocklist({ | |
86 | pagination: this.pagination, | |
87 | sort: this.sort, | |
88 | search: this.search | |
89 | }) | |
90 | ||
91 | return operation.subscribe( | |
92 | resultList => { | |
93 | this.blockedServers = resultList.data | |
94 | this.totalRecords = resultList.total | |
95 | }, | |
96 | ||
97 | err => this.notifier.error(err.message) | |
98 | ) | |
99 | } | |
100 | } |