]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+accounts/accounts.component.ts
Allow users/visitors to search through an account's videos (#3589)
[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'
67ed6552 5import { AuthService, Notifier, RedirectService, RestExtractor, ScreenService, UserService } from '@app/core'
cfde28ba
C
6import { Account, AccountService, DropdownAction, ListOverflowItem, VideoChannel, VideoChannelService } from '@app/shared/shared-main'
7import { AccountReportComponent } from '@app/shared/shared-moderation'
67ed6552 8import { User, UserRight } from '@shared/models'
f2eb23cd 9import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
37024082 10import { AccountSearchComponent } from './account-search/account-search.component'
0626e7af
C
11
12@Component({
170726f5
C
13 templateUrl: './accounts.component.html',
14 styleUrls: [ './accounts.component.scss' ]
0626e7af 15})
3baf9be2 16export class AccountsComponent implements OnInit, OnDestroy {
cfde28ba 17 @ViewChild('accountReportModal') accountReportModal: AccountReportComponent
37024082 18 accountSearch: AccountSearchComponent
cfde28ba 19
6b738c7a 20 account: Account
496b02e3 21 accountUser: User
a004ff17 22 videoChannels: VideoChannel[] = []
24e7916c 23 links: ListOverflowItem[] = []
0626e7af 24
496b02e3
C
25 isAccountManageable = false
26 accountFollowerTitle = ''
27
cfde28ba
C
28 prependModerationActions: DropdownAction<any>[]
29
734a5ceb
C
30 private routeSub: Subscription
31
0626e7af
C
32 constructor (
33 private route: ActivatedRoute,
79bd2632 34 private userService: UserService,
a51bad1a 35 private accountService: AccountService,
41eb700f 36 private videoChannelService: VideoChannelService,
f8b2c1b4 37 private notifier: Notifier,
79bd2632
C
38 private restExtractor: RestExtractor,
39 private redirectService: RedirectService,
ee1d0dfb 40 private authService: AuthService,
66357162 41 private screenService: ScreenService
fef213ca
C
42 ) {
43 }
0626e7af
C
44
45 ngOnInit () {
734a5ceb 46 this.routeSub = this.route.params
fef213ca
C
47 .pipe(
48 map(params => params[ 'accountId' ]),
49 distinctUntilChanged(),
50 switchMap(accountId => this.accountService.getAccount(accountId)),
cfde28ba 51 tap(account => this.onAccount(account)),
fef213ca 52 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
f2eb23cd
RK
53 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [
54 HttpStatusCode.BAD_REQUEST_400,
55 HttpStatusCode.NOT_FOUND_404
56 ]))
fef213ca
C
57 )
58 .subscribe(
59 videoChannels => this.videoChannels = videoChannels.data,
60
61 err => this.notifier.error(err.message)
62 )
24e7916c
RK
63
64 this.links = [
66357162
C
65 { label: $localize`VIDEO CHANNELS`, routerLink: 'video-channels' },
66 { label: $localize`VIDEOS`, routerLink: 'videos' },
67 { label: $localize`ABOUT`, routerLink: 'about' }
24e7916c 68 ]
734a5ceb 69 }
0626e7af 70
734a5ceb
C
71 ngOnDestroy () {
72 if (this.routeSub) this.routeSub.unsubscribe()
0626e7af 73 }
79bd2632 74
a004ff17
RK
75 get naiveAggregatedSubscribers () {
76 return this.videoChannels.reduce(
77 (acc, val) => acc + val.followersCount,
78 this.account.followersCount // accumulator starts with the base number of subscribers the account has
79 )
80 }
81
937b7a6a
RK
82 get isInSmallView () {
83 return this.screenService.isInSmallView()
84 }
85
79bd2632
C
86 onUserChanged () {
87 this.getUserIfNeeded(this.account)
88 }
89
90 onUserDeleted () {
91 this.redirectService.redirectToHomepage()
92 }
93
ee1d0dfb 94 activateCopiedMessage () {
66357162 95 this.notifier.success($localize`Username copied`)
ee1d0dfb
C
96 }
97
a004ff17 98 subscribersDisplayFor (count: number) {
66357162
C
99 if (count === 1) return $localize`1 subscriber`
100
101 return $localize`${count} subscribers`
a004ff17
RK
102 }
103
37024082
RK
104 onOutletLoaded (component: Component) {
105 if (component instanceof AccountSearchComponent) {
106 this.accountSearch = component
107 } else {
108 this.accountSearch = undefined
109 }
110 }
111
112 searchChanged (search: string) {
113 if (this.accountSearch) this.accountSearch.updateSearch(search)
114 }
115
cfde28ba
C
116 private onAccount (account: Account) {
117 this.prependModerationActions = undefined
118
119 this.account = account
120
121 if (this.authService.isLoggedIn()) {
122 this.authService.userInformationLoaded.subscribe(
123 () => {
124 this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id
125
66357162
C
126 const followers = this.subscribersDisplayFor(account.followersCount)
127 this.accountFollowerTitle = $localize`${followers} direct account followers`
cfde28ba
C
128
129 // It's not our account, we can report it
130 if (!this.isAccountManageable) {
131 this.prependModerationActions = [
132 {
d3bb7994 133 label: $localize`Report this account`,
cfde28ba
C
134 handler: () => this.showReportModal()
135 }
136 ]
137 }
138 }
139 )
140 }
141
142 this.getUserIfNeeded(account)
143 }
144
145 private showReportModal () {
146 this.accountReportModal.show()
147 }
148
79bd2632 149 private getUserIfNeeded (account: Account) {
496b02e3 150 if (!account.userId || !this.authService.isLoggedIn()) return
79bd2632
C
151
152 const user = this.authService.getUser()
153 if (user.hasRight(UserRight.MANAGE_USERS)) {
fef213ca
C
154 this.userService.getUser(account.userId).subscribe(
155 accountUser => this.accountUser = accountUser,
79bd2632 156
496b02e3
C
157 err => this.notifier.error(err.message)
158 )
79bd2632
C
159 }
160 }
0626e7af 161}