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'
15 templateUrl: './accounts.component.html',
16 styleUrls: [ './accounts.component.scss' ]
18 export class AccountsComponent implements OnInit, OnDestroy {
21 videoChannels: VideoChannel[] = []
23 private routeSub: Subscription
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,
38 this.routeSub = this.route.params
40 map(params => params[ 'accountId' ]),
41 distinctUntilChanged(),
42 switchMap(accountId => this.accountService.getAccount(accountId)),
44 this.account = account
45 this.getUserIfNeeded(account)
47 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
48 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
51 videoChannels => this.videoChannels = videoChannels.data,
53 err => this.notifier.error(err.message)
58 if (this.routeSub) this.routeSub.unsubscribe()
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
69 this.getUserIfNeeded(this.account)
73 this.redirectService.redirectToHomepage()
76 activateCopiedMessage () {
77 this.notifier.success(this.i18n('Username copied'))
80 subscribersDisplayFor (count: number) {
81 return this.i18n(`{count, plural, =1 {1 subscriber} other {${count} subscribers}}`, { count })
84 private getUserIfNeeded (account: Account) {
85 if (!account.userId) return
86 if (!this.authService.isLoggedIn()) return
88 const user = this.authService.getUser()
89 if (user.hasRight(UserRight.MANAGE_USERS)) {
90 this.userService.getUser(account.userId)
92 user => this.user = user,
94 err => this.notifier.error(err.message)