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