Factorize rest-table and fix/simplify SQL
[oweals/peertube.git] / client / src / app / +admin / follows / following-list / following-list.component.ts
1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { SortMeta } from 'primeng/api'
4 import { ActorFollow } from '../../../../../../shared/models/actors/follow.model'
5 import { ConfirmService } from '../../../core/confirm/confirm.service'
6 import { RestPagination, RestTable } from '../../../shared'
7 import { FollowService } from '@app/shared/instance/follow.service'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { BatchDomainsModalComponent } from '@app/+admin/config/shared/batch-domains-modal.component'
10
11 @Component({
12   selector: 'my-followers-list',
13   templateUrl: './following-list.component.html',
14   styleUrls: [ '../follows.component.scss', './following-list.component.scss' ]
15 })
16 export class FollowingListComponent extends RestTable implements OnInit {
17   @ViewChild('batchDomainsModal') batchDomainsModal: BatchDomainsModalComponent
18
19   following: ActorFollow[] = []
20   totalRecords = 0
21   sort: SortMeta = { field: 'createdAt', order: -1 }
22   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
23
24   constructor (
25     private notifier: Notifier,
26     private confirmService: ConfirmService,
27     private followService: FollowService,
28     private i18n: I18n
29   ) {
30     super()
31   }
32
33   ngOnInit () {
34     this.initialize()
35   }
36
37   getIdentifier () {
38     return 'FollowingListComponent'
39   }
40
41   addDomainsToFollow () {
42     this.batchDomainsModal.openModal()
43   }
44
45   httpEnabled () {
46     return window.location.protocol === 'https:'
47   }
48
49   async addFollowing (hosts: string[]) {
50     this.followService.follow(hosts).subscribe(
51       () => {
52         this.notifier.success(this.i18n('Follow request(s) sent!'))
53         this.loadData()
54       },
55
56       err => this.notifier.error(err.message)
57     )
58   }
59
60   async removeFollowing (follow: ActorFollow) {
61     const res = await this.confirmService.confirm(
62       this.i18n('Do you really want to unfollow {{host}}?', { host: follow.following.host }),
63       this.i18n('Unfollow')
64     )
65     if (res === false) return
66
67     this.followService.unfollow(follow).subscribe(
68       () => {
69         this.notifier.success(this.i18n('You are not following {{host}} anymore.', { host: follow.following.host }))
70         this.loadData()
71       },
72
73       err => this.notifier.error(err.message)
74     )
75   }
76
77   protected loadData () {
78     this.followService.getFollowing({ pagination: this.pagination, sort: this.sort, search: this.search })
79                       .subscribe(
80                         resultList => {
81                           this.following = resultList.data
82                           this.totalRecords = resultList.total
83                         },
84
85                         err => this.notifier.error(err.message)
86                       )
87   }
88 }