]>
Commit | Line | Data |
---|---|---|
a6dbbf03 | 1 | import { SortMeta } from 'primeng/api' |
67ed6552 | 2 | import { Component, OnInit } from '@angular/core' |
1a6304ce | 3 | import { ComponentPagination, hasMoreItems, Notifier, RestService, ServerService } from '@app/core' |
67ed6552 | 4 | import { InstanceFollowService } from '@app/shared/shared-instance' |
1a6304ce | 5 | import { Actor } from '@shared/models/actors' |
a6dbbf03 C |
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 { | |
1a6304ce C |
14 | instanceName: string |
15 | ||
a6dbbf03 C |
16 | followers: string[] = [] |
17 | followings: string[] = [] | |
18 | ||
0d7c7314 C |
19 | loadedAllFollowers = false |
20 | loadedAllFollowings = false | |
49c4dd7e | 21 | |
a6dbbf03 C |
22 | followersPagination: ComponentPagination = { |
23 | currentPage: 1, | |
ad453580 | 24 | itemsPerPage: 20, |
ed8b4014 | 25 | totalItems: 0 |
a6dbbf03 C |
26 | } |
27 | ||
28 | followingsPagination: ComponentPagination = { | |
29 | currentPage: 1, | |
ad453580 | 30 | itemsPerPage: 20, |
ed8b4014 | 31 | totalItems: 0 |
a6dbbf03 C |
32 | } |
33 | ||
34 | sort: SortMeta = { | |
35 | field: 'createdAt', | |
36 | order: -1 | |
37 | } | |
38 | ||
39 | constructor ( | |
1a6304ce | 40 | private server: ServerService, |
a6dbbf03 C |
41 | private restService: RestService, |
42 | private notifier: Notifier, | |
67ed6552 | 43 | private followService: InstanceFollowService |
a6dbbf03 C |
44 | ) { } |
45 | ||
46 | ngOnInit () { | |
47 | this.loadMoreFollowers() | |
48 | ||
49 | this.loadMoreFollowings() | |
1a6304ce C |
50 | |
51 | this.instanceName = this.server.getHTMLConfig().instance.name | |
a6dbbf03 C |
52 | } |
53 | ||
49c4dd7e | 54 | loadAllFollowings () { |
0d7c7314 C |
55 | if (this.loadedAllFollowings) return |
56 | ||
57 | this.loadedAllFollowings = true | |
58 | this.followingsPagination.itemsPerPage = 100 | |
59 | ||
60 | this.loadMoreFollowings(true) | |
61 | ||
bd49f8e9 | 62 | while (hasMoreItems(this.followingsPagination)) { |
49c4dd7e | 63 | this.followingsPagination.currentPage += 1 |
a6dbbf03 | 64 | |
49c4dd7e M |
65 | this.loadMoreFollowings() |
66 | } | |
a6dbbf03 C |
67 | } |
68 | ||
49c4dd7e | 69 | loadAllFollowers () { |
0d7c7314 C |
70 | if (this.loadedAllFollowers) return |
71 | ||
72 | this.loadedAllFollowers = true | |
73 | this.followersPagination.itemsPerPage = 100 | |
74 | ||
75 | this.loadMoreFollowers(true) | |
76 | ||
bd49f8e9 | 77 | while (hasMoreItems(this.followersPagination)) { |
49c4dd7e | 78 | this.followersPagination.currentPage += 1 |
bd49f8e9 M |
79 | |
80 | this.loadMoreFollowers() | |
49c4dd7e | 81 | } |
a6dbbf03 C |
82 | } |
83 | ||
84 | buildLink (host: string) { | |
85 | return window.location.protocol + '//' + host | |
86 | } | |
87 | ||
0d7c7314 C |
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) { | |
4beda9e1 | 97 | const pagination = this.restService.componentToRestPagination(this.followersPagination) |
a6dbbf03 | 98 | |
9df52d66 | 99 | this.followService.getFollowers({ pagination, sort: this.sort, state: 'accepted' }) |
1378c0d3 C |
100 | .subscribe({ |
101 | next: resultList => { | |
0d7c7314 | 102 | if (reset) this.followers = [] |
2ce94945 | 103 | |
1a6304ce | 104 | const newFollowers = resultList.data.map(r => this.formatFollow(r.follower)) |
0d7c7314 | 105 | this.followers = this.followers.concat(newFollowers) |
a6dbbf03 C |
106 | |
107 | this.followersPagination.totalItems = resultList.total | |
108 | }, | |
109 | ||
1378c0d3 C |
110 | error: err => this.notifier.error(err.message) |
111 | }) | |
a6dbbf03 C |
112 | } |
113 | ||
0d7c7314 | 114 | private loadMoreFollowings (reset = false) { |
4beda9e1 | 115 | const pagination = this.restService.componentToRestPagination(this.followingsPagination) |
a6dbbf03 | 116 | |
b8f4167f | 117 | this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' }) |
1378c0d3 C |
118 | .subscribe({ |
119 | next: resultList => { | |
0d7c7314 | 120 | if (reset) this.followings = [] |
2ce94945 | 121 | |
1a6304ce | 122 | const newFollowings = resultList.data.map(r => this.formatFollow(r.following)) |
0d7c7314 | 123 | this.followings = this.followings.concat(newFollowings) |
a6dbbf03 C |
124 | |
125 | this.followingsPagination.totalItems = resultList.total | |
126 | }, | |
127 | ||
1378c0d3 C |
128 | error: err => this.notifier.error(err.message) |
129 | }) | |
a6dbbf03 C |
130 | } |
131 | ||
1a6304ce C |
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 | ||
a6dbbf03 | 139 | } |