2020-06-23 07:10:17 -05:00
|
|
|
import { Subscription } from 'rxjs'
|
|
|
|
import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
|
2020-07-09 08:54:24 -05:00
|
|
|
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
|
2018-04-24 08:10:54 -05:00
|
|
|
import { ActivatedRoute } from '@angular/router'
|
2020-06-23 07:10:17 -05:00
|
|
|
import { AuthService, Notifier, RedirectService, RestExtractor, ScreenService, UserService } from '@app/core'
|
2020-07-09 08:54:24 -05:00
|
|
|
import { Account, AccountService, DropdownAction, ListOverflowItem, VideoChannel, VideoChannelService } from '@app/shared/shared-main'
|
|
|
|
import { AccountReportComponent } from '@app/shared/shared-moderation'
|
2019-06-19 08:14:13 -05:00
|
|
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
2020-06-23 07:10:17 -05:00
|
|
|
import { User, UserRight } from '@shared/models'
|
2018-04-24 08:10:54 -05:00
|
|
|
|
|
|
|
@Component({
|
2018-04-25 09:56:13 -05:00
|
|
|
templateUrl: './accounts.component.html',
|
|
|
|
styleUrls: [ './accounts.component.scss' ]
|
2018-04-24 08:10:54 -05:00
|
|
|
})
|
2018-06-07 07:58:41 -05:00
|
|
|
export class AccountsComponent implements OnInit, OnDestroy {
|
2020-07-09 08:54:24 -05:00
|
|
|
@ViewChild('accountReportModal') accountReportModal: AccountReportComponent
|
|
|
|
|
2018-04-25 03:21:38 -05:00
|
|
|
account: Account
|
2020-01-22 07:02:36 -06:00
|
|
|
accountUser: User
|
2020-01-10 09:22:55 -06:00
|
|
|
videoChannels: VideoChannel[] = []
|
2020-02-05 13:54:37 -06:00
|
|
|
links: ListOverflowItem[] = []
|
2018-04-24 08:10:54 -05:00
|
|
|
|
2020-01-22 07:02:36 -06:00
|
|
|
isAccountManageable = false
|
|
|
|
accountFollowerTitle = ''
|
|
|
|
|
2020-07-09 08:54:24 -05:00
|
|
|
prependModerationActions: DropdownAction<any>[]
|
|
|
|
|
2018-06-07 04:19:26 -05:00
|
|
|
private routeSub: Subscription
|
|
|
|
|
2018-04-24 08:10:54 -05:00
|
|
|
constructor (
|
|
|
|
private route: ActivatedRoute,
|
2018-10-05 09:56:14 -05:00
|
|
|
private userService: UserService,
|
2018-05-31 04:35:01 -05:00
|
|
|
private accountService: AccountService,
|
2020-01-07 16:51:14 -06:00
|
|
|
private videoChannelService: VideoChannelService,
|
2018-12-19 09:04:34 -06:00
|
|
|
private notifier: Notifier,
|
2018-10-05 09:56:14 -05:00
|
|
|
private restExtractor: RestExtractor,
|
|
|
|
private redirectService: RedirectService,
|
2019-06-19 08:14:13 -05:00
|
|
|
private authService: AuthService,
|
2020-03-09 04:22:11 -05:00
|
|
|
private screenService: ScreenService,
|
2019-06-19 08:14:13 -05:00
|
|
|
private i18n: I18n
|
2020-01-22 08:01:38 -06:00
|
|
|
) {
|
|
|
|
}
|
2018-04-24 08:10:54 -05:00
|
|
|
|
|
|
|
ngOnInit () {
|
2018-06-07 04:19:26 -05:00
|
|
|
this.routeSub = this.route.params
|
2020-01-22 08:01:38 -06:00
|
|
|
.pipe(
|
|
|
|
map(params => params[ 'accountId' ]),
|
|
|
|
distinctUntilChanged(),
|
|
|
|
switchMap(accountId => this.accountService.getAccount(accountId)),
|
2020-07-09 08:54:24 -05:00
|
|
|
tap(account => this.onAccount(account)),
|
2020-01-22 08:01:38 -06:00
|
|
|
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 13:54:37 -06:00
|
|
|
|
|
|
|
this.links = [
|
2020-02-21 09:50:12 -06:00
|
|
|
{ label: this.i18n('VIDEO CHANNELS'), routerLink: 'video-channels' },
|
|
|
|
{ label: this.i18n('VIDEOS'), routerLink: 'videos' },
|
|
|
|
{ label: this.i18n('ABOUT'), routerLink: 'about' }
|
2020-02-05 13:54:37 -06:00
|
|
|
]
|
2018-06-07 04:19:26 -05:00
|
|
|
}
|
2018-04-24 08:10:54 -05:00
|
|
|
|
2018-06-07 04:19:26 -05:00
|
|
|
ngOnDestroy () {
|
|
|
|
if (this.routeSub) this.routeSub.unsubscribe()
|
2018-04-24 08:10:54 -05:00
|
|
|
}
|
2018-10-05 09:56:14 -05:00
|
|
|
|
2020-01-10 09:22:55 -06: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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-09 04:22:11 -05:00
|
|
|
get isInSmallView () {
|
|
|
|
return this.screenService.isInSmallView()
|
|
|
|
}
|
|
|
|
|
2018-10-05 09:56:14 -05:00
|
|
|
onUserChanged () {
|
|
|
|
this.getUserIfNeeded(this.account)
|
|
|
|
}
|
|
|
|
|
|
|
|
onUserDeleted () {
|
|
|
|
this.redirectService.redirectToHomepage()
|
|
|
|
}
|
|
|
|
|
2019-06-19 08:14:13 -05:00
|
|
|
activateCopiedMessage () {
|
|
|
|
this.notifier.success(this.i18n('Username copied'))
|
|
|
|
}
|
|
|
|
|
2020-01-10 09:22:55 -06:00
|
|
|
subscribersDisplayFor (count: number) {
|
2020-02-04 10:13:47 -06:00
|
|
|
return this.i18n('{count, plural, =1 {1 subscriber} other {{{count}} subscribers}}', { count })
|
2020-01-10 09:22:55 -06:00
|
|
|
}
|
|
|
|
|
2020-07-09 08:54:24 -05:00
|
|
|
private onAccount (account: Account) {
|
|
|
|
this.prependModerationActions = undefined
|
|
|
|
|
|
|
|
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) }
|
|
|
|
)
|
|
|
|
|
|
|
|
// It's not our account, we can report it
|
|
|
|
if (!this.isAccountManageable) {
|
|
|
|
this.prependModerationActions = [
|
|
|
|
{
|
|
|
|
label: this.i18n('Report account'),
|
|
|
|
handler: () => this.showReportModal()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.getUserIfNeeded(account)
|
|
|
|
}
|
|
|
|
|
|
|
|
private showReportModal () {
|
|
|
|
this.accountReportModal.show()
|
|
|
|
}
|
|
|
|
|
2018-10-05 09:56:14 -05:00
|
|
|
private getUserIfNeeded (account: Account) {
|
2020-01-22 07:02:36 -06:00
|
|
|
if (!account.userId || !this.authService.isLoggedIn()) return
|
2018-10-05 09:56:14 -05:00
|
|
|
|
|
|
|
const user = this.authService.getUser()
|
|
|
|
if (user.hasRight(UserRight.MANAGE_USERS)) {
|
2020-01-22 08:01:38 -06:00
|
|
|
this.userService.getUser(account.userId).subscribe(
|
|
|
|
accountUser => this.accountUser = accountUser,
|
2018-10-05 09:56:14 -05:00
|
|
|
|
2020-01-22 07:02:36 -06:00
|
|
|
err => this.notifier.error(err.message)
|
|
|
|
)
|
2018-10-05 09:56:14 -05:00
|
|
|
}
|
|
|
|
}
|
2018-04-24 08:10:54 -05:00
|
|
|
}
|