]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+about/about-follows/about-follows.component.ts
fix merge conflicts
[github/Chocobozzz/PeerTube.git] / client / src / app / +about / about-follows / about-follows.component.ts
1 import { SortMeta } from 'primeng/api'
2 import { Subject } from 'rxjs'
3 import { Component, OnInit } from '@angular/core'
4 import { ComponentPagination, hasMoreItems, Notifier, RestService } from '@app/core'
5 import { InstanceFollowService } from '@app/shared/shared-instance'
6
7 @Component({
8 selector: 'my-about-follows',
9 templateUrl: './about-follows.component.html',
10 styleUrls: [ './about-follows.component.scss' ]
11 })
12
13 export class AboutFollowsComponent implements OnInit {
14 followers: string[] = []
15 followings: string[] = []
16 moreFollowers: string[] = []
17 moreFollowings: string[] = []
18
19 showMoreFollowers = false
20 showMoreFollowings = false
21
22 followersPagination: ComponentPagination = {
23 currentPage: 1,
24 itemsPerPage: 20,
25 totalItems: null
26 }
27
28 followingsPagination: ComponentPagination = {
29 currentPage: 1,
30 itemsPerPage: 20,
31 totalItems: null
32 }
33
34 sort: SortMeta = {
35 field: 'createdAt',
36 order: -1
37 }
38
39 onDataSubject = new Subject<any[]>()
40
41 constructor (
42 private restService: RestService,
43 private notifier: Notifier,
44 private followService: InstanceFollowService
45 ) { }
46
47 ngOnInit () {
48 this.loadMoreFollowers()
49
50 this.loadMoreFollowings()
51 }
52
53 loadAllFollowings () {
54 while (hasMoreItems(this.followingsPagination)) {
55 this.followingsPagination.currentPage += 1
56
57 this.loadMoreFollowings()
58 }
59 }
60
61 loadAllFollowers () {
62 while (hasMoreItems(this.followersPagination)) {
63 this.followersPagination.currentPage += 1
64
65 this.loadMoreFollowers()
66 }
67 }
68
69 buildLink (host: string) {
70 return window.location.protocol + '//' + host
71 }
72
73 private loadMoreFollowers () {
74 const pagination = this.restService.componentPaginationToRestPagination(this.followersPagination)
75
76 this.followService.getFollowers({ pagination: pagination, sort: this.sort, state: 'accepted' })
77 .subscribe(
78 resultList => {
79 const newFollowers = resultList.data.map(r => r.follower.host)
80 if (this.followers.length === 0) this.followers = this.followers.concat(newFollowers)
81
82 else this.moreFollowers = this.moreFollowers.concat(newFollowers)
83
84 this.followersPagination.totalItems = resultList.total
85
86 this.onDataSubject.next(newFollowers)
87 },
88
89 err => this.notifier.error(err.message)
90 )
91 }
92
93 private loadMoreFollowings () {
94 const pagination = this.restService.componentPaginationToRestPagination(this.followingsPagination)
95
96 this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' })
97 .subscribe(
98 resultList => {
99 const newFollowings = resultList.data.map(r => r.following.host)
100 if (this.followings.length === 0) this.followings = this.followings.concat(newFollowings)
101
102 else this.moreFollowings = this.moreFollowings.concat(newFollowings)
103
104 this.followingsPagination.totalItems = resultList.total
105
106 this.onDataSubject.next(newFollowings)
107 },
108
109 err => this.notifier.error(err.message)
110 )
111 }
112
113 }