]>
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 | |
fef213ca C |
38 | ) { |
39 | } | |
0626e7af C |
40 | |
41 | ngOnInit () { | |
734a5ceb | 42 | this.routeSub = this.route.params |
fef213ca C |
43 | .pipe( |
44 | map(params => params[ 'accountId' ]), | |
45 | distinctUntilChanged(), | |
46 | switchMap(accountId => this.accountService.getAccount(accountId)), | |
47 | tap(account => { | |
48 | this.account = account | |
49 | ||
50 | if (this.authService.isLoggedIn()) { | |
51 | this.authService.userInformationLoaded.subscribe( | |
52 | () => { | |
53 | this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id | |
54 | ||
55 | this.accountFollowerTitle = this.i18n( | |
56 | '{{followers}} direct account followers', | |
57 | { followers: this.subscribersDisplayFor(account.followersCount) } | |
58 | ) | |
59 | } | |
60 | ) | |
61 | } | |
62 | ||
63 | this.getUserIfNeeded(account) | |
64 | }), | |
65 | switchMap(account => this.videoChannelService.listAccountVideoChannels(account)), | |
66 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])) | |
67 | ) | |
68 | .subscribe( | |
69 | videoChannels => this.videoChannels = videoChannels.data, | |
70 | ||
71 | err => this.notifier.error(err.message) | |
72 | ) | |
734a5ceb | 73 | } |
0626e7af | 74 | |
734a5ceb C |
75 | ngOnDestroy () { |
76 | if (this.routeSub) this.routeSub.unsubscribe() | |
0626e7af | 77 | } |
79bd2632 | 78 | |
a004ff17 RK |
79 | get naiveAggregatedSubscribers () { |
80 | return this.videoChannels.reduce( | |
81 | (acc, val) => acc + val.followersCount, | |
82 | this.account.followersCount // accumulator starts with the base number of subscribers the account has | |
83 | ) | |
84 | } | |
85 | ||
79bd2632 C |
86 | onUserChanged () { |
87 | this.getUserIfNeeded(this.account) | |
88 | } | |
89 | ||
90 | onUserDeleted () { | |
91 | this.redirectService.redirectToHomepage() | |
92 | } | |
93 | ||
ee1d0dfb C |
94 | activateCopiedMessage () { |
95 | this.notifier.success(this.i18n('Username copied')) | |
96 | } | |
97 | ||
a004ff17 | 98 | subscribersDisplayFor (count: number) { |
0b1de586 | 99 | return this.i18n('{count, plural, =1 {1 subscriber} other {{{count}} subscribers}}', { count }) |
a004ff17 RK |
100 | } |
101 | ||
79bd2632 | 102 | private getUserIfNeeded (account: Account) { |
496b02e3 | 103 | if (!account.userId || !this.authService.isLoggedIn()) return |
79bd2632 C |
104 | |
105 | const user = this.authService.getUser() | |
106 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
fef213ca C |
107 | this.userService.getUser(account.userId).subscribe( |
108 | accountUser => this.accountUser = accountUser, | |
79bd2632 | 109 | |
496b02e3 C |
110 | err => this.notifier.error(err.message) |
111 | ) | |
79bd2632 C |
112 | } |
113 | } | |
0626e7af | 114 | } |