]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+accounts/accounts.component.ts
Fix server run
[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'
0626e7af 4import { ActivatedRoute } 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'
f2eb23cd 16import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
67264e06 17import { User, UserRight } from '@shared/models'
37024082 18import { AccountSearchComponent } from './account-search/account-search.component'
0626e7af
C
19
20@Component({
170726f5
C
21 templateUrl: './accounts.component.html',
22 styleUrls: [ './accounts.component.scss' ]
0626e7af 23})
3baf9be2 24export class AccountsComponent implements OnInit, OnDestroy {
cfde28ba 25 @ViewChild('accountReportModal') accountReportModal: AccountReportComponent
67264e06 26
37024082 27 accountSearch: AccountSearchComponent
cfde28ba 28
6b738c7a 29 account: Account
496b02e3 30 accountUser: User
67264e06 31
a004ff17 32 videoChannels: VideoChannel[] = []
67264e06 33
24e7916c 34 links: ListOverflowItem[] = []
67264e06 35 hideMenu = false
0626e7af 36
496b02e3
C
37 accountFollowerTitle = ''
38
67264e06
C
39 accountVideosCount: number
40 accountDescriptionHTML = ''
41 accountDescriptionExpanded = false
42
cfde28ba
C
43 prependModerationActions: DropdownAction<any>[]
44
734a5ceb
C
45 private routeSub: Subscription
46
0626e7af
C
47 constructor (
48 private route: ActivatedRoute,
79bd2632 49 private userService: UserService,
a51bad1a 50 private accountService: AccountService,
41eb700f 51 private videoChannelService: VideoChannelService,
f8b2c1b4 52 private notifier: Notifier,
79bd2632
C
53 private restExtractor: RestExtractor,
54 private redirectService: RedirectService,
ee1d0dfb 55 private authService: AuthService,
67264e06
C
56 private videoService: VideoService,
57 private markdown: MarkdownService,
66357162 58 private screenService: ScreenService
fef213ca
C
59 ) {
60 }
0626e7af
C
61
62 ngOnInit () {
734a5ceb 63 this.routeSub = this.route.params
fef213ca
C
64 .pipe(
65 map(params => params[ 'accountId' ]),
66 distinctUntilChanged(),
67 switchMap(accountId => this.accountService.getAccount(accountId)),
cfde28ba 68 tap(account => this.onAccount(account)),
dc2b2938 69 switchMap(account => this.videoChannelService.listAccountVideoChannels({ account })),
ab398a05 70 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
f2eb23cd
RK
71 HttpStatusCode.BAD_REQUEST_400,
72 HttpStatusCode.NOT_FOUND_404
73 ]))
fef213ca
C
74 )
75 .subscribe(
76 videoChannels => this.videoChannels = videoChannels.data,
77
78 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
RK
130 onOutletLoaded (component: Component) {
131 if (component instanceof AccountSearchComponent) {
132 this.accountSearch = component
133 } else {
134 this.accountSearch = undefined
135 }
136 }
137
138 searchChanged (search: string) {
139 if (this.accountSearch) this.accountSearch.updateSearch(search)
140 }
141
67264e06
C
142 onSearchInputDisplayChanged (displayed: boolean) {
143 this.hideMenu = this.isInSmallView() && displayed
144 }
145
900f7820
C
146 hasVideoChannels () {
147 return this.videoChannels.length !== 0
148 }
149
733dbc53
C
150 hasShowMoreDescription () {
151 return !this.accountDescriptionExpanded && this.accountDescriptionHTML.length > 100
152 }
153
67264e06
C
154 private async onAccount (account: Account) {
155 this.accountFollowerTitle = $localize`${account.followersCount} direct account followers`
156
cfde28ba
C
157 this.prependModerationActions = undefined
158
67264e06 159 this.accountDescriptionHTML = await this.markdown.textMarkdownToHTML(account.description)
cfde28ba 160
67264e06
C
161 // After the markdown renderer to avoid layout changes
162 this.account = account
cfde28ba 163
67264e06
C
164 this.updateModerationActions()
165 this.loadUserIfNeeded(account)
166 this.loadAccountVideosCount()
cfde28ba
C
167 }
168
169 private showReportModal () {
170 this.accountReportModal.show()
171 }
172
67264e06 173 private loadUserIfNeeded (account: Account) {
496b02e3 174 if (!account.userId || !this.authService.isLoggedIn()) return
79bd2632
C
175
176 const user = this.authService.getUser()
177 if (user.hasRight(UserRight.MANAGE_USERS)) {
fef213ca
C
178 this.userService.getUser(account.userId).subscribe(
179 accountUser => this.accountUser = accountUser,
79bd2632 180
496b02e3
C
181 err => this.notifier.error(err.message)
182 )
79bd2632
C
183 }
184 }
67264e06
C
185
186 private updateModerationActions () {
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 = [
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'
212 }).subscribe(res => this.accountVideosCount = res.total)
213 }
0626e7af 214}