1 import { Subscription } from 'rxjs'
2 import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
4 import { ActivatedRoute } from '@angular/router'
5 import { AuthService, Notifier, RedirectService, RestExtractor, ScreenService, UserService } from '@app/core'
6 import { Account, AccountService, DropdownAction, ListOverflowItem, VideoChannel, VideoChannelService } from '@app/shared/shared-main'
7 import { AccountReportComponent } from '@app/shared/shared-moderation'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { User, UserRight } from '@shared/models'
12 templateUrl: './accounts.component.html',
13 styleUrls: [ './accounts.component.scss' ]
15 export class AccountsComponent implements OnInit, OnDestroy {
16 @ViewChild('accountReportModal') accountReportModal: AccountReportComponent
20 videoChannels: VideoChannel[] = []
21 links: ListOverflowItem[] = []
23 isAccountManageable = false
24 accountFollowerTitle = ''
26 prependModerationActions: DropdownAction<any>[]
28 private routeSub: Subscription
31 private route: ActivatedRoute,
32 private userService: UserService,
33 private accountService: AccountService,
34 private videoChannelService: VideoChannelService,
35 private notifier: Notifier,
36 private restExtractor: RestExtractor,
37 private redirectService: RedirectService,
38 private authService: AuthService,
39 private screenService: ScreenService,
45 this.routeSub = this.route.params
47 map(params => params[ 'accountId' ]),
48 distinctUntilChanged(),
49 switchMap(accountId => this.accountService.getAccount(accountId)),
50 tap(account => this.onAccount(account)),
51 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
52 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
55 videoChannels => this.videoChannels = videoChannels.data,
57 err => this.notifier.error(err.message)
61 { label: this.i18n('VIDEO CHANNELS'), routerLink: 'video-channels' },
62 { label: this.i18n('VIDEOS'), routerLink: 'videos' },
63 { label: this.i18n('ABOUT'), routerLink: 'about' }
68 if (this.routeSub) this.routeSub.unsubscribe()
71 get naiveAggregatedSubscribers () {
72 return this.videoChannels.reduce(
73 (acc, val) => acc + val.followersCount,
74 this.account.followersCount // accumulator starts with the base number of subscribers the account has
78 get isInSmallView () {
79 return this.screenService.isInSmallView()
83 this.getUserIfNeeded(this.account)
87 this.redirectService.redirectToHomepage()
90 activateCopiedMessage () {
91 this.notifier.success(this.i18n('Username copied'))
94 subscribersDisplayFor (count: number) {
95 return this.i18n('{count, plural, =1 {1 subscriber} other {{{count}} subscribers}}', { count })
98 private onAccount (account: Account) {
99 this.prependModerationActions = undefined
101 this.account = account
103 if (this.authService.isLoggedIn()) {
104 this.authService.userInformationLoaded.subscribe(
106 this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id
108 this.accountFollowerTitle = this.i18n(
109 '{{followers}} direct account followers',
110 { followers: this.subscribersDisplayFor(account.followersCount) }
113 // It's not our account, we can report it
114 if (!this.isAccountManageable) {
115 this.prependModerationActions = [
117 label: this.i18n('Report account'),
118 handler: () => this.showReportModal()
126 this.getUserIfNeeded(account)
129 private showReportModal () {
130 this.accountReportModal.show()
133 private getUserIfNeeded (account: Account) {
134 if (!account.userId || !this.authService.isLoggedIn()) return
136 const user = this.authService.getUser()
137 if (user.hasRight(UserRight.MANAGE_USERS)) {
138 this.userService.getUser(account.userId).subscribe(
139 accountUser => this.accountUser = accountUser,
141 err => this.notifier.error(err.message)