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, first, map, switchMap, tap } from 'rxjs/operators'
7 import { forkJoin, 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'
15 templateUrl: './accounts.component.html',
16 styleUrls: [ './accounts.component.scss' ]
18 export class AccountsComponent implements OnInit, OnDestroy {
21 videoChannels: VideoChannel[] = []
23 isAccountManageable = false
24 accountFollowerTitle = ''
26 private routeSub: Subscription
29 private route: ActivatedRoute,
30 private userService: UserService,
31 private accountService: AccountService,
32 private videoChannelService: VideoChannelService,
33 private notifier: Notifier,
34 private restExtractor: RestExtractor,
35 private redirectService: RedirectService,
36 private authService: AuthService,
41 this.routeSub = this.route.params
43 map(params => params[ 'accountId' ]),
44 distinctUntilChanged(),
45 switchMap(accountId => this.accountService.getAccount(accountId)),
47 this.account = account
49 this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id
51 this.accountFollowerTitle = this.i18n(
52 '{{followers}} direct account followers',
53 { followers: this.subscribersDisplayFor(account.followersCount) }
56 this.getUserIfNeeded(account)
58 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
59 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
62 videoChannels => this.videoChannels = videoChannels.data,
64 err => this.notifier.error(err.message)
69 if (this.routeSub) this.routeSub.unsubscribe()
72 get naiveAggregatedSubscribers () {
73 return this.videoChannels.reduce(
74 (acc, val) => acc + val.followersCount,
75 this.account.followersCount // accumulator starts with the base number of subscribers the account has
80 this.getUserIfNeeded(this.account)
84 this.redirectService.redirectToHomepage()
87 activateCopiedMessage () {
88 this.notifier.success(this.i18n('Username copied'))
91 subscribersDisplayFor (count: number) {
92 return this.i18n(`{count, plural, =1 {1 subscriber} other {${count} subscribers}}`, { count })
95 private getUserIfNeeded (account: Account) {
96 if (!account.userId || !this.authService.isLoggedIn()) return
98 const user = this.authService.getUser()
99 if (user.hasRight(UserRight.MANAGE_USERS)) {
101 this.userService.getUser(account.userId),
102 this.authService.userInformationLoaded.pipe(first())
104 ([ accountUser ]) => this.accountUser = accountUser,
106 err => this.notifier.error(err.message)