1 import { Subscription } from 'rxjs'
2 import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute } from '@angular/router'
5 import { AuthService, Notifier, RedirectService, RestExtractor, ScreenService, UserService } from '@app/core'
6 import { Account, AccountService, ListOverflowItem, VideoChannel, VideoChannelService } from '@app/shared/shared-main'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { User, UserRight } from '@shared/models'
11 templateUrl: './accounts.component.html',
12 styleUrls: [ './accounts.component.scss' ]
14 export class AccountsComponent implements OnInit, OnDestroy {
17 videoChannels: VideoChannel[] = []
18 links: ListOverflowItem[] = []
20 isAccountManageable = false
21 accountFollowerTitle = ''
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,
34 private screenService: ScreenService,
40 this.routeSub = this.route.params
42 map(params => params[ 'accountId' ]),
43 distinctUntilChanged(),
44 switchMap(accountId => this.accountService.getAccount(accountId)),
46 this.account = account
48 if (this.authService.isLoggedIn()) {
49 this.authService.userInformationLoaded.subscribe(
51 this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id
53 this.accountFollowerTitle = this.i18n(
54 '{{followers}} direct account followers',
55 { followers: this.subscribersDisplayFor(account.followersCount) }
61 this.getUserIfNeeded(account)
63 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
64 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
67 videoChannels => this.videoChannels = videoChannels.data,
69 err => this.notifier.error(err.message)
73 { label: this.i18n('VIDEO CHANNELS'), routerLink: 'video-channels' },
74 { label: this.i18n('VIDEOS'), routerLink: 'videos' },
75 { label: this.i18n('ABOUT'), routerLink: 'about' }
80 if (this.routeSub) this.routeSub.unsubscribe()
83 get naiveAggregatedSubscribers () {
84 return this.videoChannels.reduce(
85 (acc, val) => acc + val.followersCount,
86 this.account.followersCount // accumulator starts with the base number of subscribers the account has
90 get isInSmallView () {
91 return this.screenService.isInSmallView()
95 this.getUserIfNeeded(this.account)
99 this.redirectService.redirectToHomepage()
102 activateCopiedMessage () {
103 this.notifier.success(this.i18n('Username copied'))
106 subscribersDisplayFor (count: number) {
107 return this.i18n('{count, plural, =1 {1 subscriber} other {{{count}} subscribers}}', { count })
110 private getUserIfNeeded (account: Account) {
111 if (!account.userId || !this.authService.isLoggedIn()) return
113 const user = this.authService.getUser()
114 if (user.hasRight(UserRight.MANAGE_USERS)) {
115 this.userService.getUser(account.userId).subscribe(
116 accountUser => this.accountUser = accountUser,
118 err => this.notifier.error(err.message)