aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+about/about-follows/about-follows.component.ts
blob: 84b47e96791ac1eab94108ed2e2582cf48260035 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { SortMeta } from 'primeng/api'
import { Component, OnInit } from '@angular/core'
import { ComponentPagination, hasMoreItems, Notifier, RestService } from '@app/core'
import { InstanceFollowService } from '@app/shared/shared-instance'

@Component({
  selector: 'my-about-follows',
  templateUrl: './about-follows.component.html',
  styleUrls: [ './about-follows.component.scss' ]
})

export class AboutFollowsComponent implements OnInit {
  followers: string[] = []
  followings: string[] = []

  loadedAllFollowers = false
  loadedAllFollowings = false

  followersPagination: ComponentPagination = {
    currentPage: 1,
    itemsPerPage: 20,
    totalItems: 0
  }

  followingsPagination: ComponentPagination = {
    currentPage: 1,
    itemsPerPage: 20,
    totalItems: 0
  }

  sort: SortMeta = {
    field: 'createdAt',
    order: -1
  }

  constructor (
    private restService: RestService,
    private notifier: Notifier,
    private followService: InstanceFollowService
  ) { }

  ngOnInit () {
    this.loadMoreFollowers()

    this.loadMoreFollowings()
  }

  loadAllFollowings () {
    if (this.loadedAllFollowings) return

    this.loadedAllFollowings = true
    this.followingsPagination.itemsPerPage = 100

    this.loadMoreFollowings(true)

    while (hasMoreItems(this.followingsPagination)) {
      this.followingsPagination.currentPage += 1

      this.loadMoreFollowings()
    }
  }

  loadAllFollowers () {
    if (this.loadedAllFollowers) return

    this.loadedAllFollowers = true
    this.followersPagination.itemsPerPage = 100

    this.loadMoreFollowers(true)

    while (hasMoreItems(this.followersPagination)) {
      this.followersPagination.currentPage += 1

      this.loadMoreFollowers()
    }
  }

  buildLink (host: string) {
    return window.location.protocol + '//' + host
  }

  canLoadMoreFollowers () {
    return this.loadedAllFollowers || this.followersPagination.totalItems > this.followersPagination.itemsPerPage
  }

  canLoadMoreFollowings () {
    return this.loadedAllFollowings || this.followingsPagination.totalItems > this.followingsPagination.itemsPerPage
  }

  private loadMoreFollowers (reset = false) {
    const pagination = this.restService.componentToRestPagination(this.followersPagination)

    this.followService.getFollowers({ pagination, sort: this.sort, state: 'accepted' })
        .subscribe({
          next: resultList => {
            if (reset) this.followers = []

            const newFollowers = resultList.data.map(r => r.follower.host)
            this.followers = this.followers.concat(newFollowers)

            this.followersPagination.totalItems = resultList.total
          },

          error: err => this.notifier.error(err.message)
        })
  }

  private loadMoreFollowings (reset = false) {
    const pagination = this.restService.componentToRestPagination(this.followingsPagination)

    this.followService.getFollowing({ pagination, sort: this.sort, state: 'accepted' })
        .subscribe({
          next: resultList => {
            if (reset) this.followings = []

            const newFollowings = resultList.data.map(r => r.following.host)
            this.followings = this.followings.concat(newFollowings)

            this.followingsPagination.totalItems = resultList.total
          },

          error: err => this.notifier.error(err.message)
        })
  }

}