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