]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+accounts/accounts.component.ts
Redesign account page
[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)),
fef213ca 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 = [
66357162 82 { label: $localize`VIDEO 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
146 private async onAccount (account: Account) {
147 this.accountFollowerTitle = $localize`${account.followersCount} direct account followers`
148
cfde28ba
C
149 this.prependModerationActions = undefined
150
67264e06 151 this.accountDescriptionHTML = await this.markdown.textMarkdownToHTML(account.description)
cfde28ba 152
67264e06
C
153 // After the markdown renderer to avoid layout changes
154 this.account = account
cfde28ba 155
67264e06
C
156 this.updateModerationActions()
157 this.loadUserIfNeeded(account)
158 this.loadAccountVideosCount()
cfde28ba
C
159 }
160
161 private showReportModal () {
162 this.accountReportModal.show()
163 }
164
67264e06 165 private loadUserIfNeeded (account: Account) {
496b02e3 166 if (!account.userId || !this.authService.isLoggedIn()) return
79bd2632
C
167
168 const user = this.authService.getUser()
169 if (user.hasRight(UserRight.MANAGE_USERS)) {
fef213ca
C
170 this.userService.getUser(account.userId).subscribe(
171 accountUser => this.accountUser = accountUser,
79bd2632 172
496b02e3
C
173 err => this.notifier.error(err.message)
174 )
79bd2632
C
175 }
176 }
67264e06
C
177
178 private updateModerationActions () {
179 if (!this.authService.isLoggedIn()) return
180
181 this.authService.userInformationLoaded.subscribe(
182 () => {
183 if (this.isManageable()) return
184
185 // It's not our account, we can report it
186 this.prependModerationActions = [
187 {
188 label: $localize`Report this account`,
189 handler: () => this.showReportModal()
190 }
191 ]
192 }
193 )
194 }
195
196 private loadAccountVideosCount () {
197 this.videoService.getAccountVideos({
198 account: this.account,
199 videoPagination: {
200 currentPage: 1,
201 itemsPerPage: 0
202 },
203 sort: '-publishedAt'
204 }).subscribe(res => this.accountVideosCount = res.total)
205 }
0626e7af 206}