]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-follows/my-followers.component.ts
Add channel filters for my videos/followers
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-follows / my-followers.component.ts
1 import { Subject } from 'rxjs'
2 import { Component, OnInit } from '@angular/core'
3 import { ActivatedRoute } from '@angular/router'
4 import { AuthService, ComponentPagination, Notifier } from '@app/core'
5 import { AdvancedInputFilter } from '@app/shared/shared-forms'
6 import { UserSubscriptionService } from '@app/shared/shared-user-subscription'
7 import { ActorFollow } from '@shared/models'
8
9 @Component({
10 templateUrl: './my-followers.component.html',
11 styleUrls: [ './my-followers.component.scss' ]
12 })
13 export class MyFollowersComponent implements OnInit {
14 follows: ActorFollow[] = []
15
16 pagination: ComponentPagination = {
17 currentPage: 1,
18 itemsPerPage: 10,
19 totalItems: null
20 }
21
22 onDataSubject = new Subject<any[]>()
23 search: string
24
25 inputFilters: AdvancedInputFilter[]
26
27 constructor (
28 private route: ActivatedRoute,
29 private auth: AuthService,
30 private userSubscriptionService: UserSubscriptionService,
31 private notifier: Notifier
32 ) {}
33
34 ngOnInit () {
35 if (this.route.snapshot.queryParams['search']) {
36 this.search = this.route.snapshot.queryParams['search']
37 }
38
39 this.auth.userInformationLoaded.subscribe(() => {
40 const channelFilters = this.auth.getUser().videoChannels.map(c => {
41 return {
42 queryParams: { search: 'channel:' + c.name },
43 label: c.name
44 }
45 })
46
47 this.inputFilters = [
48 {
49 title: $localize`Channel filters`,
50 children: channelFilters
51 }
52 ]
53 })
54 }
55
56 onNearOfBottom () {
57 // Last page
58 if (this.pagination.totalItems <= (this.pagination.currentPage * this.pagination.itemsPerPage)) return
59
60 this.pagination.currentPage += 1
61 this.loadFollowers()
62 }
63
64 onSearch (search: string) {
65 this.search = search
66 this.loadFollowers(false)
67 }
68
69 isFollowingAccount (follow: ActorFollow) {
70 return follow.following.name === this.getUsername()
71 }
72
73 private loadFollowers (more = true) {
74 this.userSubscriptionService.listFollowers({
75 pagination: this.pagination,
76 nameWithHost: this.getUsername(),
77 search: this.search
78 }).subscribe({
79 next: res => {
80 this.follows = more
81 ? this.follows.concat(res.data)
82 : res.data
83 this.pagination.totalItems = res.total
84
85 this.onDataSubject.next(res.data)
86 },
87
88 error: err => this.notifier.error(err.message)
89 })
90 }
91
92 private getUsername () {
93 return this.auth.getUser().username
94 }
95 }