]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+accounts/accounts.component.ts
Add naive aggregation from channels to account display of subscribers
[github/Chocobozzz/PeerTube.git] / client / src / app / +accounts / accounts.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { AccountService } from '@app/shared/account/account.service'
4 import { Account } from '@app/shared/account/account.model'
5 import { RestExtractor, UserService } from '@app/shared'
6 import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
7 import { Subscription } from 'rxjs'
8 import { AuthService, Notifier, RedirectService } from '@app/core'
9 import { User, UserRight } from '../../../../shared'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
12 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
13
14 @Component({
15 templateUrl: './accounts.component.html',
16 styleUrls: [ './accounts.component.scss' ]
17 })
18 export class AccountsComponent implements OnInit, OnDestroy {
19 account: Account
20 user: User
21 videoChannels: VideoChannel[] = []
22
23 private routeSub: Subscription
24
25 constructor (
26 private route: ActivatedRoute,
27 private userService: UserService,
28 private accountService: AccountService,
29 private videoChannelService: VideoChannelService,
30 private notifier: Notifier,
31 private restExtractor: RestExtractor,
32 private redirectService: RedirectService,
33 private authService: AuthService,
34 private i18n: I18n
35 ) {}
36
37 ngOnInit () {
38 this.routeSub = this.route.params
39 .pipe(
40 map(params => params[ 'accountId' ]),
41 distinctUntilChanged(),
42 switchMap(accountId => this.accountService.getAccount(accountId)),
43 tap(account => {
44 this.account = account
45 this.getUserIfNeeded(account)
46 }),
47 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
48 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
49 )
50 .subscribe(
51 videoChannels => this.videoChannels = videoChannels.data,
52
53 err => this.notifier.error(err.message)
54 )
55 }
56
57 ngOnDestroy () {
58 if (this.routeSub) this.routeSub.unsubscribe()
59 }
60
61 get naiveAggregatedSubscribers () {
62 return this.videoChannels.reduce(
63 (acc, val) => acc + val.followersCount,
64 this.account.followersCount // accumulator starts with the base number of subscribers the account has
65 )
66 }
67
68 onUserChanged () {
69 this.getUserIfNeeded(this.account)
70 }
71
72 onUserDeleted () {
73 this.redirectService.redirectToHomepage()
74 }
75
76 activateCopiedMessage () {
77 this.notifier.success(this.i18n('Username copied'))
78 }
79
80 subscribersDisplayFor (count: number) {
81 return this.i18n(`{count, plural, =1 {1 subscriber} other {${count} subscribers}}`, { count })
82 }
83
84 private getUserIfNeeded (account: Account) {
85 if (!account.userId) return
86 if (!this.authService.isLoggedIn()) return
87
88 const user = this.authService.getUser()
89 if (user.hasRight(UserRight.MANAGE_USERS)) {
90 this.userService.getUser(account.userId)
91 .subscribe(
92 user => this.user = user,
93
94 err => this.notifier.error(err.message)
95 )
96 }
97 }
98 }