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