]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/+about/about-follows/about-follows.component.ts
Fix video right check
[github/Chocobozzz/PeerTube.git] / client / src / app / +about / about-follows / about-follows.component.ts
index d6030792864892728ca805721f67165e94c4c014..84b47e96791ac1eab94108ed2e2582cf48260035 100644 (file)
@@ -1,10 +1,7 @@
-import { Component, OnInit } from '@angular/core'
-import { FollowService } from '@app/shared/instance/follow.service'
-import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
-import { Notifier } from '@app/core'
-import { RestService } from '@app/shared'
 import { SortMeta } from 'primeng/api'
-import { Subject } from 'rxjs'
+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',
@@ -16,16 +13,19 @@ export class AboutFollowsComponent implements OnInit {
   followers: string[] = []
   followings: string[] = []
 
+  loadedAllFollowers = false
+  loadedAllFollowings = false
+
   followersPagination: ComponentPagination = {
     currentPage: 1,
     itemsPerPage: 20,
-    totalItems: null
+    totalItems: 0
   }
 
   followingsPagination: ComponentPagination = {
     currentPage: 1,
     itemsPerPage: 20,
-    totalItems: null
+    totalItems: 0
   }
 
   sort: SortMeta = {
@@ -33,12 +33,10 @@ export class AboutFollowsComponent implements OnInit {
     order: -1
   }
 
-  onDataSubject = new Subject<any[]>()
-
   constructor (
     private restService: RestService,
     private notifier: Notifier,
-    private followService: FollowService
+    private followService: InstanceFollowService
   ) { }
 
   ngOnInit () {
@@ -47,64 +45,82 @@ export class AboutFollowsComponent implements OnInit {
     this.loadMoreFollowings()
   }
 
-  onNearOfBottom () {
-    this.onNearOfFollowersBottom()
+  loadAllFollowings () {
+    if (this.loadedAllFollowings) return
 
-    this.onNearOfFollowingsBottom()
-  }
+    this.loadedAllFollowings = true
+    this.followingsPagination.itemsPerPage = 100
 
-  onNearOfFollowersBottom () {
-    if (!hasMoreItems(this.followersPagination)) return
+    this.loadMoreFollowings(true)
 
-    this.followersPagination.currentPage += 1
-    this.loadMoreFollowers()
+    while (hasMoreItems(this.followingsPagination)) {
+      this.followingsPagination.currentPage += 1
+
+      this.loadMoreFollowings()
+    }
   }
 
-  onNearOfFollowingsBottom () {
-    if (!hasMoreItems(this.followingsPagination)) return
+  loadAllFollowers () {
+    if (this.loadedAllFollowers) return
 
-    this.followingsPagination.currentPage += 1
-    this.loadMoreFollowings()
+    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
   }
 
-  private loadMoreFollowers () {
-    const pagination = this.restService.componentPaginationToRestPagination(this.followersPagination)
+  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 = []
 
-    this.followService.getFollowers(pagination, this.sort)
-        .subscribe(
-          resultList => {
             const newFollowers = resultList.data.map(r => r.follower.host)
             this.followers = this.followers.concat(newFollowers)
 
             this.followersPagination.totalItems = resultList.total
-
-            this.onDataSubject.next(newFollowers)
           },
 
-          err => this.notifier.error(err.message)
-        )
+          error: err => this.notifier.error(err.message)
+        })
   }
 
-  private loadMoreFollowings () {
-    const pagination = this.restService.componentPaginationToRestPagination(this.followingsPagination)
+  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 = []
 
-    this.followService.getFollowing(pagination, this.sort)
-        .subscribe(
-          resultList => {
             const newFollowings = resultList.data.map(r => r.following.host)
             this.followings = this.followings.concat(newFollowings)
 
             this.followingsPagination.totalItems = resultList.total
-
-            this.onDataSubject.next(newFollowings)
           },
 
-          err => this.notifier.error(err.message)
-        )
+          error: err => this.notifier.error(err.message)
+        })
   }
 
 }