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