2019-06-06 11:39:22 +02:00
|
|
|
import { SortMeta } from 'primeng/api'
|
2020-06-23 14:10:17 +02:00
|
|
|
import { Component, OnInit } from '@angular/core'
|
|
|
|
import { ComponentPagination, hasMoreItems, Notifier, RestService } from '@app/core'
|
|
|
|
import { InstanceFollowService } from '@app/shared/shared-instance'
|
2019-06-06 11:39:22 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-about-follows',
|
|
|
|
templateUrl: './about-follows.component.html',
|
|
|
|
styleUrls: [ './about-follows.component.scss' ]
|
|
|
|
})
|
|
|
|
|
|
|
|
export class AboutFollowsComponent implements OnInit {
|
|
|
|
followers: string[] = []
|
|
|
|
followings: string[] = []
|
|
|
|
|
2020-12-14 10:23:24 +01:00
|
|
|
loadedAllFollowers = false
|
|
|
|
loadedAllFollowings = false
|
2020-04-27 12:18:24 +02:00
|
|
|
|
2019-06-06 11:39:22 +02:00
|
|
|
followersPagination: ComponentPagination = {
|
|
|
|
currentPage: 1,
|
2019-08-02 14:49:25 +02:00
|
|
|
itemsPerPage: 20,
|
2020-12-12 22:19:01 +01:00
|
|
|
totalItems: 0
|
2019-06-06 11:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
followingsPagination: ComponentPagination = {
|
|
|
|
currentPage: 1,
|
2019-08-02 14:49:25 +02:00
|
|
|
itemsPerPage: 20,
|
2020-12-12 22:19:01 +01:00
|
|
|
totalItems: 0
|
2019-06-06 11:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sort: SortMeta = {
|
|
|
|
field: 'createdAt',
|
|
|
|
order: -1
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor (
|
|
|
|
private restService: RestService,
|
|
|
|
private notifier: Notifier,
|
2020-06-23 14:10:17 +02:00
|
|
|
private followService: InstanceFollowService
|
2019-06-06 11:39:22 +02:00
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit () {
|
|
|
|
this.loadMoreFollowers()
|
|
|
|
|
|
|
|
this.loadMoreFollowings()
|
|
|
|
}
|
|
|
|
|
2020-04-27 12:18:24 +02:00
|
|
|
loadAllFollowings () {
|
2020-12-14 10:23:24 +01:00
|
|
|
if (this.loadedAllFollowings) return
|
|
|
|
|
|
|
|
this.loadedAllFollowings = true
|
|
|
|
this.followingsPagination.itemsPerPage = 100
|
|
|
|
|
|
|
|
this.loadMoreFollowings(true)
|
|
|
|
|
2020-04-27 13:37:18 +02:00
|
|
|
while (hasMoreItems(this.followingsPagination)) {
|
2020-04-27 12:18:24 +02:00
|
|
|
this.followingsPagination.currentPage += 1
|
2019-06-06 11:39:22 +02:00
|
|
|
|
2020-04-27 12:18:24 +02:00
|
|
|
this.loadMoreFollowings()
|
|
|
|
}
|
2019-06-06 11:39:22 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 12:18:24 +02:00
|
|
|
loadAllFollowers () {
|
2020-12-14 10:23:24 +01:00
|
|
|
if (this.loadedAllFollowers) return
|
|
|
|
|
|
|
|
this.loadedAllFollowers = true
|
|
|
|
this.followersPagination.itemsPerPage = 100
|
|
|
|
|
|
|
|
this.loadMoreFollowers(true)
|
|
|
|
|
2020-04-27 13:37:18 +02:00
|
|
|
while (hasMoreItems(this.followersPagination)) {
|
2020-04-27 12:18:24 +02:00
|
|
|
this.followersPagination.currentPage += 1
|
2020-04-27 13:37:18 +02:00
|
|
|
|
|
|
|
this.loadMoreFollowers()
|
2020-04-27 12:18:24 +02:00
|
|
|
}
|
2019-06-06 11:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buildLink (host: string) {
|
|
|
|
return window.location.protocol + '//' + host
|
|
|
|
}
|
|
|
|
|
2020-12-14 10:23:24 +01:00
|
|
|
canLoadMoreFollowers () {
|
|
|
|
return this.loadedAllFollowers || this.followersPagination.totalItems > this.followersPagination.itemsPerPage
|
|
|
|
}
|
|
|
|
|
|
|
|
canLoadMoreFollowings () {
|
|
|
|
return this.loadedAllFollowings || this.followingsPagination.totalItems > this.followingsPagination.itemsPerPage
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadMoreFollowers (reset = false) {
|
2019-06-06 11:39:22 +02:00
|
|
|
const pagination = this.restService.componentPaginationToRestPagination(this.followersPagination)
|
|
|
|
|
2021-08-17 14:42:53 +02:00
|
|
|
this.followService.getFollowers({ pagination, sort: this.sort, state: 'accepted' })
|
2021-08-17 11:27:47 +02:00
|
|
|
.subscribe({
|
|
|
|
next: resultList => {
|
2020-12-14 10:23:24 +01:00
|
|
|
if (reset) this.followers = []
|
2020-04-27 21:14:40 +02:00
|
|
|
|
2020-12-14 10:23:24 +01:00
|
|
|
const newFollowers = resultList.data.map(r => r.follower.host)
|
|
|
|
this.followers = this.followers.concat(newFollowers)
|
2019-06-06 11:39:22 +02:00
|
|
|
|
|
|
|
this.followersPagination.totalItems = resultList.total
|
|
|
|
},
|
|
|
|
|
2021-08-17 11:27:47 +02:00
|
|
|
error: err => this.notifier.error(err.message)
|
|
|
|
})
|
2019-06-06 11:39:22 +02:00
|
|
|
}
|
|
|
|
|
2020-12-14 10:23:24 +01:00
|
|
|
private loadMoreFollowings (reset = false) {
|
2019-06-06 11:39:22 +02:00
|
|
|
const pagination = this.restService.componentPaginationToRestPagination(this.followingsPagination)
|
|
|
|
|
2019-11-28 11:37:32 +01:00
|
|
|
this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' })
|
2021-08-17 11:27:47 +02:00
|
|
|
.subscribe({
|
|
|
|
next: resultList => {
|
2020-12-14 10:23:24 +01:00
|
|
|
if (reset) this.followings = []
|
2020-04-27 21:14:40 +02:00
|
|
|
|
2020-12-14 10:23:24 +01:00
|
|
|
const newFollowings = resultList.data.map(r => r.following.host)
|
|
|
|
this.followings = this.followings.concat(newFollowings)
|
2019-06-06 11:39:22 +02:00
|
|
|
|
|
|
|
this.followingsPagination.totalItems = resultList.total
|
|
|
|
},
|
|
|
|
|
2021-08-17 11:27:47 +02:00
|
|
|
error: err => this.notifier.error(err.message)
|
|
|
|
})
|
2019-06-06 11:39:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|