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, Router } from '@angular/router'
5 import { AuthService, MarkdownService, Notifier, RedirectService, RestExtractor, ScreenService, UserService } from '@app/core'
14 } from '@app/shared/shared-main'
15 import { AccountReportComponent } from '@app/shared/shared-moderation'
16 import { HttpStatusCode, User, UserRight } from '@shared/models'
19 templateUrl: './accounts.component.html',
20 styleUrls: [ './accounts.component.scss' ]
22 export class AccountsComponent implements OnInit, OnDestroy {
23 @ViewChild('accountReportModal') accountReportModal: AccountReportComponent
28 videoChannels: VideoChannel[] = []
30 links: ListOverflowItem[] = []
33 accountFollowerTitle = ''
35 accountVideosCount: number
36 accountDescriptionHTML = ''
37 accountDescriptionExpanded = false
39 prependModerationActions: DropdownAction<any>[]
41 private routeSub: Subscription
44 private route: ActivatedRoute,
45 private router: Router,
46 private userService: UserService,
47 private accountService: AccountService,
48 private videoChannelService: VideoChannelService,
49 private notifier: Notifier,
50 private restExtractor: RestExtractor,
51 private redirectService: RedirectService,
52 private authService: AuthService,
53 private videoService: VideoService,
54 private markdown: MarkdownService,
55 private screenService: ScreenService
60 this.routeSub = this.route.params
62 map(params => params['accountId']),
63 distinctUntilChanged(),
64 switchMap(accountId => this.accountService.getAccount(accountId)),
65 tap(account => this.onAccount(account)),
66 switchMap(account => this.videoChannelService.listAccountVideoChannels({ account })),
67 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
68 HttpStatusCode.BAD_REQUEST_400,
69 HttpStatusCode.NOT_FOUND_404
73 next: videoChannels => {
74 this.videoChannels = videoChannels.data
77 error: err => this.notifier.error(err.message)
81 { label: $localize`CHANNELS`, routerLink: 'video-channels' },
82 { label: $localize`VIDEOS`, routerLink: 'videos' }
87 if (this.routeSub) this.routeSub.unsubscribe()
90 naiveAggregatedSubscribers () {
91 return this.videoChannels.reduce(
92 (acc, val) => acc + val.followersCount,
93 this.account.followersCount // accumulator starts with the base number of subscribers the account has
98 return this.authService.isLoggedIn()
102 return this.screenService.isInSmallView()
106 if (!this.isUserLoggedIn()) return false
108 return this.account?.userId === this.authService.getUser().id
112 this.loadUserIfNeeded(this.account)
116 this.redirectService.redirectToHomepage()
119 activateCopiedMessage () {
120 this.notifier.success($localize`Username copied`)
123 subscribersDisplayFor (count: number) {
124 if (count === 1) return $localize`1 subscriber`
126 return $localize`${count} subscribers`
129 searchChanged (search: string) {
130 const queryParams = { search }
132 this.router.navigate([ './videos' ], { queryParams, relativeTo: this.route, queryParamsHandling: 'merge' })
135 onSearchInputDisplayChanged (displayed: boolean) {
136 this.hideMenu = this.isInSmallView() && displayed
139 hasVideoChannels () {
140 return this.videoChannels.length !== 0
143 hasShowMoreDescription () {
144 return !this.accountDescriptionExpanded && this.accountDescriptionHTML.length > 100
148 return this.route.children[0].snapshot.url[0].path === 'video-channels'
151 private async onAccount (account: Account) {
152 this.accountFollowerTitle = $localize`${account.followersCount} direct account followers`
154 this.prependModerationActions = undefined
156 this.accountDescriptionHTML = await this.markdown.textMarkdownToHTML(account.description)
158 // After the markdown renderer to avoid layout changes
159 this.account = account
161 this.updateModerationActions()
162 this.loadUserIfNeeded(account)
163 this.loadAccountVideosCount()
166 private showReportModal () {
167 this.accountReportModal.show()
170 private loadUserIfNeeded (account: Account) {
171 if (!account.userId || !this.authService.isLoggedIn()) return
173 const user = this.authService.getUser()
174 if (user.hasRight(UserRight.MANAGE_USERS)) {
175 this.userService.getUser(account.userId)
177 next: accountUser => {
178 this.accountUser = accountUser
181 error: err => this.notifier.error(err.message)
186 private updateModerationActions () {
187 if (!this.authService.isLoggedIn()) return
189 this.authService.userInformationLoaded.subscribe(
191 if (this.isManageable()) return
193 // It's not our account, we can report it
194 this.prependModerationActions = [
196 label: $localize`Report this account`,
197 handler: () => this.showReportModal()
204 private loadAccountVideosCount () {
205 this.videoService.getAccountVideos({
206 account: this.account,
212 }).subscribe(res => {
213 this.accountVideosCount = res.total