]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+accounts/accounts.component.ts
Fix search on ios
[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'
cfde28ba 15import { AccountReportComponent } 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,
66357162 55 private screenService: ScreenService
fef213ca
C
56 ) {
57 }
0626e7af
C
58
59 ngOnInit () {
734a5ceb 60 this.routeSub = this.route.params
fef213ca 61 .pipe(
9df52d66 62 map(params => params['accountId']),
fef213ca
C
63 distinctUntilChanged(),
64 switchMap(accountId => this.accountService.getAccount(accountId)),
cfde28ba 65 tap(account => this.onAccount(account)),
dc2b2938 66 switchMap(account => this.videoChannelService.listAccountVideoChannels({ account })),
ab398a05 67 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
f2eb23cd
RK
68 HttpStatusCode.BAD_REQUEST_400,
69 HttpStatusCode.NOT_FOUND_404
70 ]))
fef213ca 71 )
1378c0d3 72 .subscribe({
9df52d66
C
73 next: videoChannels => {
74 this.videoChannels = videoChannels.data
75 },
fef213ca 76
1378c0d3
C
77 error: err => this.notifier.error(err.message)
78 })
24e7916c
RK
79
80 this.links = [
0a25749f 81 { label: $localize`CHANNELS`, routerLink: 'video-channels' },
67264e06 82 { label: $localize`VIDEOS`, routerLink: 'videos' }
24e7916c 83 ]
734a5ceb 84 }
0626e7af 85
734a5ceb
C
86 ngOnDestroy () {
87 if (this.routeSub) this.routeSub.unsubscribe()
0626e7af 88 }
79bd2632 89
67264e06 90 naiveAggregatedSubscribers () {
a004ff17
RK
91 return this.videoChannels.reduce(
92 (acc, val) => acc + val.followersCount,
93 this.account.followersCount // accumulator starts with the base number of subscribers the account has
94 )
95 }
96
67264e06
C
97 isUserLoggedIn () {
98 return this.authService.isLoggedIn()
99 }
100
101 isInSmallView () {
937b7a6a
RK
102 return this.screenService.isInSmallView()
103 }
104
67264e06
C
105 isManageable () {
106 if (!this.isUserLoggedIn()) return false
107
108 return this.account?.userId === this.authService.getUser().id
109 }
110
79bd2632 111 onUserChanged () {
67264e06 112 this.loadUserIfNeeded(this.account)
79bd2632
C
113 }
114
115 onUserDeleted () {
116 this.redirectService.redirectToHomepage()
117 }
118
ee1d0dfb 119 activateCopiedMessage () {
66357162 120 this.notifier.success($localize`Username copied`)
ee1d0dfb
C
121 }
122
a004ff17 123 subscribersDisplayFor (count: number) {
66357162
C
124 if (count === 1) return $localize`1 subscriber`
125
126 return $localize`${count} subscribers`
a004ff17
RK
127 }
128
37024082 129 searchChanged (search: string) {
dd24f1bb
C
130 const queryParams = { search }
131
132 this.router.navigate([ './videos' ], { queryParams, relativeTo: this.route, queryParamsHandling: 'merge' })
37024082
RK
133 }
134
67264e06
C
135 onSearchInputDisplayChanged (displayed: boolean) {
136 this.hideMenu = this.isInSmallView() && displayed
137 }
138
900f7820
C
139 hasVideoChannels () {
140 return this.videoChannels.length !== 0
141 }
142
733dbc53
C
143 hasShowMoreDescription () {
144 return !this.accountDescriptionExpanded && this.accountDescriptionHTML.length > 100
145 }
146
dd24f1bb
C
147 isOnChannelPage () {
148 return this.route.children[0].snapshot.url[0].path === 'video-channels'
149 }
150
67264e06
C
151 private async onAccount (account: Account) {
152 this.accountFollowerTitle = $localize`${account.followersCount} direct account followers`
153
67264e06 154 this.accountDescriptionHTML = await this.markdown.textMarkdownToHTML(account.description)
cfde28ba 155
67264e06
C
156 // After the markdown renderer to avoid layout changes
157 this.account = account
cfde28ba 158
67264e06
C
159 this.updateModerationActions()
160 this.loadUserIfNeeded(account)
161 this.loadAccountVideosCount()
cfde28ba
C
162 }
163
164 private showReportModal () {
165 this.accountReportModal.show()
166 }
167
67264e06 168 private loadUserIfNeeded (account: Account) {
496b02e3 169 if (!account.userId || !this.authService.isLoggedIn()) return
79bd2632
C
170
171 const user = this.authService.getUser()
172 if (user.hasRight(UserRight.MANAGE_USERS)) {
1378c0d3
C
173 this.userService.getUser(account.userId)
174 .subscribe({
9df52d66
C
175 next: accountUser => {
176 this.accountUser = accountUser
177 },
79bd2632 178
1378c0d3
C
179 error: err => this.notifier.error(err.message)
180 })
79bd2632
C
181 }
182 }
67264e06
C
183
184 private updateModerationActions () {
a2c3564a
C
185 this.prependModerationActions = []
186
67264e06
C
187 if (!this.authService.isLoggedIn()) return
188
189 this.authService.userInformationLoaded.subscribe(
190 () => {
191 if (this.isManageable()) return
192
193 // It's not our account, we can report it
194 this.prependModerationActions = [
a2c3564a
C
195 {
196 label: $localize`Report`,
197 isHeader: true
198 },
67264e06
C
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}