]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+accounts/accounts.component.ts
Merge branch 'release/4.0.0' into develop
[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
496b02e3
C
33 accountFollowerTitle = ''
34
67264e06
C
35 accountVideosCount: number
36 accountDescriptionHTML = ''
37 accountDescriptionExpanded = false
38
a2c3564a 39 prependModerationActions: DropdownAction<any>[] = []
cfde28ba 40
734a5ceb
C
41 private routeSub: Subscription
42
0626e7af
C
43 constructor (
44 private route: ActivatedRoute,
dd24f1bb 45 private router: Router,
79bd2632 46 private userService: UserService,
a51bad1a 47 private accountService: AccountService,
41eb700f 48 private videoChannelService: VideoChannelService,
f8b2c1b4 49 private notifier: Notifier,
79bd2632
C
50 private restExtractor: RestExtractor,
51 private redirectService: RedirectService,
ee1d0dfb 52 private authService: AuthService,
67264e06
C
53 private videoService: VideoService,
54 private markdown: MarkdownService,
80badf49 55 private blocklist: BlocklistService,
66357162 56 private screenService: ScreenService
fef213ca
C
57 ) {
58 }
0626e7af
C
59
60 ngOnInit () {
734a5ceb 61 this.routeSub = this.route.params
fef213ca 62 .pipe(
9df52d66 63 map(params => params['accountId']),
fef213ca
C
64 distinctUntilChanged(),
65 switchMap(accountId => this.accountService.getAccount(accountId)),
cfde28ba 66 tap(account => this.onAccount(account)),
dc2b2938 67 switchMap(account => this.videoChannelService.listAccountVideoChannels({ account })),
ab398a05 68 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
f2eb23cd
RK
69 HttpStatusCode.BAD_REQUEST_400,
70 HttpStatusCode.NOT_FOUND_404
71 ]))
fef213ca 72 )
1378c0d3 73 .subscribe({
9df52d66
C
74 next: videoChannels => {
75 this.videoChannels = videoChannels.data
76 },
fef213ca 77
1378c0d3
C
78 error: err => this.notifier.error(err.message)
79 })
24e7916c
RK
80
81 this.links = [
0a25749f 82 { label: $localize`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 130 searchChanged (search: string) {
dd24f1bb
C
131 const queryParams = { search }
132
133 this.router.navigate([ './videos' ], { queryParams, relativeTo: this.route, queryParamsHandling: 'merge' })
37024082
RK
134 }
135
67264e06
C
136 onSearchInputDisplayChanged (displayed: boolean) {
137 this.hideMenu = this.isInSmallView() && displayed
138 }
139
900f7820
C
140 hasVideoChannels () {
141 return this.videoChannels.length !== 0
142 }
143
733dbc53
C
144 hasShowMoreDescription () {
145 return !this.accountDescriptionExpanded && this.accountDescriptionHTML.length > 100
146 }
147
dd24f1bb
C
148 isOnChannelPage () {
149 return this.route.children[0].snapshot.url[0].path === 'video-channels'
150 }
151
67264e06
C
152 private async onAccount (account: Account) {
153 this.accountFollowerTitle = $localize`${account.followersCount} direct account followers`
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()
80badf49 163 this.loadAccountBlockStatus()
cfde28ba
C
164 }
165
166 private showReportModal () {
ae9809a7 167 this.accountReportModal.show(this.account)
cfde28ba
C
168 }
169
67264e06 170 private loadUserIfNeeded (account: Account) {
496b02e3 171 if (!account.userId || !this.authService.isLoggedIn()) return
79bd2632
C
172
173 const user = this.authService.getUser()
174 if (user.hasRight(UserRight.MANAGE_USERS)) {
1378c0d3
C
175 this.userService.getUser(account.userId)
176 .subscribe({
9df52d66
C
177 next: accountUser => {
178 this.accountUser = accountUser
179 },
79bd2632 180
1378c0d3
C
181 error: err => this.notifier.error(err.message)
182 })
79bd2632
C
183 }
184 }
67264e06
C
185
186 private updateModerationActions () {
a2c3564a
C
187 this.prependModerationActions = []
188
67264e06
C
189 if (!this.authService.isLoggedIn()) return
190
191 this.authService.userInformationLoaded.subscribe(
192 () => {
193 if (this.isManageable()) return
194
195 // It's not our account, we can report it
196 this.prependModerationActions = [
a2c3564a
C
197 {
198 label: $localize`Report`,
199 isHeader: true
200 },
67264e06
C
201 {
202 label: $localize`Report this account`,
203 handler: () => this.showReportModal()
204 }
205 ]
206 }
207 )
208 }
209
210 private loadAccountVideosCount () {
211 this.videoService.getAccountVideos({
212 account: this.account,
213 videoPagination: {
214 currentPage: 1,
215 itemsPerPage: 0
216 },
217 sort: '-publishedAt'
9df52d66
C
218 }).subscribe(res => {
219 this.accountVideosCount = res.total
220 })
67264e06 221 }
80badf49
C
222
223 private loadAccountBlockStatus () {
224 this.blocklist.getStatus({ accounts: [ this.account.nameWithHostForced ], hosts: [ this.account.host ] })
225 .subscribe(status => this.account.updateBlockStatus(status))
226 }
0626e7af 227}