2018-10-05 16:56:14 +02:00
|
|
|
import { Component, OnDestroy, OnInit } from '@angular/core'
|
2018-04-24 15:10:54 +02:00
|
|
|
import { ActivatedRoute } from '@angular/router'
|
|
|
|
import { AccountService } from '@app/shared/account/account.service'
|
|
|
|
import { Account } from '@app/shared/account/account.model'
|
2018-10-05 16:56:14 +02:00
|
|
|
import { RestExtractor, UserService } from '@app/shared'
|
2020-03-05 23:49:12 +01:00
|
|
|
import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
|
|
|
|
import { Subscription } from 'rxjs'
|
2018-12-19 16:04:34 +01:00
|
|
|
import { AuthService, Notifier, RedirectService } from '@app/core'
|
2018-10-05 16:56:14 +02:00
|
|
|
import { User, UserRight } from '../../../../shared'
|
2019-06-19 15:14:13 +02:00
|
|
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
2020-01-07 23:51:14 +01:00
|
|
|
import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
|
|
|
|
import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
|
2020-02-05 20:54:37 +01:00
|
|
|
import { ListOverflowItem } from '@app/shared/misc/list-overflow.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 {
|
2018-04-25 10:21:38 +02:00
|
|
|
account: Account
|
2020-01-22 14:02:36 +01:00
|
|
|
accountUser: User
|
2020-01-10 16:22:55 +01:00
|
|
|
videoChannels: VideoChannel[] = []
|
2020-02-05 20:54:37 +01:00
|
|
|
links: ListOverflowItem[] = []
|
2018-04-24 15:10:54 +02:00
|
|
|
|
2020-01-22 14:02:36 +01:00
|
|
|
isAccountManageable = false
|
|
|
|
accountFollowerTitle = ''
|
|
|
|
|
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,
|
|
|
|
private i18n: I18n
|
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(
|
|
|
|
map(params => params[ 'accountId' ]),
|
|
|
|
distinctUntilChanged(),
|
|
|
|
switchMap(accountId => this.accountService.getAccount(accountId)),
|
|
|
|
tap(account => {
|
|
|
|
this.account = account
|
|
|
|
|
|
|
|
if (this.authService.isLoggedIn()) {
|
|
|
|
this.authService.userInformationLoaded.subscribe(
|
|
|
|
() => {
|
|
|
|
this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id
|
|
|
|
|
|
|
|
this.accountFollowerTitle = this.i18n(
|
|
|
|
'{{followers}} direct account followers',
|
|
|
|
{ followers: this.subscribersDisplayFor(account.followersCount) }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getUserIfNeeded(account)
|
|
|
|
}),
|
|
|
|
switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
|
|
|
|
catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
|
|
|
|
)
|
|
|
|
.subscribe(
|
|
|
|
videoChannels => this.videoChannels = videoChannels.data,
|
|
|
|
|
|
|
|
err => this.notifier.error(err.message)
|
|
|
|
)
|
2020-02-05 20:54:37 +01:00
|
|
|
|
|
|
|
this.links = [
|
2020-02-21 16:50:12 +01:00
|
|
|
{ label: this.i18n('VIDEO CHANNELS'), routerLink: 'video-channels' },
|
|
|
|
{ label: this.i18n('VIDEOS'), routerLink: 'videos' },
|
|
|
|
{ label: this.i18n('ABOUT'), routerLink: 'about' }
|
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
|
|
|
|
2020-01-10 16:22:55 +01:00
|
|
|
get naiveAggregatedSubscribers () {
|
|
|
|
return this.videoChannels.reduce(
|
|
|
|
(acc, val) => acc + val.followersCount,
|
|
|
|
this.account.followersCount // accumulator starts with the base number of subscribers the account has
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:56:14 +02:00
|
|
|
onUserChanged () {
|
|
|
|
this.getUserIfNeeded(this.account)
|
|
|
|
}
|
|
|
|
|
|
|
|
onUserDeleted () {
|
|
|
|
this.redirectService.redirectToHomepage()
|
|
|
|
}
|
|
|
|
|
2019-06-19 15:14:13 +02:00
|
|
|
activateCopiedMessage () {
|
|
|
|
this.notifier.success(this.i18n('Username copied'))
|
|
|
|
}
|
|
|
|
|
2020-01-10 16:22:55 +01:00
|
|
|
subscribersDisplayFor (count: number) {
|
2020-02-04 17:13:47 +01:00
|
|
|
return this.i18n('{count, plural, =1 {1 subscriber} other {{{count}} subscribers}}', { count })
|
2020-01-10 16:22:55 +01:00
|
|
|
}
|
|
|
|
|
2018-10-05 16:56:14 +02:00
|
|
|
private getUserIfNeeded (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)) {
|
2020-01-22 15:01:38 +01:00
|
|
|
this.userService.getUser(account.userId).subscribe(
|
|
|
|
accountUser => this.accountUser = accountUser,
|
2018-10-05 16:56:14 +02:00
|
|
|
|
2020-01-22 14:02:36 +01:00
|
|
|
err => this.notifier.error(err.message)
|
|
|
|
)
|
2018-10-05 16:56:14 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-24 15:10:54 +02:00
|
|
|
}
|