]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnDestroy, OnInit } from '@angular/core' | |
2 | import { ActivatedRoute } from '@angular/router' | |
3 | import { AccountService } from '@app/shared/account/account.service' | |
4 | import { Account } from '@app/shared/account/account.model' | |
5 | import { RestExtractor, UserService } from '@app/shared' | |
6 | import { catchError, distinctUntilChanged, first, map, switchMap, tap } from 'rxjs/operators' | |
7 | import { forkJoin, Subscription } from 'rxjs' | |
8 | import { AuthService, Notifier, RedirectService } from '@app/core' | |
9 | import { User, UserRight } from '../../../../shared' | |
10 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
11 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | |
12 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | |
13 | ||
14 | @Component({ | |
15 | templateUrl: './accounts.component.html', | |
16 | styleUrls: [ './accounts.component.scss' ] | |
17 | }) | |
18 | export class AccountsComponent implements OnInit, OnDestroy { | |
19 | account: Account | |
20 | accountUser: User | |
21 | videoChannels: VideoChannel[] = [] | |
22 | ||
23 | isAccountManageable = false | |
24 | accountFollowerTitle = '' | |
25 | ||
26 | private routeSub: Subscription | |
27 | ||
28 | constructor ( | |
29 | private route: ActivatedRoute, | |
30 | private userService: UserService, | |
31 | private accountService: AccountService, | |
32 | private videoChannelService: VideoChannelService, | |
33 | private notifier: Notifier, | |
34 | private restExtractor: RestExtractor, | |
35 | private redirectService: RedirectService, | |
36 | private authService: AuthService, | |
37 | private i18n: I18n | |
38 | ) {} | |
39 | ||
40 | ngOnInit () { | |
41 | this.routeSub = this.route.params | |
42 | .pipe( | |
43 | map(params => params[ 'accountId' ]), | |
44 | distinctUntilChanged(), | |
45 | switchMap(accountId => this.accountService.getAccount(accountId)), | |
46 | tap(account => { | |
47 | this.account = account | |
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 | ||
56 | this.getUserIfNeeded(account) | |
57 | }), | |
58 | switchMap(account => this.videoChannelService.listAccountVideoChannels(account)), | |
59 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])) | |
60 | ) | |
61 | .subscribe( | |
62 | videoChannels => this.videoChannels = videoChannels.data, | |
63 | ||
64 | err => this.notifier.error(err.message) | |
65 | ) | |
66 | } | |
67 | ||
68 | ngOnDestroy () { | |
69 | if (this.routeSub) this.routeSub.unsubscribe() | |
70 | } | |
71 | ||
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 | ||
79 | onUserChanged () { | |
80 | this.getUserIfNeeded(this.account) | |
81 | } | |
82 | ||
83 | onUserDeleted () { | |
84 | this.redirectService.redirectToHomepage() | |
85 | } | |
86 | ||
87 | activateCopiedMessage () { | |
88 | this.notifier.success(this.i18n('Username copied')) | |
89 | } | |
90 | ||
91 | subscribersDisplayFor (count: number) { | |
92 | return this.i18n(`{count, plural, =1 {1 subscriber} other {${count} subscribers}}`, { count }) | |
93 | } | |
94 | ||
95 | private getUserIfNeeded (account: Account) { | |
96 | if (!account.userId || !this.authService.isLoggedIn()) return | |
97 | ||
98 | const user = this.authService.getUser() | |
99 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
100 | forkJoin([ | |
101 | this.userService.getUser(account.userId), | |
102 | this.authService.userInformationLoaded.pipe(first()) | |
103 | ]).subscribe( | |
104 | ([ accountUser ]) => this.accountUser = accountUser, | |
105 | ||
106 | err => this.notifier.error(err.message) | |
107 | ) | |
108 | } | |
109 | } | |
110 | } |