]>
Commit | Line | Data |
---|---|---|
79bd2632 | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
0626e7af C |
2 | import { ActivatedRoute } from '@angular/router' |
3 | import { AccountService } from '@app/shared/account/account.service' | |
4 | import { Account } from '@app/shared/account/account.model' | |
79bd2632 | 5 | import { RestExtractor, UserService } from '@app/shared' |
496b02e3 C |
6 | import { catchError, distinctUntilChanged, first, map, switchMap, tap } from 'rxjs/operators' |
7 | import { forkJoin, Subscription } from 'rxjs' | |
f8b2c1b4 | 8 | import { AuthService, Notifier, RedirectService } from '@app/core' |
79bd2632 | 9 | import { User, UserRight } from '../../../../shared' |
ee1d0dfb | 10 | import { I18n } from '@ngx-translate/i18n-polyfill' |
41eb700f RK |
11 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' |
12 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | |
0626e7af C |
13 | |
14 | @Component({ | |
170726f5 C |
15 | templateUrl: './accounts.component.html', |
16 | styleUrls: [ './accounts.component.scss' ] | |
0626e7af | 17 | }) |
3baf9be2 | 18 | export class AccountsComponent implements OnInit, OnDestroy { |
6b738c7a | 19 | account: Account |
496b02e3 | 20 | accountUser: User |
a004ff17 | 21 | videoChannels: VideoChannel[] = [] |
0626e7af | 22 | |
496b02e3 C |
23 | isAccountManageable = false |
24 | accountFollowerTitle = '' | |
25 | ||
734a5ceb C |
26 | private routeSub: Subscription |
27 | ||
0626e7af C |
28 | constructor ( |
29 | private route: ActivatedRoute, | |
79bd2632 | 30 | private userService: UserService, |
a51bad1a | 31 | private accountService: AccountService, |
41eb700f | 32 | private videoChannelService: VideoChannelService, |
f8b2c1b4 | 33 | private notifier: Notifier, |
79bd2632 C |
34 | private restExtractor: RestExtractor, |
35 | private redirectService: RedirectService, | |
ee1d0dfb C |
36 | private authService: AuthService, |
37 | private i18n: I18n | |
0626e7af C |
38 | ) {} |
39 | ||
40 | ngOnInit () { | |
734a5ceb C |
41 | this.routeSub = this.route.params |
42 | .pipe( | |
43 | map(params => params[ 'accountId' ]), | |
44 | distinctUntilChanged(), | |
45 | switchMap(accountId => this.accountService.getAccount(accountId)), | |
9270ccf6 RK |
46 | tap(account => { |
47 | this.account = account | |
496b02e3 C |
48 | |
49 | this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id | |
50 | ||
51 | this.accountFollowerTitle = this.i18n( | |
52 | '{{followers}} direct account followers', | |
53 | { followers: this.subscribersDisplayFor(account.followersCount) } | |
54 | ) | |
55 | ||
9270ccf6 RK |
56 | this.getUserIfNeeded(account) |
57 | }), | |
58 | switchMap(account => this.videoChannelService.listAccountVideoChannels(account)), | |
734a5ceb C |
59 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])) |
60 | ) | |
79bd2632 | 61 | .subscribe( |
9270ccf6 | 62 | videoChannels => this.videoChannels = videoChannels.data, |
79bd2632 | 63 | |
f8b2c1b4 | 64 | err => this.notifier.error(err.message) |
79bd2632 | 65 | ) |
734a5ceb | 66 | } |
0626e7af | 67 | |
734a5ceb C |
68 | ngOnDestroy () { |
69 | if (this.routeSub) this.routeSub.unsubscribe() | |
0626e7af | 70 | } |
79bd2632 | 71 | |
a004ff17 RK |
72 | get naiveAggregatedSubscribers () { |
73 | return this.videoChannels.reduce( | |
74 | (acc, val) => acc + val.followersCount, | |
75 | this.account.followersCount // accumulator starts with the base number of subscribers the account has | |
76 | ) | |
77 | } | |
78 | ||
79bd2632 C |
79 | onUserChanged () { |
80 | this.getUserIfNeeded(this.account) | |
81 | } | |
82 | ||
83 | onUserDeleted () { | |
84 | this.redirectService.redirectToHomepage() | |
85 | } | |
86 | ||
ee1d0dfb C |
87 | activateCopiedMessage () { |
88 | this.notifier.success(this.i18n('Username copied')) | |
89 | } | |
90 | ||
a004ff17 RK |
91 | subscribersDisplayFor (count: number) { |
92 | return this.i18n(`{count, plural, =1 {1 subscriber} other {${count} subscribers}}`, { count }) | |
93 | } | |
94 | ||
79bd2632 | 95 | private getUserIfNeeded (account: Account) { |
496b02e3 | 96 | if (!account.userId || !this.authService.isLoggedIn()) return |
79bd2632 C |
97 | |
98 | const user = this.authService.getUser() | |
99 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
496b02e3 C |
100 | forkJoin([ |
101 | this.userService.getUser(account.userId), | |
102 | this.authService.userInformationLoaded.pipe(first()) | |
103 | ]).subscribe( | |
104 | ([ accountUser ]) => this.accountUser = accountUser, | |
79bd2632 | 105 | |
496b02e3 C |
106 | err => this.notifier.error(err.message) |
107 | ) | |
79bd2632 C |
108 | } |
109 | } | |
0626e7af | 110 | } |