]>
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' |
67264e06 C |
5 | import { AuthService, MarkdownService, Notifier, RedirectService, RestExtractor, ScreenService, UserService } from '@app/core' |
6 | import { | |
7 | Account, | |
8 | AccountService, | |
9 | DropdownAction, | |
10 | ListOverflowItem, | |
11 | VideoChannel, | |
12 | VideoChannelService, | |
13 | VideoService | |
14 | } from '@app/shared/shared-main' | |
cfde28ba | 15 | import { AccountReportComponent } from '@app/shared/shared-moderation' |
f2eb23cd | 16 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
67264e06 | 17 | import { User, UserRight } from '@shared/models' |
37024082 | 18 | import { AccountSearchComponent } from './account-search/account-search.component' |
0626e7af C |
19 | |
20 | @Component({ | |
170726f5 C |
21 | templateUrl: './accounts.component.html', |
22 | styleUrls: [ './accounts.component.scss' ] | |
0626e7af | 23 | }) |
3baf9be2 | 24 | export class AccountsComponent implements OnInit, OnDestroy { |
cfde28ba | 25 | @ViewChild('accountReportModal') accountReportModal: AccountReportComponent |
67264e06 | 26 | |
37024082 | 27 | accountSearch: AccountSearchComponent |
cfde28ba | 28 | |
6b738c7a | 29 | account: Account |
496b02e3 | 30 | accountUser: User |
67264e06 | 31 | |
a004ff17 | 32 | videoChannels: VideoChannel[] = [] |
67264e06 | 33 | |
24e7916c | 34 | links: ListOverflowItem[] = [] |
67264e06 | 35 | hideMenu = false |
0626e7af | 36 | |
496b02e3 C |
37 | accountFollowerTitle = '' |
38 | ||
67264e06 C |
39 | accountVideosCount: number |
40 | accountDescriptionHTML = '' | |
41 | accountDescriptionExpanded = false | |
42 | ||
cfde28ba C |
43 | prependModerationActions: DropdownAction<any>[] |
44 | ||
734a5ceb C |
45 | private routeSub: Subscription |
46 | ||
0626e7af C |
47 | constructor ( |
48 | private route: ActivatedRoute, | |
79bd2632 | 49 | private userService: UserService, |
a51bad1a | 50 | private accountService: AccountService, |
41eb700f | 51 | private videoChannelService: VideoChannelService, |
f8b2c1b4 | 52 | private notifier: Notifier, |
79bd2632 C |
53 | private restExtractor: RestExtractor, |
54 | private redirectService: RedirectService, | |
ee1d0dfb | 55 | private authService: AuthService, |
67264e06 C |
56 | private videoService: VideoService, |
57 | private markdown: MarkdownService, | |
66357162 | 58 | private screenService: ScreenService |
fef213ca C |
59 | ) { |
60 | } | |
0626e7af C |
61 | |
62 | ngOnInit () { | |
734a5ceb | 63 | this.routeSub = this.route.params |
fef213ca C |
64 | .pipe( |
65 | map(params => params[ 'accountId' ]), | |
66 | distinctUntilChanged(), | |
67 | switchMap(accountId => this.accountService.getAccount(accountId)), | |
cfde28ba | 68 | tap(account => this.onAccount(account)), |
fef213ca | 69 | switchMap(account => this.videoChannelService.listAccountVideoChannels(account)), |
ab398a05 | 70 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [ |
f2eb23cd RK |
71 | HttpStatusCode.BAD_REQUEST_400, |
72 | HttpStatusCode.NOT_FOUND_404 | |
73 | ])) | |
fef213ca C |
74 | ) |
75 | .subscribe( | |
76 | videoChannels => this.videoChannels = videoChannels.data, | |
77 | ||
78 | err => this.notifier.error(err.message) | |
79 | ) | |
24e7916c RK |
80 | |
81 | this.links = [ | |
66357162 | 82 | { label: $localize`VIDEO CHANNELS`, routerLink: 'video-channels' }, |
67264e06 | 83 | { label: $localize`VIDEOS`, routerLink: 'videos' } |
24e7916c | 84 | ] |
734a5ceb | 85 | } |
0626e7af | 86 | |
734a5ceb C |
87 | ngOnDestroy () { |
88 | if (this.routeSub) this.routeSub.unsubscribe() | |
0626e7af | 89 | } |
79bd2632 | 90 | |
67264e06 | 91 | naiveAggregatedSubscribers () { |
a004ff17 RK |
92 | return this.videoChannels.reduce( |
93 | (acc, val) => acc + val.followersCount, | |
94 | this.account.followersCount // accumulator starts with the base number of subscribers the account has | |
95 | ) | |
96 | } | |
97 | ||
67264e06 C |
98 | isUserLoggedIn () { |
99 | return this.authService.isLoggedIn() | |
100 | } | |
101 | ||
102 | isInSmallView () { | |
937b7a6a RK |
103 | return this.screenService.isInSmallView() |
104 | } | |
105 | ||
67264e06 C |
106 | isManageable () { |
107 | if (!this.isUserLoggedIn()) return false | |
108 | ||
109 | return this.account?.userId === this.authService.getUser().id | |
110 | } | |
111 | ||
79bd2632 | 112 | onUserChanged () { |
67264e06 | 113 | this.loadUserIfNeeded(this.account) |
79bd2632 C |
114 | } |
115 | ||
116 | onUserDeleted () { | |
117 | this.redirectService.redirectToHomepage() | |
118 | } | |
119 | ||
ee1d0dfb | 120 | activateCopiedMessage () { |
66357162 | 121 | this.notifier.success($localize`Username copied`) |
ee1d0dfb C |
122 | } |
123 | ||
a004ff17 | 124 | subscribersDisplayFor (count: number) { |
66357162 C |
125 | if (count === 1) return $localize`1 subscriber` |
126 | ||
127 | return $localize`${count} subscribers` | |
a004ff17 RK |
128 | } |
129 | ||
37024082 RK |
130 | onOutletLoaded (component: Component) { |
131 | if (component instanceof AccountSearchComponent) { | |
132 | this.accountSearch = component | |
133 | } else { | |
134 | this.accountSearch = undefined | |
135 | } | |
136 | } | |
137 | ||
138 | searchChanged (search: string) { | |
139 | if (this.accountSearch) this.accountSearch.updateSearch(search) | |
140 | } | |
141 | ||
67264e06 C |
142 | onSearchInputDisplayChanged (displayed: boolean) { |
143 | this.hideMenu = this.isInSmallView() && displayed | |
144 | } | |
145 | ||
900f7820 C |
146 | hasVideoChannels () { |
147 | return this.videoChannels.length !== 0 | |
148 | } | |
149 | ||
67264e06 C |
150 | private async onAccount (account: Account) { |
151 | this.accountFollowerTitle = $localize`${account.followersCount} direct account followers` | |
152 | ||
cfde28ba C |
153 | this.prependModerationActions = undefined |
154 | ||
67264e06 | 155 | this.accountDescriptionHTML = await this.markdown.textMarkdownToHTML(account.description) |
cfde28ba | 156 | |
67264e06 C |
157 | // After the markdown renderer to avoid layout changes |
158 | this.account = account | |
cfde28ba | 159 | |
67264e06 C |
160 | this.updateModerationActions() |
161 | this.loadUserIfNeeded(account) | |
162 | this.loadAccountVideosCount() | |
cfde28ba C |
163 | } |
164 | ||
165 | private showReportModal () { | |
166 | this.accountReportModal.show() | |
167 | } | |
168 | ||
67264e06 | 169 | private loadUserIfNeeded (account: Account) { |
496b02e3 | 170 | if (!account.userId || !this.authService.isLoggedIn()) return |
79bd2632 C |
171 | |
172 | const user = this.authService.getUser() | |
173 | if (user.hasRight(UserRight.MANAGE_USERS)) { | |
fef213ca C |
174 | this.userService.getUser(account.userId).subscribe( |
175 | accountUser => this.accountUser = accountUser, | |
79bd2632 | 176 | |
496b02e3 C |
177 | err => this.notifier.error(err.message) |
178 | ) | |
79bd2632 C |
179 | } |
180 | } | |
67264e06 C |
181 | |
182 | private updateModerationActions () { | |
183 | if (!this.authService.isLoggedIn()) return | |
184 | ||
185 | this.authService.userInformationLoaded.subscribe( | |
186 | () => { | |
187 | if (this.isManageable()) return | |
188 | ||
189 | // It's not our account, we can report it | |
190 | this.prependModerationActions = [ | |
191 | { | |
192 | label: $localize`Report this account`, | |
193 | handler: () => this.showReportModal() | |
194 | } | |
195 | ] | |
196 | } | |
197 | ) | |
198 | } | |
199 | ||
200 | private loadAccountVideosCount () { | |
201 | this.videoService.getAccountVideos({ | |
202 | account: this.account, | |
203 | videoPagination: { | |
204 | currentPage: 1, | |
205 | itemsPerPage: 0 | |
206 | }, | |
207 | sort: '-publishedAt' | |
208 | }).subscribe(res => this.accountVideosCount = res.total) | |
209 | } | |
0626e7af | 210 | } |