]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+accounts/accounts.component.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / +accounts / accounts.component.ts
CommitLineData
67ed6552
C
1import { Subscription } from 'rxjs'
2import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
cfde28ba 3import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
dd24f1bb 4import { ActivatedRoute, Router } from '@angular/router'
67264e06
C
5import { AuthService, MarkdownService, Notifier, RedirectService, RestExtractor, ScreenService, UserService } from '@app/core'
6import {
7 Account,
8 AccountService,
9 DropdownAction,
10 ListOverflowItem,
11 VideoChannel,
12 VideoChannelService,
13 VideoService
14} from '@app/shared/shared-main'
80badf49 15import { AccountReportComponent, BlocklistService } from '@app/shared/shared-moderation'
c0e8b12e 16import { 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 22export 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) {
0e45e336
C
145 this.accountDescriptionHTML = await this.markdown.textMarkdownToHTML({
146 markdown: account.description,
147 withEmoji: true,
148 withHtml: true
149 })
cfde28ba 150
67264e06
C
151 // After the markdown renderer to avoid layout changes
152 this.account = account
cfde28ba 153
67264e06
C
154 this.updateModerationActions()
155 this.loadUserIfNeeded(account)
156 this.loadAccountVideosCount()
80badf49 157 this.loadAccountBlockStatus()
cfde28ba
C
158 }
159
160 private showReportModal () {
ae9809a7 161 this.accountReportModal.show(this.account)
cfde28ba
C
162 }
163
67264e06 164 private loadUserIfNeeded (account: Account) {
496b02e3 165 if (!account.userId || !this.authService.isLoggedIn()) return
79bd2632
C
166
167 const user = this.authService.getUser()
168 if (user.hasRight(UserRight.MANAGE_USERS)) {
1378c0d3
C
169 this.userService.getUser(account.userId)
170 .subscribe({
9df52d66
C
171 next: accountUser => {
172 this.accountUser = accountUser
173 },
79bd2632 174
1378c0d3
C
175 error: err => this.notifier.error(err.message)
176 })
79bd2632
C
177 }
178 }
67264e06
C
179
180 private updateModerationActions () {
a2c3564a
C
181 this.prependModerationActions = []
182
67264e06
C
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 = [
a2c3564a
C
191 {
192 label: $localize`Report`,
193 isHeader: true
194 },
67264e06
C
195 {
196 label: $localize`Report this account`,
197 handler: () => this.showReportModal()
198 }
199 ]
200 }
201 )
202 }
203
204 private loadAccountVideosCount () {
205 this.videoService.getAccountVideos({
206 account: this.account,
207 videoPagination: {
208 currentPage: 1,
209 itemsPerPage: 0
210 },
211 sort: '-publishedAt'
9df52d66
C
212 }).subscribe(res => {
213 this.accountVideosCount = res.total
214 })
67264e06 215 }
80badf49
C
216
217 private loadAccountBlockStatus () {
218 this.blocklist.getStatus({ accounts: [ this.account.nameWithHostForced ], hosts: [ this.account.host ] })
219 .subscribe(status => this.account.updateBlockStatus(status))
220 }
0626e7af 221}