2020-06-23 14:10:17 +02:00
|
|
|
import { Subscription } from 'rxjs'
|
|
|
|
import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
|
2020-07-09 15:54:24 +02:00
|
|
|
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
|
2018-04-24 15:10:54 +02:00
|
|
|
import { ActivatedRoute } from '@angular/router'
|
2021-03-26 13:20:37 +01:00
|
|
|
import { AuthService, MarkdownService, Notifier, RedirectService, RestExtractor, ScreenService, UserService } from '@app/core'
|
|
|
|
import {
|
|
|
|
Account,
|
|
|
|
AccountService,
|
|
|
|
DropdownAction,
|
|
|
|
ListOverflowItem,
|
|
|
|
VideoChannel,
|
|
|
|
VideoChannelService,
|
|
|
|
VideoService
|
|
|
|
} from '@app/shared/shared-main'
|
2020-07-09 15:54:24 +02:00
|
|
|
import { AccountReportComponent } from '@app/shared/shared-moderation'
|
2021-07-16 10:42:24 +02:00
|
|
|
import { HttpStatusCode, User, UserRight } from '@shared/models'
|
2021-01-19 13:43:33 +01:00
|
|
|
import { AccountSearchComponent } from './account-search/account-search.component'
|
2018-04-24 15:10:54 +02:00
|
|
|
|
|
|
|
@Component({
|
2018-04-25 16:56:13 +02:00
|
|
|
templateUrl: './accounts.component.html',
|
|
|
|
styleUrls: [ './accounts.component.scss' ]
|
2018-04-24 15:10:54 +02:00
|
|
|
})
|
2018-06-07 14:58:41 +02:00
|
|
|
export class AccountsComponent implements OnInit, OnDestroy {
|
2020-07-09 15:54:24 +02:00
|
|
|
@ViewChild('accountReportModal') accountReportModal: AccountReportComponent
|
2021-03-26 13:20:37 +01:00
|
|
|
|
2021-01-19 13:43:33 +01:00
|
|
|
accountSearch: AccountSearchComponent
|
2020-07-09 15:54:24 +02:00
|
|
|
|
2018-04-25 10:21:38 +02:00
|
|
|
account: Account
|
2020-01-22 14:02:36 +01:00
|
|
|
accountUser: User
|
2021-03-26 13:20:37 +01:00
|
|
|
|
2020-01-10 16:22:55 +01:00
|
|
|
videoChannels: VideoChannel[] = []
|
2021-03-26 13:20:37 +01:00
|
|
|
|
2020-02-05 20:54:37 +01:00
|
|
|
links: ListOverflowItem[] = []
|
2021-03-26 13:20:37 +01:00
|
|
|
hideMenu = false
|
2018-04-24 15:10:54 +02:00
|
|
|
|
2020-01-22 14:02:36 +01:00
|
|
|
accountFollowerTitle = ''
|
|
|
|
|
2021-03-26 13:20:37 +01:00
|
|
|
accountVideosCount: number
|
|
|
|
accountDescriptionHTML = ''
|
|
|
|
accountDescriptionExpanded = false
|
|
|
|
|
2020-07-09 15:54:24 +02:00
|
|
|
prependModerationActions: DropdownAction<any>[]
|
|
|
|
|
2018-06-07 11:19:26 +02:00
|
|
|
private routeSub: Subscription
|
|
|
|
|
2018-04-24 15:10:54 +02:00
|
|
|
constructor (
|
|
|
|
private route: ActivatedRoute,
|
2018-10-05 16:56:14 +02:00
|
|
|
private userService: UserService,
|
2018-05-31 11:35:01 +02:00
|
|
|
private accountService: AccountService,
|
2020-01-07 23:51:14 +01:00
|
|
|
private videoChannelService: VideoChannelService,
|
2018-12-19 16:04:34 +01:00
|
|
|
private notifier: Notifier,
|
2018-10-05 16:56:14 +02:00
|
|
|
private restExtractor: RestExtractor,
|
|
|
|
private redirectService: RedirectService,
|
2019-06-19 15:14:13 +02:00
|
|
|
private authService: AuthService,
|
2021-03-26 13:20:37 +01:00
|
|
|
private videoService: VideoService,
|
|
|
|
private markdown: MarkdownService,
|
2020-08-12 10:40:04 +02:00
|
|
|
private screenService: ScreenService
|
2020-01-22 15:01:38 +01:00
|
|
|
) {
|
|
|
|
}
|
2018-04-24 15:10:54 +02:00
|
|
|
|
|
|
|
ngOnInit () {
|
2018-06-07 11:19:26 +02:00
|
|
|
this.routeSub = this.route.params
|
2020-01-22 15:01:38 +01:00
|
|
|
.pipe(
|
2021-08-17 14:42:53 +02:00
|
|
|
map(params => params['accountId']),
|
2020-01-22 15:01:38 +01:00
|
|
|
distinctUntilChanged(),
|
|
|
|
switchMap(accountId => this.accountService.getAccount(accountId)),
|
2020-07-09 15:54:24 +02:00
|
|
|
tap(account => this.onAccount(account)),
|
2021-05-10 09:31:33 +02:00
|
|
|
switchMap(account => this.videoChannelService.listAccountVideoChannels({ account })),
|
2021-01-24 03:02:04 +01:00
|
|
|
catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
|
2020-12-08 21:16:10 +01:00
|
|
|
HttpStatusCode.BAD_REQUEST_400,
|
|
|
|
HttpStatusCode.NOT_FOUND_404
|
|
|
|
]))
|
2020-01-22 15:01:38 +01:00
|
|
|
)
|
2021-08-17 11:27:47 +02:00
|
|
|
.subscribe({
|
2021-08-17 14:42:53 +02:00
|
|
|
next: videoChannels => {
|
|
|
|
this.videoChannels = videoChannels.data
|
|
|
|
},
|
2020-01-22 15:01:38 +01:00
|
|
|
|
2021-08-17 11:27:47 +02:00
|
|
|
error: err => this.notifier.error(err.message)
|
|
|
|
})
|
2020-02-05 20:54:37 +01:00
|
|
|
|
|
|
|
this.links = [
|
2021-04-02 15:01:54 +02:00
|
|
|
{ label: $localize`CHANNELS`, routerLink: 'video-channels' },
|
2021-03-26 13:20:37 +01:00
|
|
|
{ label: $localize`VIDEOS`, routerLink: 'videos' }
|
2020-02-05 20:54:37 +01:00
|
|
|
]
|
2018-06-07 11:19:26 +02:00
|
|
|
}
|
2018-04-24 15:10:54 +02:00
|
|
|
|
2018-06-07 11:19:26 +02:00
|
|
|
ngOnDestroy () {
|
|
|
|
if (this.routeSub) this.routeSub.unsubscribe()
|
2018-04-24 15:10:54 +02:00
|
|
|
}
|
2018-10-05 16:56:14 +02:00
|
|
|
|
2021-03-26 13:20:37 +01:00
|
|
|
naiveAggregatedSubscribers () {
|
2020-01-10 16:22:55 +01:00
|
|
|
return this.videoChannels.reduce(
|
|
|
|
(acc, val) => acc + val.followersCount,
|
|
|
|
this.account.followersCount // accumulator starts with the base number of subscribers the account has
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-26 13:20:37 +01:00
|
|
|
isUserLoggedIn () {
|
|
|
|
return this.authService.isLoggedIn()
|
|
|
|
}
|
|
|
|
|
|
|
|
isInSmallView () {
|
2020-03-09 10:22:11 +01:00
|
|
|
return this.screenService.isInSmallView()
|
|
|
|
}
|
|
|
|
|
2021-03-26 13:20:37 +01:00
|
|
|
isManageable () {
|
|
|
|
if (!this.isUserLoggedIn()) return false
|
|
|
|
|
|
|
|
return this.account?.userId === this.authService.getUser().id
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:56:14 +02:00
|
|
|
onUserChanged () {
|
2021-03-26 13:20:37 +01:00
|
|
|
this.loadUserIfNeeded(this.account)
|
2018-10-05 16:56:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onUserDeleted () {
|
|
|
|
this.redirectService.redirectToHomepage()
|
|
|
|
}
|
|
|
|
|
2019-06-19 15:14:13 +02:00
|
|
|
activateCopiedMessage () {
|
2020-08-12 10:40:04 +02:00
|
|
|
this.notifier.success($localize`Username copied`)
|
2019-06-19 15:14:13 +02:00
|
|
|
}
|
|
|
|
|
2020-01-10 16:22:55 +01:00
|
|
|
subscribersDisplayFor (count: number) {
|
2020-08-12 10:40:04 +02:00
|
|
|
if (count === 1) return $localize`1 subscriber`
|
|
|
|
|
|
|
|
return $localize`${count} subscribers`
|
2020-01-10 16:22:55 +01:00
|
|
|
}
|
|
|
|
|
2021-01-19 13:43:33 +01:00
|
|
|
onOutletLoaded (component: Component) {
|
|
|
|
if (component instanceof AccountSearchComponent) {
|
|
|
|
this.accountSearch = component
|
|
|
|
} else {
|
|
|
|
this.accountSearch = undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
searchChanged (search: string) {
|
|
|
|
if (this.accountSearch) this.accountSearch.updateSearch(search)
|
|
|
|
}
|
|
|
|
|
2021-03-26 13:20:37 +01:00
|
|
|
onSearchInputDisplayChanged (displayed: boolean) {
|
|
|
|
this.hideMenu = this.isInSmallView() && displayed
|
|
|
|
}
|
|
|
|
|
2021-03-26 15:53:18 +01:00
|
|
|
hasVideoChannels () {
|
|
|
|
return this.videoChannels.length !== 0
|
|
|
|
}
|
|
|
|
|
2021-03-31 08:59:52 +02:00
|
|
|
hasShowMoreDescription () {
|
|
|
|
return !this.accountDescriptionExpanded && this.accountDescriptionHTML.length > 100
|
|
|
|
}
|
|
|
|
|
2021-03-26 13:20:37 +01:00
|
|
|
private async onAccount (account: Account) {
|
|
|
|
this.accountFollowerTitle = $localize`${account.followersCount} direct account followers`
|
|
|
|
|
2020-07-09 15:54:24 +02:00
|
|
|
this.prependModerationActions = undefined
|
|
|
|
|
2021-03-26 13:20:37 +01:00
|
|
|
this.accountDescriptionHTML = await this.markdown.textMarkdownToHTML(account.description)
|
2020-07-09 15:54:24 +02:00
|
|
|
|
2021-03-26 13:20:37 +01:00
|
|
|
// After the markdown renderer to avoid layout changes
|
|
|
|
this.account = account
|
2020-07-09 15:54:24 +02:00
|
|
|
|
2021-03-26 13:20:37 +01:00
|
|
|
this.updateModerationActions()
|
|
|
|
this.loadUserIfNeeded(account)
|
|
|
|
this.loadAccountVideosCount()
|
2020-07-09 15:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private showReportModal () {
|
|
|
|
this.accountReportModal.show()
|
|
|
|
}
|
|
|
|
|
2021-03-26 13:20:37 +01:00
|
|
|
private loadUserIfNeeded (account: Account) {
|
2020-01-22 14:02:36 +01:00
|
|
|
if (!account.userId || !this.authService.isLoggedIn()) return
|
2018-10-05 16:56:14 +02:00
|
|
|
|
|
|
|
const user = this.authService.getUser()
|
|
|
|
if (user.hasRight(UserRight.MANAGE_USERS)) {
|
2021-08-17 11:27:47 +02:00
|
|
|
this.userService.getUser(account.userId)
|
|
|
|
.subscribe({
|
2021-08-17 14:42:53 +02:00
|
|
|
next: accountUser => {
|
|
|
|
this.accountUser = accountUser
|
|
|
|
},
|
2018-10-05 16:56:14 +02:00
|
|
|
|
2021-08-17 11:27:47 +02:00
|
|
|
error: err => this.notifier.error(err.message)
|
|
|
|
})
|
2018-10-05 16:56:14 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-26 13:20:37 +01:00
|
|
|
|
|
|
|
private updateModerationActions () {
|
|
|
|
if (!this.authService.isLoggedIn()) return
|
|
|
|
|
|
|
|
this.authService.userInformationLoaded.subscribe(
|
|
|
|
() => {
|
|
|
|
if (this.isManageable()) return
|
|
|
|
|
|
|
|
// It's not our account, we can report it
|
|
|
|
this.prependModerationActions = [
|
|
|
|
{
|
|
|
|
label: $localize`Report this account`,
|
|
|
|
handler: () => this.showReportModal()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadAccountVideosCount () {
|
|
|
|
this.videoService.getAccountVideos({
|
|
|
|
account: this.account,
|
|
|
|
videoPagination: {
|
|
|
|
currentPage: 1,
|
|
|
|
itemsPerPage: 0
|
|
|
|
},
|
|
|
|
sort: '-publishedAt'
|
2021-08-17 14:42:53 +02:00
|
|
|
}).subscribe(res => {
|
|
|
|
this.accountVideosCount = res.total
|
|
|
|
})
|
2021-03-26 13:20:37 +01:00
|
|
|
}
|
2018-04-24 15:10:54 +02:00
|
|
|
}
|