PeerTube/client/src/app/+accounts/accounts.component.ts

221 lines
6.2 KiB
TypeScript
Raw Normal View History

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'
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
})
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
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
videoChannels: VideoChannel[] = []
2021-03-26 13:20: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>[]
private routeSub: Subscription
2018-04-24 15:10:54 +02:00
constructor (
private route: ActivatedRoute,
private userService: UserService,
2018-05-31 11:35:01 +02:00
private accountService: AccountService,
private videoChannelService: VideoChannelService,
private notifier: Notifier,
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,
private screenService: ScreenService
2020-01-22 15:01:38 +01:00
) {
}
2018-04-24 15:10:54 +02:00
ngOnInit () {
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 })),
catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
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)
})
this.links = [
{ label: $localize`CHANNELS`, routerLink: 'video-channels' },
2021-03-26 13:20:37 +01:00
{ label: $localize`VIDEOS`, routerLink: 'videos' }
]
}
2018-04-24 15:10:54 +02:00
ngOnDestroy () {
if (this.routeSub) this.routeSub.unsubscribe()
2018-04-24 15:10:54 +02:00
}
2021-03-26 13:20:37 +01:00
naiveAggregatedSubscribers () {
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 () {
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
}
onUserChanged () {
2021-03-26 13:20:37 +01:00
this.loadUserIfNeeded(this.account)
}
onUserDeleted () {
this.redirectService.redirectToHomepage()
}
2019-06-19 15:14:13 +02:00
activateCopiedMessage () {
this.notifier.success($localize`Username copied`)
2019-06-19 15:14:13 +02:00
}
subscribersDisplayFor (count: number) {
if (count === 1) return $localize`1 subscriber`
return $localize`${count} subscribers`
}
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
}
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
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
},
2021-08-17 11:27:47 +02:00
error: err => this.notifier.error(err.message)
})
}
}
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
}