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, 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 } from '@shared/core-utils/miscs/http-error-codes'
17 import { User, UserRight } from '@shared/models'
18 import { AccountSearchComponent } from './account-search/account-search.component'
21 templateUrl: './accounts.component.html',
22 styleUrls: [ './accounts.component.scss' ]
24 export class AccountsComponent implements OnInit, OnDestroy {
25 @ViewChild('accountReportModal') accountReportModal: AccountReportComponent
27 accountSearch: AccountSearchComponent
32 videoChannels: VideoChannel[] = []
34 links: ListOverflowItem[] = []
37 accountFollowerTitle = ''
39 accountVideosCount: number
40 accountDescriptionHTML = ''
41 accountDescriptionExpanded = false
43 prependModerationActions: DropdownAction<any>[]
45 private routeSub: Subscription
48 private route: ActivatedRoute,
49 private userService: UserService,
50 private accountService: AccountService,
51 private videoChannelService: VideoChannelService,
52 private notifier: Notifier,
53 private restExtractor: RestExtractor,
54 private redirectService: RedirectService,
55 private authService: AuthService,
56 private videoService: VideoService,
57 private markdown: MarkdownService,
58 private screenService: ScreenService
63 this.routeSub = this.route.params
65 map(params => params[ 'accountId' ]),
66 distinctUntilChanged(),
67 switchMap(accountId => this.accountService.getAccount(accountId)),
68 tap(account => this.onAccount(account)),
69 switchMap(account => this.videoChannelService.listAccountVideoChannels({ account })),
70 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
71 HttpStatusCode.BAD_REQUEST_400,
72 HttpStatusCode.NOT_FOUND_404
76 videoChannels => this.videoChannels = videoChannels.data,
78 err => this.notifier.error(err.message)
82 { label: $localize`CHANNELS`, routerLink: 'video-channels' },
83 { label: $localize`VIDEOS`, routerLink: 'videos' }
88 if (this.routeSub) this.routeSub.unsubscribe()
91 naiveAggregatedSubscribers () {
92 return this.videoChannels.reduce(
93 (acc, val) => acc + val.followersCount,
94 this.account.followersCount // accumulator starts with the base number of subscribers the account has
99 return this.authService.isLoggedIn()
103 return this.screenService.isInSmallView()
107 if (!this.isUserLoggedIn()) return false
109 return this.account?.userId === this.authService.getUser().id
113 this.loadUserIfNeeded(this.account)
117 this.redirectService.redirectToHomepage()
120 activateCopiedMessage () {
121 this.notifier.success($localize`Username copied`)
124 subscribersDisplayFor (count: number) {
125 if (count === 1) return $localize`1 subscriber`
127 return $localize`${count} subscribers`
130 onOutletLoaded (component: Component) {
131 if (component instanceof AccountSearchComponent) {
132 this.accountSearch = component
134 this.accountSearch = undefined
138 searchChanged (search: string) {
139 if (this.accountSearch) this.accountSearch.updateSearch(search)
142 onSearchInputDisplayChanged (displayed: boolean) {
143 this.hideMenu = this.isInSmallView() && displayed
146 hasVideoChannels () {
147 return this.videoChannels.length !== 0
150 hasShowMoreDescription () {
151 return !this.accountDescriptionExpanded && this.accountDescriptionHTML.length > 100
154 private async onAccount (account: Account) {
155 this.accountFollowerTitle = $localize`${account.followersCount} direct account followers`
157 this.prependModerationActions = undefined
159 this.accountDescriptionHTML = await this.markdown.textMarkdownToHTML(account.description)
161 // After the markdown renderer to avoid layout changes
162 this.account = account
164 this.updateModerationActions()
165 this.loadUserIfNeeded(account)
166 this.loadAccountVideosCount()
169 private showReportModal () {
170 this.accountReportModal.show()
173 private loadUserIfNeeded (account: Account) {
174 if (!account.userId || !this.authService.isLoggedIn()) return
176 const user = this.authService.getUser()
177 if (user.hasRight(UserRight.MANAGE_USERS)) {
178 this.userService.getUser(account.userId).subscribe(
179 accountUser => this.accountUser = accountUser,
181 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 => this.accountVideosCount = res.total)