]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { Subscription } from 'rxjs' |
2 | import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators' | |
79bd2632 | 3 | import { Component, OnDestroy, OnInit } from '@angular/core' |
0626e7af | 4 | import { ActivatedRoute } from '@angular/router' |
67ed6552 C |
5 | import { AuthService, Notifier, RedirectService, RestExtractor, ScreenService, UserService } from '@app/core' |
6 | import { Account, AccountService, ListOverflowItem, VideoChannel, VideoChannelService } from '@app/shared/shared-main' | |
ee1d0dfb | 7 | import { I18n } from '@ngx-translate/i18n-polyfill' |
67ed6552 | 8 | import { User, UserRight } from '@shared/models' |
0626e7af C |
9 | |
10 | @Component({ | |
170726f5 C |
11 | templateUrl: './accounts.component.html', |
12 | styleUrls: [ './accounts.component.scss' ] | |
0626e7af | 13 | }) |
3baf9be2 | 14 | export class AccountsComponent implements OnInit, OnDestroy { |
6b738c7a | 15 | account: Account |
496b02e3 | 16 | accountUser: User |
a004ff17 | 17 | videoChannels: VideoChannel[] = [] |
24e7916c | 18 | links: ListOverflowItem[] = [] |
0626e7af | 19 | |
496b02e3 C |
20 | isAccountManageable = false |
21 | accountFollowerTitle = '' | |
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 | 33 | private authService: AuthService, |
937b7a6a | 34 | private screenService: ScreenService, |
ee1d0dfb | 35 | private i18n: I18n |
fef213ca C |
36 | ) { |
37 | } | |
0626e7af C |
38 | |
39 | ngOnInit () { | |
734a5ceb | 40 | this.routeSub = this.route.params |
fef213ca C |
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 | ) | |
24e7916c RK |
71 | |
72 | this.links = [ | |
14571f19 RK |
73 | { label: this.i18n('VIDEO CHANNELS'), routerLink: 'video-channels' }, |
74 | { label: this.i18n('VIDEOS'), routerLink: 'videos' }, | |
75 | { label: this.i18n('ABOUT'), routerLink: 'about' } | |
24e7916c | 76 | ] |
734a5ceb | 77 | } |
0626e7af | 78 | |
734a5ceb C |
79 | ngOnDestroy () { |
80 | if (this.routeSub) this.routeSub.unsubscribe() | |
0626e7af | 81 | } |
79bd2632 | 82 | |
a004ff17 RK |
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 | ||
937b7a6a RK |
90 | get isInSmallView () { |
91 | return this.screenService.isInSmallView() | |
92 | } | |
93 | ||
79bd2632 C |
94 | onUserChanged () { |
95 | this.getUserIfNeeded(this.account) | |
96 | } | |
97 | ||
98 | onUserDeleted () { | |
99 | this.redirectService.redirectToHomepage() | |
100 | } | |
101 | ||
ee1d0dfb C |
102 | activateCopiedMessage () { |
103 | this.notifier.success(this.i18n('Username copied')) | |
104 | } | |
105 | ||
a004ff17 | 106 | subscribersDisplayFor (count: number) { |
0b1de586 | 107 | return this.i18n('{count, plural, =1 {1 subscriber} other {{{count}} subscribers}}', { count }) |
a004ff17 RK |
108 | } |
109 | ||
79bd2632 | 110 | private getUserIfNeeded (account: Account) { |
496b02e3 | 111 | if (!account.userId || !this.authService.isLoggedIn()) return |
79bd2632 C |
112 | |
113 | const user = this.authService.getUser() | |
114 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
fef213ca C |
115 | this.userService.getUser(account.userId).subscribe( |
116 | accountUser => this.accountUser = accountUser, | |
79bd2632 | 117 | |
496b02e3 C |
118 | err => this.notifier.error(err.message) |
119 | ) | |
79bd2632 C |
120 | } |
121 | } | |
0626e7af | 122 | } |