Factorize rest-table and fix/simplify SQL
[oweals/peertube.git] / client / src / app / +admin / moderation / instance-blocklist / instance-account-blocklist.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { I18n } from '@ngx-translate/i18n-polyfill'
4 import { RestPagination, RestTable } from '@app/shared'
5 import { SortMeta } from 'primeng/api'
6 import { AccountBlock, BlocklistService } from '@app/shared/blocklist'
7 import { Actor } from '@app/shared/actor/actor.model'
8
9 @Component({
10   selector: 'my-instance-account-blocklist',
11   styleUrls: [ '../moderation.component.scss', './instance-account-blocklist.component.scss' ],
12   templateUrl: './instance-account-blocklist.component.html'
13 })
14 export class InstanceAccountBlocklistComponent extends RestTable implements OnInit {
15   blockedAccounts: AccountBlock[] = []
16   totalRecords = 0
17   sort: SortMeta = { field: 'createdAt', order: -1 }
18   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
19
20   constructor (
21     private notifier: Notifier,
22     private blocklistService: BlocklistService,
23     private i18n: I18n
24   ) {
25     super()
26   }
27
28   ngOnInit () {
29     this.initialize()
30   }
31
32   getIdentifier () {
33     return 'InstanceAccountBlocklistComponent'
34   }
35
36   switchToDefaultAvatar ($event: Event) {
37     ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
38   }
39
40   unblockAccount (accountBlock: AccountBlock) {
41     const blockedAccount = accountBlock.blockedAccount
42
43     this.blocklistService.unblockAccountByInstance(blockedAccount)
44         .subscribe(
45           () => {
46             this.notifier.success(
47               this.i18n('Account {{nameWithHost}} unmuted by your instance.', { nameWithHost: blockedAccount.nameWithHost })
48             )
49
50             this.loadData()
51           }
52         )
53   }
54
55   protected loadData () {
56     return this.blocklistService.getInstanceAccountBlocklist({
57       pagination: this.pagination,
58       sort: this.sort,
59       search: this.search
60     })
61       .subscribe(
62         resultList => {
63           this.blockedAccounts = resultList.data
64           this.totalRecords = resultList.total
65         },
66
67         err => this.notifier.error(err.message)
68       )
69   }
70 }