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'
9 import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
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
44 this.routeSub = this.route.params
46 map(params => params[ 'accountId' ]),
47 distinctUntilChanged(),
48 switchMap(accountId => this.accountService.getAccount(accountId)),
49 tap(account => this.onAccount(account)),
50 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
51 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [
52 HttpStatusCode.BAD_REQUEST_400,
53 HttpStatusCode.NOT_FOUND_404
57 videoChannels => this.videoChannels = videoChannels.data,
59 err => this.notifier.error(err.message)
63 { label: $localize`VIDEO CHANNELS`, routerLink: 'video-channels' },
64 { label: $localize`VIDEOS`, routerLink: 'videos' },
65 { label: $localize`ABOUT`, routerLink: 'about' }
70 if (this.routeSub) this.routeSub.unsubscribe()
73 get naiveAggregatedSubscribers () {
74 return this.videoChannels.reduce(
75 (acc, val) => acc + val.followersCount,
76 this.account.followersCount // accumulator starts with the base number of subscribers the account has
80 get isInSmallView () {
81 return this.screenService.isInSmallView()
85 this.getUserIfNeeded(this.account)
89 this.redirectService.redirectToHomepage()
92 activateCopiedMessage () {
93 this.notifier.success($localize`Username copied`)
96 subscribersDisplayFor (count: number) {
97 if (count === 1) return $localize`1 subscriber`
99 return $localize`${count} subscribers`
102 private onAccount (account: Account) {
103 this.prependModerationActions = undefined
105 this.account = account
107 if (this.authService.isLoggedIn()) {
108 this.authService.userInformationLoaded.subscribe(
110 this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id
112 const followers = this.subscribersDisplayFor(account.followersCount)
113 this.accountFollowerTitle = $localize`${followers} direct account followers`
115 // It's not our account, we can report it
116 if (!this.isAccountManageable) {
117 this.prependModerationActions = [
119 label: $localize`Report this account`,
120 handler: () => this.showReportModal()
128 this.getUserIfNeeded(account)
131 private showReportModal () {
132 this.accountReportModal.show()
135 private getUserIfNeeded (account: Account) {
136 if (!account.userId || !this.authService.isLoggedIn()) return
138 const user = this.authService.getUser()
139 if (user.hasRight(UserRight.MANAGE_USERS)) {
140 this.userService.getUser(account.userId).subscribe(
141 accountUser => this.accountUser = accountUser,
143 err => this.notifier.error(err.message)