]>
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, map, switchMap, tap } from 'rxjs/operators' | |
7 | import { 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 | import { ListOverflowItem } from '@app/shared/misc/list-overflow.component' | |
14 | import { ScreenService } from '@app/shared/misc/screen.service' | |
15 | ||
16 | @Component({ | |
17 | templateUrl: './accounts.component.html', | |
18 | styleUrls: [ './accounts.component.scss' ] | |
19 | }) | |
20 | export class AccountsComponent implements OnInit, OnDestroy { | |
21 | account: Account | |
22 | accountUser: User | |
23 | videoChannels: VideoChannel[] = [] | |
24 | links: ListOverflowItem[] = [] | |
25 | ||
26 | isAccountManageable = false | |
27 | accountFollowerTitle = '' | |
28 | ||
29 | private routeSub: Subscription | |
30 | ||
31 | constructor ( | |
32 | private route: ActivatedRoute, | |
33 | private userService: UserService, | |
34 | private accountService: AccountService, | |
35 | private videoChannelService: VideoChannelService, | |
36 | private notifier: Notifier, | |
37 | private restExtractor: RestExtractor, | |
38 | private redirectService: RedirectService, | |
39 | private authService: AuthService, | |
40 | private screenService: ScreenService, | |
41 | private i18n: I18n | |
42 | ) { | |
43 | } | |
44 | ||
45 | ngOnInit () { | |
46 | this.routeSub = this.route.params | |
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 | ) | |
77 | ||
78 | this.links = [ | |
79 | { label: this.i18n('VIDEO CHANNELS'), routerLink: 'video-channels' }, | |
80 | { label: this.i18n('VIDEOS'), routerLink: 'videos' }, | |
81 | { label: this.i18n('ABOUT'), routerLink: 'about' } | |
82 | ] | |
83 | } | |
84 | ||
85 | ngOnDestroy () { | |
86 | if (this.routeSub) this.routeSub.unsubscribe() | |
87 | } | |
88 | ||
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 | ||
96 | get isInSmallView () { | |
97 | return this.screenService.isInSmallView() | |
98 | } | |
99 | ||
100 | onUserChanged () { | |
101 | this.getUserIfNeeded(this.account) | |
102 | } | |
103 | ||
104 | onUserDeleted () { | |
105 | this.redirectService.redirectToHomepage() | |
106 | } | |
107 | ||
108 | activateCopiedMessage () { | |
109 | this.notifier.success(this.i18n('Username copied')) | |
110 | } | |
111 | ||
112 | subscribersDisplayFor (count: number) { | |
113 | return this.i18n('{count, plural, =1 {1 subscriber} other {{{count}} subscribers}}', { count }) | |
114 | } | |
115 | ||
116 | private getUserIfNeeded (account: Account) { | |
117 | if (!account.userId || !this.authService.isLoggedIn()) return | |
118 | ||
119 | const user = this.authService.getUser() | |
120 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
121 | this.userService.getUser(account.userId).subscribe( | |
122 | accountUser => this.accountUser = accountUser, | |
123 | ||
124 | err => this.notifier.error(err.message) | |
125 | ) | |
126 | } | |
127 | } | |
128 | } |