]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/+accounts/accounts.component.ts
Redesign account's channels page
[github/Chocobozzz/PeerTube.git] / client / src / app / +accounts / accounts.component.ts
... / ...
CommitLineData
1import { Subscription } from 'rxjs'
2import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
3import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
4import { ActivatedRoute } from '@angular/router'
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'
15import { AccountReportComponent } from '@app/shared/shared-moderation'
16import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
17import { User, UserRight } from '@shared/models'
18import { AccountSearchComponent } from './account-search/account-search.component'
19
20@Component({
21 templateUrl: './accounts.component.html',
22 styleUrls: [ './accounts.component.scss' ]
23})
24export class AccountsComponent implements OnInit, OnDestroy {
25 @ViewChild('accountReportModal') accountReportModal: AccountReportComponent
26
27 accountSearch: AccountSearchComponent
28
29 account: Account
30 accountUser: User
31
32 videoChannels: VideoChannel[] = []
33
34 links: ListOverflowItem[] = []
35 hideMenu = false
36
37 accountFollowerTitle = ''
38
39 accountVideosCount: number
40 accountDescriptionHTML = ''
41 accountDescriptionExpanded = false
42
43 prependModerationActions: DropdownAction<any>[]
44
45 private routeSub: Subscription
46
47 constructor (
48 private route: ActivatedRoute,
49 private userService: UserService,
50 private accountService: AccountService,
51 private videoChannelService: VideoChannelService,
52 private notifier: Notifier,
53 private restExtractor: RestExtractor,
54 private redirectService: RedirectService,
55 private authService: AuthService,
56 private videoService: VideoService,
57 private markdown: MarkdownService,
58 private screenService: ScreenService
59 ) {
60 }
61
62 ngOnInit () {
63 this.routeSub = this.route.params
64 .pipe(
65 map(params => params[ 'accountId' ]),
66 distinctUntilChanged(),
67 switchMap(accountId => this.accountService.getAccount(accountId)),
68 tap(account => this.onAccount(account)),
69 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
70 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
71 HttpStatusCode.BAD_REQUEST_400,
72 HttpStatusCode.NOT_FOUND_404
73 ]))
74 )
75 .subscribe(
76 videoChannels => this.videoChannels = videoChannels.data,
77
78 err => this.notifier.error(err.message)
79 )
80
81 this.links = [
82 { label: $localize`VIDEO CHANNELS`, routerLink: 'video-channels' },
83 { label: $localize`VIDEOS`, routerLink: 'videos' }
84 ]
85 }
86
87 ngOnDestroy () {
88 if (this.routeSub) this.routeSub.unsubscribe()
89 }
90
91 naiveAggregatedSubscribers () {
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
98 isUserLoggedIn () {
99 return this.authService.isLoggedIn()
100 }
101
102 isInSmallView () {
103 return this.screenService.isInSmallView()
104 }
105
106 isManageable () {
107 if (!this.isUserLoggedIn()) return false
108
109 return this.account?.userId === this.authService.getUser().id
110 }
111
112 onUserChanged () {
113 this.loadUserIfNeeded(this.account)
114 }
115
116 onUserDeleted () {
117 this.redirectService.redirectToHomepage()
118 }
119
120 activateCopiedMessage () {
121 this.notifier.success($localize`Username copied`)
122 }
123
124 subscribersDisplayFor (count: number) {
125 if (count === 1) return $localize`1 subscriber`
126
127 return $localize`${count} subscribers`
128 }
129
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
142 onSearchInputDisplayChanged (displayed: boolean) {
143 this.hideMenu = this.isInSmallView() && displayed
144 }
145
146 hasVideoChannels () {
147 return this.videoChannels.length !== 0
148 }
149
150 private async onAccount (account: Account) {
151 this.accountFollowerTitle = $localize`${account.followersCount} direct account followers`
152
153 this.prependModerationActions = undefined
154
155 this.accountDescriptionHTML = await this.markdown.textMarkdownToHTML(account.description)
156
157 // After the markdown renderer to avoid layout changes
158 this.account = account
159
160 this.updateModerationActions()
161 this.loadUserIfNeeded(account)
162 this.loadAccountVideosCount()
163 }
164
165 private showReportModal () {
166 this.accountReportModal.show()
167 }
168
169 private loadUserIfNeeded (account: Account) {
170 if (!account.userId || !this.authService.isLoggedIn()) return
171
172 const user = this.authService.getUser()
173 if (user.hasRight(UserRight.MANAGE_USERS)) {
174 this.userService.getUser(account.userId).subscribe(
175 accountUser => this.accountUser = accountUser,
176
177 err => this.notifier.error(err.message)
178 )
179 }
180 }
181
182 private updateModerationActions () {
183 if (!this.authService.isLoggedIn()) return
184
185 this.authService.userInformationLoaded.subscribe(
186 () => {
187 if (this.isManageable()) return
188
189 // It's not our account, we can report it
190 this.prependModerationActions = [
191 {
192 label: $localize`Report this account`,
193 handler: () => this.showReportModal()
194 }
195 ]
196 }
197 )
198 }
199
200 private loadAccountVideosCount () {
201 this.videoService.getAccountVideos({
202 account: this.account,
203 videoPagination: {
204 currentPage: 1,
205 itemsPerPage: 0
206 },
207 sort: '-publishedAt'
208 }).subscribe(res => this.accountVideosCount = res.total)
209 }
210}