]>
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 { User, UserRight } from '@shared/models' | |
9 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | |
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 | ) { | |
41 | } | |
42 | ||
43 | ngOnInit () { | |
44 | this.routeSub = this.route.params | |
45 | .pipe( | |
46 | map(params => params[ 'accountId' ]), | |
47 | distinctUntilChanged(), | |
48 | switchMap(accountId => this.accountService.getAccount(accountId)), | |
49 | tap(account => this.onAccount(account)), | |
50 | switchMap(account => this.videoChannelService.listAccountVideoChannels(account)), | |
51 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ | |
52 | HttpStatusCode.BAD_REQUEST_400, | |
53 | HttpStatusCode.NOT_FOUND_404 | |
54 | ])) | |
55 | ) | |
56 | .subscribe( | |
57 | videoChannels => this.videoChannels = videoChannels.data, | |
58 | ||
59 | err => this.notifier.error(err.message) | |
60 | ) | |
61 | ||
62 | this.links = [ | |
63 | { label: $localize`VIDEO CHANNELS`, routerLink: 'video-channels' }, | |
64 | { label: $localize`VIDEOS`, routerLink: 'videos' }, | |
65 | { label: $localize`ABOUT`, routerLink: 'about' } | |
66 | ] | |
67 | } | |
68 | ||
69 | ngOnDestroy () { | |
70 | if (this.routeSub) this.routeSub.unsubscribe() | |
71 | } | |
72 | ||
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 | ||
80 | get isInSmallView () { | |
81 | return this.screenService.isInSmallView() | |
82 | } | |
83 | ||
84 | onUserChanged () { | |
85 | this.getUserIfNeeded(this.account) | |
86 | } | |
87 | ||
88 | onUserDeleted () { | |
89 | this.redirectService.redirectToHomepage() | |
90 | } | |
91 | ||
92 | activateCopiedMessage () { | |
93 | this.notifier.success($localize`Username copied`) | |
94 | } | |
95 | ||
96 | subscribersDisplayFor (count: number) { | |
97 | if (count === 1) return $localize`1 subscriber` | |
98 | ||
99 | return $localize`${count} subscribers` | |
100 | } | |
101 | ||
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 | ||
112 | const followers = this.subscribersDisplayFor(account.followersCount) | |
113 | this.accountFollowerTitle = $localize`${followers} direct account followers` | |
114 | ||
115 | // It's not our account, we can report it | |
116 | if (!this.isAccountManageable) { | |
117 | this.prependModerationActions = [ | |
118 | { | |
119 | label: $localize`Report this account`, | |
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 | ||
135 | private getUserIfNeeded (account: Account) { | |
136 | if (!account.userId || !this.authService.isLoggedIn()) return | |
137 | ||
138 | const user = this.authService.getUser() | |
139 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
140 | this.userService.getUser(account.userId).subscribe( | |
141 | accountUser => this.accountUser = accountUser, | |
142 | ||
143 | err => this.notifier.error(err.message) | |
144 | ) | |
145 | } | |
146 | } | |
147 | } |