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 { User, UserRight } from '@shared/models'
11 templateUrl: './accounts.component.html',
12 styleUrls: [ './accounts.component.scss' ]
14 export class AccountsComponent implements OnInit, OnDestroy {
15 @ViewChild('accountReportModal') accountReportModal: AccountReportComponent
19 videoChannels: VideoChannel[] = []
20 links: ListOverflowItem[] = []
22 isAccountManageable = false
23 accountFollowerTitle = ''
25 prependModerationActions: DropdownAction<any>[]
27 private routeSub: Subscription
30 private route: ActivatedRoute,
31 private userService: UserService,
32 private accountService: AccountService,
33 private videoChannelService: VideoChannelService,
34 private notifier: Notifier,
35 private restExtractor: RestExtractor,
36 private redirectService: RedirectService,
37 private authService: AuthService,
38 private screenService: ScreenService
43 this.routeSub = this.route.params
45 map(params => params[ 'accountId' ]),
46 distinctUntilChanged(),
47 switchMap(accountId => this.accountService.getAccount(accountId)),
48 tap(account => this.onAccount(account)),
49 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
50 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
53 videoChannels => this.videoChannels = videoChannels.data,
55 err => this.notifier.error(err.message)
59 { label: $localize`VIDEO CHANNELS`, routerLink: 'video-channels' },
60 { label: $localize`VIDEOS`, routerLink: 'videos' },
61 { label: $localize`ABOUT`, routerLink: 'about' }
66 if (this.routeSub) this.routeSub.unsubscribe()
69 get naiveAggregatedSubscribers () {
70 return this.videoChannels.reduce(
71 (acc, val) => acc + val.followersCount,
72 this.account.followersCount // accumulator starts with the base number of subscribers the account has
76 get isInSmallView () {
77 return this.screenService.isInSmallView()
81 this.getUserIfNeeded(this.account)
85 this.redirectService.redirectToHomepage()
88 activateCopiedMessage () {
89 this.notifier.success($localize`Username copied`)
92 subscribersDisplayFor (count: number) {
93 if (count === 1) return $localize`1 subscriber`
95 return $localize`${count} subscribers`
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 const followers = this.subscribersDisplayFor(account.followersCount)
109 this.accountFollowerTitle = $localize`${followers} direct account followers`
111 // It's not our account, we can report it
112 if (!this.isAccountManageable) {
113 this.prependModerationActions = [
115 label: $localize`Report this account`,
116 handler: () => this.showReportModal()
124 this.getUserIfNeeded(account)
127 private showReportModal () {
128 this.accountReportModal.show()
131 private getUserIfNeeded (account: Account) {
132 if (!account.userId || !this.authService.isLoggedIn()) return
134 const user = this.authService.getUser()
135 if (user.hasRight(UserRight.MANAGE_USERS)) {
136 this.userService.getUser(account.userId).subscribe(
137 accountUser => this.accountUser = accountUser,
139 err => this.notifier.error(err.message)