]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { Subscription } from 'rxjs' |
2 | import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators' | |
cfde28ba | 3 | import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core' |
0626e7af | 4 | import { ActivatedRoute } from '@angular/router' |
67ed6552 | 5 | import { AuthService, Notifier, RedirectService, RestExtractor, ScreenService, UserService } from '@app/core' |
cfde28ba C |
6 | import { Account, AccountService, DropdownAction, ListOverflowItem, VideoChannel, VideoChannelService } from '@app/shared/shared-main' |
7 | import { AccountReportComponent } from '@app/shared/shared-moderation' | |
ee1d0dfb | 8 | import { I18n } from '@ngx-translate/i18n-polyfill' |
67ed6552 | 9 | import { User, UserRight } from '@shared/models' |
0626e7af C |
10 | |
11 | @Component({ | |
170726f5 C |
12 | templateUrl: './accounts.component.html', |
13 | styleUrls: [ './accounts.component.scss' ] | |
0626e7af | 14 | }) |
3baf9be2 | 15 | export class AccountsComponent implements OnInit, OnDestroy { |
cfde28ba C |
16 | @ViewChild('accountReportModal') accountReportModal: AccountReportComponent |
17 | ||
6b738c7a | 18 | account: Account |
496b02e3 | 19 | accountUser: User |
a004ff17 | 20 | videoChannels: VideoChannel[] = [] |
24e7916c | 21 | links: ListOverflowItem[] = [] |
0626e7af | 22 | |
496b02e3 C |
23 | isAccountManageable = false |
24 | accountFollowerTitle = '' | |
25 | ||
cfde28ba C |
26 | prependModerationActions: DropdownAction<any>[] |
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 | 38 | private authService: AuthService, |
937b7a6a | 39 | private screenService: ScreenService, |
ee1d0dfb | 40 | private i18n: I18n |
fef213ca C |
41 | ) { |
42 | } | |
0626e7af C |
43 | |
44 | ngOnInit () { | |
734a5ceb | 45 | this.routeSub = this.route.params |
fef213ca C |
46 | .pipe( |
47 | map(params => params[ 'accountId' ]), | |
48 | distinctUntilChanged(), | |
49 | switchMap(accountId => this.accountService.getAccount(accountId)), | |
cfde28ba | 50 | tap(account => this.onAccount(account)), |
fef213ca C |
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 | ) | |
24e7916c RK |
59 | |
60 | this.links = [ | |
14571f19 RK |
61 | { label: this.i18n('VIDEO CHANNELS'), routerLink: 'video-channels' }, |
62 | { label: this.i18n('VIDEOS'), routerLink: 'videos' }, | |
63 | { label: this.i18n('ABOUT'), routerLink: 'about' } | |
24e7916c | 64 | ] |
734a5ceb | 65 | } |
0626e7af | 66 | |
734a5ceb C |
67 | ngOnDestroy () { |
68 | if (this.routeSub) this.routeSub.unsubscribe() | |
0626e7af | 69 | } |
79bd2632 | 70 | |
a004ff17 RK |
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 | ||
937b7a6a RK |
78 | get isInSmallView () { |
79 | return this.screenService.isInSmallView() | |
80 | } | |
81 | ||
79bd2632 C |
82 | onUserChanged () { |
83 | this.getUserIfNeeded(this.account) | |
84 | } | |
85 | ||
86 | onUserDeleted () { | |
87 | this.redirectService.redirectToHomepage() | |
88 | } | |
89 | ||
ee1d0dfb C |
90 | activateCopiedMessage () { |
91 | this.notifier.success(this.i18n('Username copied')) | |
92 | } | |
93 | ||
a004ff17 | 94 | subscribersDisplayFor (count: number) { |
0b1de586 | 95 | return this.i18n('{count, plural, =1 {1 subscriber} other {{{count}} subscribers}}', { count }) |
a004ff17 RK |
96 | } |
97 | ||
cfde28ba C |
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 | ||
79bd2632 | 133 | private getUserIfNeeded (account: Account) { |
496b02e3 | 134 | if (!account.userId || !this.authService.isLoggedIn()) return |
79bd2632 C |
135 | |
136 | const user = this.authService.getUser() | |
137 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
fef213ca C |
138 | this.userService.getUser(account.userId).subscribe( |
139 | accountUser => this.accountUser = accountUser, | |
79bd2632 | 140 | |
496b02e3 C |
141 | err => this.notifier.error(err.message) |
142 | ) | |
79bd2632 C |
143 | } |
144 | } | |
0626e7af | 145 | } |