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