]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+accounts/accounts.component.ts
Try to improve tools doc
[github/Chocobozzz/PeerTube.git] / client / src / app / +accounts / accounts.component.ts
1 import { Subscription } from 'rxjs'
2 import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
4 import { ActivatedRoute } from '@angular/router'
5 import { AuthService, Notifier, RedirectService, RestExtractor, ScreenService, UserService } from '@app/core'
6 import { Account, AccountService, DropdownAction, ListOverflowItem, VideoChannel, VideoChannelService } from '@app/shared/shared-main'
7 import { AccountReportComponent } from '@app/shared/shared-moderation'
8 import { User, UserRight } from '@shared/models'
9
10 @Component({
11 templateUrl: './accounts.component.html',
12 styleUrls: [ './accounts.component.scss' ]
13 })
14 export class AccountsComponent implements OnInit, OnDestroy {
15 @ViewChild('accountReportModal') accountReportModal: AccountReportComponent
16
17 account: Account
18 accountUser: User
19 videoChannels: VideoChannel[] = []
20 links: ListOverflowItem[] = []
21
22 isAccountManageable = false
23 accountFollowerTitle = ''
24
25 prependModerationActions: DropdownAction<any>[]
26
27 private routeSub: Subscription
28
29 constructor (
30 private route: ActivatedRoute,
31 private userService: UserService,
32 private accountService: AccountService,
33 private videoChannelService: VideoChannelService,
34 private notifier: Notifier,
35 private restExtractor: RestExtractor,
36 private redirectService: RedirectService,
37 private authService: AuthService,
38 private screenService: ScreenService
39 ) {
40 }
41
42 ngOnInit () {
43 this.routeSub = this.route.params
44 .pipe(
45 map(params => params[ 'accountId' ]),
46 distinctUntilChanged(),
47 switchMap(accountId => this.accountService.getAccount(accountId)),
48 tap(account => this.onAccount(account)),
49 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
50 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
51 )
52 .subscribe(
53 videoChannels => this.videoChannels = videoChannels.data,
54
55 err => this.notifier.error(err.message)
56 )
57
58 this.links = [
59 { label: $localize`VIDEO CHANNELS`, routerLink: 'video-channels' },
60 { label: $localize`VIDEOS`, routerLink: 'videos' },
61 { label: $localize`ABOUT`, routerLink: 'about' }
62 ]
63 }
64
65 ngOnDestroy () {
66 if (this.routeSub) this.routeSub.unsubscribe()
67 }
68
69 get naiveAggregatedSubscribers () {
70 return this.videoChannels.reduce(
71 (acc, val) => acc + val.followersCount,
72 this.account.followersCount // accumulator starts with the base number of subscribers the account has
73 )
74 }
75
76 get isInSmallView () {
77 return this.screenService.isInSmallView()
78 }
79
80 onUserChanged () {
81 this.getUserIfNeeded(this.account)
82 }
83
84 onUserDeleted () {
85 this.redirectService.redirectToHomepage()
86 }
87
88 activateCopiedMessage () {
89 this.notifier.success($localize`Username copied`)
90 }
91
92 subscribersDisplayFor (count: number) {
93 if (count === 1) return $localize`1 subscriber`
94
95 return $localize`${count} subscribers`
96 }
97
98 private onAccount (account: Account) {
99 this.prependModerationActions = undefined
100
101 this.account = account
102
103 if (this.authService.isLoggedIn()) {
104 this.authService.userInformationLoaded.subscribe(
105 () => {
106 this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id
107
108 const followers = this.subscribersDisplayFor(account.followersCount)
109 this.accountFollowerTitle = $localize`${followers} direct account followers`
110
111 // It's not our account, we can report it
112 if (!this.isAccountManageable) {
113 this.prependModerationActions = [
114 {
115 label: $localize`Report this account`,
116 handler: () => this.showReportModal()
117 }
118 ]
119 }
120 }
121 )
122 }
123
124 this.getUserIfNeeded(account)
125 }
126
127 private showReportModal () {
128 this.accountReportModal.show()
129 }
130
131 private getUserIfNeeded (account: Account) {
132 if (!account.userId || !this.authService.isLoggedIn()) return
133
134 const user = this.authService.getUser()
135 if (user.hasRight(UserRight.MANAGE_USERS)) {
136 this.userService.getUser(account.userId).subscribe(
137 accountUser => this.accountUser = accountUser,
138
139 err => this.notifier.error(err.message)
140 )
141 }
142 }
143 }