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, BlocklistService } 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 blocklist: BlocklistService,
56 private screenService: ScreenService
61 this.routeSub = this.route.params
63 map(params => params['accountId']),
64 distinctUntilChanged(),
65 switchMap(accountId => this.accountService.getAccount(accountId)),
66 tap(account => this.onAccount(account)),
67 switchMap(account => this.videoChannelService.listAccountVideoChannels({ account })),
68 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
69 HttpStatusCode.BAD_REQUEST_400,
70 HttpStatusCode.NOT_FOUND_404
74 next: videoChannels => {
75 this.videoChannels = videoChannels.data
78 error: 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 searchChanged (search: string) {
131 const queryParams = { search }
133 this.router.navigate([ './videos' ], { queryParams, relativeTo: this.route, queryParamsHandling: 'merge' })
136 onSearchInputDisplayChanged (displayed: boolean) {
137 this.hideMenu = this.isInSmallView() && displayed
140 hasVideoChannels () {
141 return this.videoChannels.length !== 0
144 hasShowMoreDescription () {
145 return !this.accountDescriptionExpanded && this.accountDescriptionHTML.length > 100
149 return this.route.children[0].snapshot.url[0].path === 'video-channels'
152 private async onAccount (account: Account) {
153 this.accountFollowerTitle = $localize`${account.followersCount} direct account followers`
155 this.accountDescriptionHTML = await this.markdown.textMarkdownToHTML(account.description)
157 // After the markdown renderer to avoid layout changes
158 this.account = account
160 this.updateModerationActions()
161 this.loadUserIfNeeded(account)
162 this.loadAccountVideosCount()
163 this.loadAccountBlockStatus()
166 private showReportModal () {
167 this.accountReportModal.show(this.account)
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 this.prependModerationActions = []
189 if (!this.authService.isLoggedIn()) return
191 this.authService.userInformationLoaded.subscribe(
193 if (this.isManageable()) return
195 // It's not our account, we can report it
196 this.prependModerationActions = [
198 label: $localize`Report`,
202 label: $localize`Report this account`,
203 handler: () => this.showReportModal()
210 private loadAccountVideosCount () {
211 this.videoService.getAccountVideos({
212 account: this.account,
218 }).subscribe(res => {
219 this.accountVideosCount = res.total
223 private loadAccountBlockStatus () {
224 this.blocklist.getStatus({ accounts: [ this.account.nameWithHostForced ], hosts: [ this.account.host ] })
225 .subscribe(status => this.account.updateBlockStatus(status))