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