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, User, UserRight } from '@shared/models'
17 import { AccountSearchComponent } from './account-search/account-search.component'
20 templateUrl: './accounts.component.html',
21 styleUrls: [ './accounts.component.scss' ]
23 export class AccountsComponent implements OnInit, OnDestroy {
24 @ViewChild('accountReportModal') accountReportModal: AccountReportComponent
26 accountSearch: AccountSearchComponent
31 videoChannels: VideoChannel[] = []
33 links: ListOverflowItem[] = []
36 accountFollowerTitle = ''
38 accountVideosCount: number
39 accountDescriptionHTML = ''
40 accountDescriptionExpanded = false
42 prependModerationActions: DropdownAction<any>[]
44 private routeSub: Subscription
47 private route: ActivatedRoute,
48 private userService: UserService,
49 private accountService: AccountService,
50 private videoChannelService: VideoChannelService,
51 private notifier: Notifier,
52 private restExtractor: RestExtractor,
53 private redirectService: RedirectService,
54 private authService: AuthService,
55 private videoService: VideoService,
56 private markdown: MarkdownService,
57 private screenService: ScreenService
62 this.routeSub = this.route.params
64 map(params => params['accountId']),
65 distinctUntilChanged(),
66 switchMap(accountId => this.accountService.getAccount(accountId)),
67 tap(account => this.onAccount(account)),
68 switchMap(account => this.videoChannelService.listAccountVideoChannels({ account })),
69 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
70 HttpStatusCode.BAD_REQUEST_400,
71 HttpStatusCode.NOT_FOUND_404
75 next: videoChannels => {
76 this.videoChannels = videoChannels.data
79 error: err => this.notifier.error(err.message)
83 { label: $localize`CHANNELS`, routerLink: 'video-channels' },
84 { label: $localize`VIDEOS`, routerLink: 'videos' }
89 if (this.routeSub) this.routeSub.unsubscribe()
92 naiveAggregatedSubscribers () {
93 return this.videoChannels.reduce(
94 (acc, val) => acc + val.followersCount,
95 this.account.followersCount // accumulator starts with the base number of subscribers the account has
100 return this.authService.isLoggedIn()
104 return this.screenService.isInSmallView()
108 if (!this.isUserLoggedIn()) return false
110 return this.account?.userId === this.authService.getUser().id
114 this.loadUserIfNeeded(this.account)
118 this.redirectService.redirectToHomepage()
121 activateCopiedMessage () {
122 this.notifier.success($localize`Username copied`)
125 subscribersDisplayFor (count: number) {
126 if (count === 1) return $localize`1 subscriber`
128 return $localize`${count} subscribers`
131 onOutletLoaded (component: Component) {
132 if (component instanceof AccountSearchComponent) {
133 this.accountSearch = component
135 this.accountSearch = undefined
139 searchChanged (search: string) {
140 if (this.accountSearch) this.accountSearch.updateSearch(search)
143 onSearchInputDisplayChanged (displayed: boolean) {
144 this.hideMenu = this.isInSmallView() && displayed
147 hasVideoChannels () {
148 return this.videoChannels.length !== 0
151 hasShowMoreDescription () {
152 return !this.accountDescriptionExpanded && this.accountDescriptionHTML.length > 100
155 private async onAccount (account: Account) {
156 this.accountFollowerTitle = $localize`${account.followersCount} direct account followers`
158 this.prependModerationActions = undefined
160 this.accountDescriptionHTML = await this.markdown.textMarkdownToHTML(account.description)
162 // After the markdown renderer to avoid layout changes
163 this.account = account
165 this.updateModerationActions()
166 this.loadUserIfNeeded(account)
167 this.loadAccountVideosCount()
170 private showReportModal () {
171 this.accountReportModal.show()
174 private loadUserIfNeeded (account: Account) {
175 if (!account.userId || !this.authService.isLoggedIn()) return
177 const user = this.authService.getUser()
178 if (user.hasRight(UserRight.MANAGE_USERS)) {
179 this.userService.getUser(account.userId)
181 next: accountUser => {
182 this.accountUser = accountUser
185 error: err => this.notifier.error(err.message)
190 private updateModerationActions () {
191 if (!this.authService.isLoggedIn()) return
193 this.authService.userInformationLoaded.subscribe(
195 if (this.isManageable()) return
197 // It's not our account, we can report it
198 this.prependModerationActions = [
200 label: $localize`Report this account`,
201 handler: () => this.showReportModal()
208 private loadAccountVideosCount () {
209 this.videoService.getAccountVideos({
210 account: this.account,
216 }).subscribe(res => {
217 this.accountVideosCount = res.total