]>
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' |
f2eb23cd | 9 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
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, |
66357162 | 39 | private screenService: ScreenService |
fef213ca C |
40 | ) { |
41 | } | |
0626e7af C |
42 | |
43 | ngOnInit () { | |
734a5ceb | 44 | this.routeSub = this.route.params |
fef213ca C |
45 | .pipe( |
46 | map(params => params[ 'accountId' ]), | |
47 | distinctUntilChanged(), | |
48 | switchMap(accountId => this.accountService.getAccount(accountId)), | |
cfde28ba | 49 | tap(account => this.onAccount(account)), |
fef213ca | 50 | switchMap(account => this.videoChannelService.listAccountVideoChannels(account)), |
f2eb23cd RK |
51 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ |
52 | HttpStatusCode.BAD_REQUEST_400, | |
53 | HttpStatusCode.NOT_FOUND_404 | |
54 | ])) | |
fef213ca C |
55 | ) |
56 | .subscribe( | |
57 | videoChannels => this.videoChannels = videoChannels.data, | |
58 | ||
59 | err => this.notifier.error(err.message) | |
60 | ) | |
24e7916c RK |
61 | |
62 | this.links = [ | |
66357162 C |
63 | { label: $localize`VIDEO CHANNELS`, routerLink: 'video-channels' }, |
64 | { label: $localize`VIDEOS`, routerLink: 'videos' }, | |
65 | { label: $localize`ABOUT`, routerLink: 'about' } | |
24e7916c | 66 | ] |
734a5ceb | 67 | } |
0626e7af | 68 | |
734a5ceb C |
69 | ngOnDestroy () { |
70 | if (this.routeSub) this.routeSub.unsubscribe() | |
0626e7af | 71 | } |
79bd2632 | 72 | |
a004ff17 RK |
73 | get naiveAggregatedSubscribers () { |
74 | return this.videoChannels.reduce( | |
75 | (acc, val) => acc + val.followersCount, | |
76 | this.account.followersCount // accumulator starts with the base number of subscribers the account has | |
77 | ) | |
78 | } | |
79 | ||
937b7a6a RK |
80 | get isInSmallView () { |
81 | return this.screenService.isInSmallView() | |
82 | } | |
83 | ||
79bd2632 C |
84 | onUserChanged () { |
85 | this.getUserIfNeeded(this.account) | |
86 | } | |
87 | ||
88 | onUserDeleted () { | |
89 | this.redirectService.redirectToHomepage() | |
90 | } | |
91 | ||
ee1d0dfb | 92 | activateCopiedMessage () { |
66357162 | 93 | this.notifier.success($localize`Username copied`) |
ee1d0dfb C |
94 | } |
95 | ||
a004ff17 | 96 | subscribersDisplayFor (count: number) { |
66357162 C |
97 | if (count === 1) return $localize`1 subscriber` |
98 | ||
99 | return $localize`${count} subscribers` | |
a004ff17 RK |
100 | } |
101 | ||
cfde28ba C |
102 | private onAccount (account: Account) { |
103 | this.prependModerationActions = undefined | |
104 | ||
105 | this.account = account | |
106 | ||
107 | if (this.authService.isLoggedIn()) { | |
108 | this.authService.userInformationLoaded.subscribe( | |
109 | () => { | |
110 | this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id | |
111 | ||
66357162 C |
112 | const followers = this.subscribersDisplayFor(account.followersCount) |
113 | this.accountFollowerTitle = $localize`${followers} direct account followers` | |
cfde28ba C |
114 | |
115 | // It's not our account, we can report it | |
116 | if (!this.isAccountManageable) { | |
117 | this.prependModerationActions = [ | |
118 | { | |
d3bb7994 | 119 | label: $localize`Report this account`, |
cfde28ba C |
120 | handler: () => this.showReportModal() |
121 | } | |
122 | ] | |
123 | } | |
124 | } | |
125 | ) | |
126 | } | |
127 | ||
128 | this.getUserIfNeeded(account) | |
129 | } | |
130 | ||
131 | private showReportModal () { | |
132 | this.accountReportModal.show() | |
133 | } | |
134 | ||
79bd2632 | 135 | private getUserIfNeeded (account: Account) { |
496b02e3 | 136 | if (!account.userId || !this.authService.isLoggedIn()) return |
79bd2632 C |
137 | |
138 | const user = this.authService.getUser() | |
139 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
fef213ca C |
140 | this.userService.getUser(account.userId).subscribe( |
141 | accountUser => this.accountUser = accountUser, | |
79bd2632 | 142 | |
496b02e3 C |
143 | err => this.notifier.error(err.message) |
144 | ) | |
79bd2632 C |
145 | } |
146 | } | |
0626e7af | 147 | } |