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 import { ListOverflowItem } from '@app/shared/misc/list-overflow.component'
14 import { ScreenService } from '@app/shared/misc/screen.service'
17 templateUrl: './accounts.component.html',
18 styleUrls: [ './accounts.component.scss' ]
20 export class AccountsComponent implements OnInit, OnDestroy {
23 videoChannels: VideoChannel[] = []
24 links: ListOverflowItem[] = []
26 isAccountManageable = false
27 accountFollowerTitle = ''
29 private routeSub: Subscription
32 private route: ActivatedRoute,
33 private userService: UserService,
34 private accountService: AccountService,
35 private videoChannelService: VideoChannelService,
36 private notifier: Notifier,
37 private restExtractor: RestExtractor,
38 private redirectService: RedirectService,
39 private authService: AuthService,
40 private screenService: ScreenService,
46 this.routeSub = this.route.params
48 map(params => params[ 'accountId' ]),
49 distinctUntilChanged(),
50 switchMap(accountId => this.accountService.getAccount(accountId)),
52 this.account = account
54 if (this.authService.isLoggedIn()) {
55 this.authService.userInformationLoaded.subscribe(
57 this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id
59 this.accountFollowerTitle = this.i18n(
60 '{{followers}} direct account followers',
61 { followers: this.subscribersDisplayFor(account.followersCount) }
67 this.getUserIfNeeded(account)
69 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
70 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
73 videoChannels => this.videoChannels = videoChannels.data,
75 err => this.notifier.error(err.message)
79 { label: this.i18n('VIDEO CHANNELS'), routerLink: 'video-channels' },
80 { label: this.i18n('VIDEOS'), routerLink: 'videos' },
81 { label: this.i18n('ABOUT'), routerLink: 'about' }
86 if (this.routeSub) this.routeSub.unsubscribe()
89 get naiveAggregatedSubscribers () {
90 return this.videoChannels.reduce(
91 (acc, val) => acc + val.followersCount,
92 this.account.followersCount // accumulator starts with the base number of subscribers the account has
96 get isInSmallView () {
97 return this.screenService.isInSmallView()
101 this.getUserIfNeeded(this.account)
105 this.redirectService.redirectToHomepage()
108 activateCopiedMessage () {
109 this.notifier.success(this.i18n('Username copied'))
112 subscribersDisplayFor (count: number) {
113 return this.i18n('{count, plural, =1 {1 subscriber} other {{{count}} subscribers}}', { count })
116 private getUserIfNeeded (account: Account) {
117 if (!account.userId || !this.authService.isLoggedIn()) return
119 const user = this.authService.getUser()
120 if (user.hasRight(UserRight.MANAGE_USERS)) {
121 this.userService.getUser(account.userId).subscribe(
122 accountUser => this.accountUser = accountUser,
124 err => this.notifier.error(err.message)