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