]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+about/about-follows/about-follows.component.ts
Improve instance follow display
[github/Chocobozzz/PeerTube.git] / client / src / app / +about / about-follows / about-follows.component.ts
1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit } from '@angular/core'
3 import { ComponentPagination, hasMoreItems, Notifier, RestService, ServerService } from '@app/core'
4 import { InstanceFollowService } from '@app/shared/shared-instance'
5 import { Actor } from '@shared/models/actors'
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 instanceName: string
15
16 followers: string[] = []
17 followings: string[] = []
18
19 loadedAllFollowers = false
20 loadedAllFollowings = false
21
22 followersPagination: ComponentPagination = {
23 currentPage: 1,
24 itemsPerPage: 20,
25 totalItems: 0
26 }
27
28 followingsPagination: ComponentPagination = {
29 currentPage: 1,
30 itemsPerPage: 20,
31 totalItems: 0
32 }
33
34 sort: SortMeta = {
35 field: 'createdAt',
36 order: -1
37 }
38
39 constructor (
40 private server: ServerService,
41 private restService: RestService,
42 private notifier: Notifier,
43 private followService: InstanceFollowService
44 ) { }
45
46 ngOnInit () {
47 this.loadMoreFollowers()
48
49 this.loadMoreFollowings()
50
51 this.instanceName = this.server.getHTMLConfig().instance.name
52 }
53
54 loadAllFollowings () {
55 if (this.loadedAllFollowings) return
56
57 this.loadedAllFollowings = true
58 this.followingsPagination.itemsPerPage = 100
59
60 this.loadMoreFollowings(true)
61
62 while (hasMoreItems(this.followingsPagination)) {
63 this.followingsPagination.currentPage += 1
64
65 this.loadMoreFollowings()
66 }
67 }
68
69 loadAllFollowers () {
70 if (this.loadedAllFollowers) return
71
72 this.loadedAllFollowers = true
73 this.followersPagination.itemsPerPage = 100
74
75 this.loadMoreFollowers(true)
76
77 while (hasMoreItems(this.followersPagination)) {
78 this.followersPagination.currentPage += 1
79
80 this.loadMoreFollowers()
81 }
82 }
83
84 buildLink (host: string) {
85 return window.location.protocol + '//' + host
86 }
87
88 canLoadMoreFollowers () {
89 return this.loadedAllFollowers || this.followersPagination.totalItems > this.followersPagination.itemsPerPage
90 }
91
92 canLoadMoreFollowings () {
93 return this.loadedAllFollowings || this.followingsPagination.totalItems > this.followingsPagination.itemsPerPage
94 }
95
96 private loadMoreFollowers (reset = false) {
97 const pagination = this.restService.componentToRestPagination(this.followersPagination)
98
99 this.followService.getFollowers({ pagination, sort: this.sort, state: 'accepted' })
100 .subscribe({
101 next: resultList => {
102 if (reset) this.followers = []
103
104 const newFollowers = resultList.data.map(r => this.formatFollow(r.follower))
105 this.followers = this.followers.concat(newFollowers)
106
107 this.followersPagination.totalItems = resultList.total
108 },
109
110 error: err => this.notifier.error(err.message)
111 })
112 }
113
114 private loadMoreFollowings (reset = false) {
115 const pagination = this.restService.componentToRestPagination(this.followingsPagination)
116
117 this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' })
118 .subscribe({
119 next: resultList => {
120 if (reset) this.followings = []
121
122 const newFollowings = resultList.data.map(r => this.formatFollow(r.following))
123 this.followings = this.followings.concat(newFollowings)
124
125 this.followingsPagination.totalItems = resultList.total
126 },
127
128 error: err => this.notifier.error(err.message)
129 })
130 }
131
132 private formatFollow (actor: Actor) {
133 // Instance follow, only display host
134 if (actor.name === 'peertube') return actor.host
135
136 return actor.name + '@' + actor.host
137 }
138
139 }