]>
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' |
288c78ea RK |
6 | import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators' |
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' | |
24e7916c | 13 | import { ListOverflowItem } from '@app/shared/misc/list-overflow.component' |
0626e7af C |
14 | |
15 | @Component({ | |
170726f5 C |
16 | templateUrl: './accounts.component.html', |
17 | styleUrls: [ './accounts.component.scss' ] | |
0626e7af | 18 | }) |
3baf9be2 | 19 | export class AccountsComponent implements OnInit, OnDestroy { |
6b738c7a | 20 | account: Account |
496b02e3 | 21 | accountUser: User |
a004ff17 | 22 | videoChannels: VideoChannel[] = [] |
24e7916c | 23 | links: ListOverflowItem[] = [] |
0626e7af | 24 | |
496b02e3 C |
25 | isAccountManageable = false |
26 | accountFollowerTitle = '' | |
27 | ||
734a5ceb C |
28 | private routeSub: Subscription |
29 | ||
0626e7af C |
30 | constructor ( |
31 | private route: ActivatedRoute, | |
79bd2632 | 32 | private userService: UserService, |
a51bad1a | 33 | private accountService: AccountService, |
41eb700f | 34 | private videoChannelService: VideoChannelService, |
f8b2c1b4 | 35 | private notifier: Notifier, |
79bd2632 C |
36 | private restExtractor: RestExtractor, |
37 | private redirectService: RedirectService, | |
ee1d0dfb C |
38 | private authService: AuthService, |
39 | private i18n: I18n | |
fef213ca C |
40 | ) { |
41 | } | |
0626e7af C |
42 | |
43 | ngOnInit () { | |
734a5ceb | 44 | this.routeSub = this.route.params |
fef213ca C |
45 | .pipe( |
46 | map(params => params[ 'accountId' ]), | |
47 | distinctUntilChanged(), | |
48 | switchMap(accountId => this.accountService.getAccount(accountId)), | |
49 | tap(account => { | |
50 | this.account = account | |
51 | ||
52 | if (this.authService.isLoggedIn()) { | |
53 | this.authService.userInformationLoaded.subscribe( | |
54 | () => { | |
55 | this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id | |
56 | ||
57 | this.accountFollowerTitle = this.i18n( | |
58 | '{{followers}} direct account followers', | |
59 | { followers: this.subscribersDisplayFor(account.followersCount) } | |
60 | ) | |
61 | } | |
62 | ) | |
63 | } | |
64 | ||
65 | this.getUserIfNeeded(account) | |
66 | }), | |
67 | switchMap(account => this.videoChannelService.listAccountVideoChannels(account)), | |
68 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])) | |
69 | ) | |
70 | .subscribe( | |
71 | videoChannels => this.videoChannels = videoChannels.data, | |
72 | ||
73 | err => this.notifier.error(err.message) | |
74 | ) | |
24e7916c RK |
75 | |
76 | this.links = [ | |
14571f19 RK |
77 | { label: this.i18n('VIDEO CHANNELS'), routerLink: 'video-channels' }, |
78 | { label: this.i18n('VIDEOS'), routerLink: 'videos' }, | |
79 | { label: this.i18n('ABOUT'), routerLink: 'about' } | |
24e7916c | 80 | ] |
734a5ceb | 81 | } |
0626e7af | 82 | |
734a5ceb C |
83 | ngOnDestroy () { |
84 | if (this.routeSub) this.routeSub.unsubscribe() | |
0626e7af | 85 | } |
79bd2632 | 86 | |
a004ff17 RK |
87 | get naiveAggregatedSubscribers () { |
88 | return this.videoChannels.reduce( | |
89 | (acc, val) => acc + val.followersCount, | |
90 | this.account.followersCount // accumulator starts with the base number of subscribers the account has | |
91 | ) | |
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 | } |