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'
8 selector: 'my-about-follows',
9 templateUrl: './about-follows.component.html',
10 styleUrls: [ './about-follows.component.scss' ]
13 export class AboutFollowsComponent implements OnInit {
16 followers: string[] = []
17 followings: string[] = []
19 loadedAllFollowers = false
20 loadedAllFollowings = false
22 followersPagination: ComponentPagination = {
28 followingsPagination: ComponentPagination = {
40 private server: ServerService,
41 private restService: RestService,
42 private notifier: Notifier,
43 private followService: InstanceFollowService
47 this.loadMoreFollowers()
49 this.loadMoreFollowings()
51 this.instanceName = this.server.getHTMLConfig().instance.name
54 loadAllFollowings () {
55 if (this.loadedAllFollowings) return
57 this.loadedAllFollowings = true
58 this.followingsPagination.itemsPerPage = 100
60 this.loadMoreFollowings(true)
62 while (hasMoreItems(this.followingsPagination)) {
63 this.followingsPagination.currentPage += 1
65 this.loadMoreFollowings()
70 if (this.loadedAllFollowers) return
72 this.loadedAllFollowers = true
73 this.followersPagination.itemsPerPage = 100
75 this.loadMoreFollowers(true)
77 while (hasMoreItems(this.followersPagination)) {
78 this.followersPagination.currentPage += 1
80 this.loadMoreFollowers()
84 buildLink (host: string) {
85 return window.location.protocol + '//' + host
88 canLoadMoreFollowers () {
89 return this.loadedAllFollowers || this.followersPagination.totalItems > this.followersPagination.itemsPerPage
92 canLoadMoreFollowings () {
93 return this.loadedAllFollowings || this.followingsPagination.totalItems > this.followingsPagination.itemsPerPage
96 private loadMoreFollowers (reset = false) {
97 const pagination = this.restService.componentToRestPagination(this.followersPagination)
99 this.followService.getFollowers({ pagination, sort: this.sort, state: 'accepted' })
101 next: resultList => {
102 if (reset) this.followers = []
104 const newFollowers = resultList.data.map(r => this.formatFollow(r.follower))
105 this.followers = this.followers.concat(newFollowers)
107 this.followersPagination.totalItems = resultList.total
110 error: err => this.notifier.error(err.message)
114 private loadMoreFollowings (reset = false) {
115 const pagination = this.restService.componentToRestPagination(this.followingsPagination)
117 this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' })
119 next: resultList => {
120 if (reset) this.followings = []
122 const newFollowings = resultList.data.map(r => this.formatFollow(r.following))
123 this.followings = this.followings.concat(newFollowings)
125 this.followingsPagination.totalItems = resultList.total
128 error: err => this.notifier.error(err.message)
132 private formatFollow (actor: Actor) {
133 // Instance follow, only display host
134 if (actor.name === 'peertube') return actor.host
136 return actor.name + '@' + actor.host