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