]>
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 C |
5 | import { RestExtractor, UserService } from '@app/shared' |
6 | import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators' | |
734a5ceb | 7 | import { 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 |
79bd2632 | 20 | user: User |
a004ff17 | 21 | videoChannels: VideoChannel[] = [] |
0626e7af | 22 | |
734a5ceb C |
23 | private routeSub: Subscription |
24 | ||
0626e7af C |
25 | constructor ( |
26 | private route: ActivatedRoute, | |
79bd2632 | 27 | private userService: UserService, |
a51bad1a | 28 | private accountService: AccountService, |
41eb700f | 29 | private videoChannelService: VideoChannelService, |
f8b2c1b4 | 30 | private notifier: Notifier, |
79bd2632 C |
31 | private restExtractor: RestExtractor, |
32 | private redirectService: RedirectService, | |
ee1d0dfb C |
33 | private authService: AuthService, |
34 | private i18n: I18n | |
0626e7af C |
35 | ) {} |
36 | ||
37 | ngOnInit () { | |
734a5ceb C |
38 | this.routeSub = this.route.params |
39 | .pipe( | |
40 | map(params => params[ 'accountId' ]), | |
41 | distinctUntilChanged(), | |
42 | switchMap(accountId => this.accountService.getAccount(accountId)), | |
9270ccf6 RK |
43 | tap(account => { |
44 | this.account = account | |
45 | this.getUserIfNeeded(account) | |
46 | }), | |
47 | switchMap(account => this.videoChannelService.listAccountVideoChannels(account)), | |
734a5ceb C |
48 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])) |
49 | ) | |
79bd2632 | 50 | .subscribe( |
9270ccf6 | 51 | videoChannels => this.videoChannels = videoChannels.data, |
79bd2632 | 52 | |
f8b2c1b4 | 53 | err => this.notifier.error(err.message) |
79bd2632 | 54 | ) |
734a5ceb | 55 | } |
0626e7af | 56 | |
734a5ceb C |
57 | ngOnDestroy () { |
58 | if (this.routeSub) this.routeSub.unsubscribe() | |
0626e7af | 59 | } |
79bd2632 | 60 | |
a004ff17 RK |
61 | get naiveAggregatedSubscribers () { |
62 | return this.videoChannels.reduce( | |
63 | (acc, val) => acc + val.followersCount, | |
64 | this.account.followersCount // accumulator starts with the base number of subscribers the account has | |
65 | ) | |
66 | } | |
67 | ||
79bd2632 C |
68 | onUserChanged () { |
69 | this.getUserIfNeeded(this.account) | |
70 | } | |
71 | ||
72 | onUserDeleted () { | |
73 | this.redirectService.redirectToHomepage() | |
74 | } | |
75 | ||
ee1d0dfb C |
76 | activateCopiedMessage () { |
77 | this.notifier.success(this.i18n('Username copied')) | |
78 | } | |
79 | ||
a004ff17 RK |
80 | subscribersDisplayFor (count: number) { |
81 | return this.i18n(`{count, plural, =1 {1 subscriber} other {${count} subscribers}}`, { count }) | |
82 | } | |
83 | ||
79bd2632 C |
84 | private getUserIfNeeded (account: Account) { |
85 | if (!account.userId) return | |
86 | if (!this.authService.isLoggedIn()) return | |
87 | ||
88 | const user = this.authService.getUser() | |
89 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
90 | this.userService.getUser(account.userId) | |
91 | .subscribe( | |
92 | user => this.user = user, | |
93 | ||
f8b2c1b4 | 94 | err => this.notifier.error(err.message) |
79bd2632 C |
95 | ) |
96 | } | |
97 | } | |
0626e7af | 98 | } |