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'
10 import { AccountSearchComponent } from './account-search/account-search.component'
13 templateUrl: './accounts.component.html',
14 styleUrls: [ './accounts.component.scss' ]
16 export class AccountsComponent implements OnInit, OnDestroy {
17 @ViewChild('accountReportModal') accountReportModal: AccountReportComponent
18 accountSearch: AccountSearchComponent
22 videoChannels: VideoChannel[] = []
23 links: ListOverflowItem[] = []
25 isAccountManageable = false
26 accountFollowerTitle = ''
28 prependModerationActions: DropdownAction<any>[]
30 private routeSub: Subscription
33 private route: ActivatedRoute,
34 private userService: UserService,
35 private accountService: AccountService,
36 private videoChannelService: VideoChannelService,
37 private notifier: Notifier,
38 private restExtractor: RestExtractor,
39 private redirectService: RedirectService,
40 private authService: AuthService,
41 private screenService: ScreenService
46 this.routeSub = this.route.params
48 map(params => params[ 'accountId' ]),
49 distinctUntilChanged(),
50 switchMap(accountId => this.accountService.getAccount(accountId)),
51 tap(account => this.onAccount(account)),
52 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
53 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
54 HttpStatusCode.BAD_REQUEST_400,
55 HttpStatusCode.NOT_FOUND_404
59 videoChannels => this.videoChannels = videoChannels.data,
61 err => this.notifier.error(err.message)
65 { label: $localize`VIDEO CHANNELS`, routerLink: 'video-channels' },
66 { label: $localize`VIDEOS`, routerLink: 'videos' },
67 { label: $localize`ABOUT`, routerLink: 'about' }
72 if (this.routeSub) this.routeSub.unsubscribe()
75 get naiveAggregatedSubscribers () {
76 return this.videoChannels.reduce(
77 (acc, val) => acc + val.followersCount,
78 this.account.followersCount // accumulator starts with the base number of subscribers the account has
82 get isInSmallView () {
83 return this.screenService.isInSmallView()
87 this.getUserIfNeeded(this.account)
91 this.redirectService.redirectToHomepage()
94 activateCopiedMessage () {
95 this.notifier.success($localize`Username copied`)
98 subscribersDisplayFor (count: number) {
99 if (count === 1) return $localize`1 subscriber`
101 return $localize`${count} subscribers`
104 onOutletLoaded (component: Component) {
105 if (component instanceof AccountSearchComponent) {
106 this.accountSearch = component
108 this.accountSearch = undefined
112 searchChanged (search: string) {
113 if (this.accountSearch) this.accountSearch.updateSearch(search)
116 private onAccount (account: Account) {
117 this.prependModerationActions = undefined
119 this.account = account
121 if (this.authService.isLoggedIn()) {
122 this.authService.userInformationLoaded.subscribe(
124 this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id
126 const followers = this.subscribersDisplayFor(account.followersCount)
127 this.accountFollowerTitle = $localize`${followers} direct account followers`
129 // It's not our account, we can report it
130 if (!this.isAccountManageable) {
131 this.prependModerationActions = [
133 label: $localize`Report this account`,
134 handler: () => this.showReportModal()
142 this.getUserIfNeeded(account)
145 private showReportModal () {
146 this.accountReportModal.show()
149 private getUserIfNeeded (account: Account) {
150 if (!account.userId || !this.authService.isLoggedIn()) return
152 const user = this.authService.getUser()
153 if (user.hasRight(UserRight.MANAGE_USERS)) {
154 this.userService.getUser(account.userId).subscribe(
155 accountUser => this.accountUser = accountUser,
157 err => this.notifier.error(err.message)