]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+accounts/accounts.component.ts
Reduce dev commands RAM usage
[github/Chocobozzz/PeerTube.git] / client / src / app / +accounts / accounts.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { AccountService } from '@app/shared/account/account.service'
4 import { Account } from '@app/shared/account/account.model'
5 import { RestExtractor, UserService } from '@app/shared'
6 import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
7 import { Subscription } from 'rxjs'
8 import { AuthService, Notifier, RedirectService } from '@app/core'
9 import { User, UserRight } from '../../../../shared'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
12 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
13
14 @Component({
15 templateUrl: './accounts.component.html',
16 styleUrls: [ './accounts.component.scss' ]
17 })
18 export class AccountsComponent implements OnInit, OnDestroy {
19 account: Account
20 user: User
21 videoChannels: VideoChannel[] = []
22
23 private routeSub: Subscription
24
25 constructor (
26 private route: ActivatedRoute,
27 private userService: UserService,
28 private accountService: AccountService,
29 private videoChannelService: VideoChannelService,
30 private notifier: Notifier,
31 private restExtractor: RestExtractor,
32 private redirectService: RedirectService,
33 private authService: AuthService,
34 private i18n: I18n
35 ) {}
36
37 ngOnInit () {
38 this.routeSub = this.route.params
39 .pipe(
40 map(params => params[ 'accountId' ]),
41 distinctUntilChanged(),
42 switchMap(accountId => this.accountService.getAccount(accountId)),
43 tap(account => {
44 this.account = account
45 this.getUserIfNeeded(account)
46 }),
47 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
48 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
49 )
50 .subscribe(
51 videoChannels => this.videoChannels = videoChannels.data,
52
53 err => this.notifier.error(err.message)
54 )
55 }
56
57 ngOnDestroy () {
58 if (this.routeSub) this.routeSub.unsubscribe()
59 }
60
61 get naiveAggregatedSubscribers () {
62 return this.videoChannels.reduce(
63 (acc, val) => acc + val.followersCount,
64 this.account.followersCount // accumulator starts with the base number of subscribers the account has
65 )
66 }
67
68 get isManageable () {
69 if (!this.authService.isLoggedIn()) return false
70 return this.user.id === this.authService.getUser().id
71 }
72
73 onUserChanged () {
74 this.getUserIfNeeded(this.account)
75 }
76
77 onUserDeleted () {
78 this.redirectService.redirectToHomepage()
79 }
80
81 activateCopiedMessage () {
82 this.notifier.success(this.i18n('Username copied'))
83 }
84
85 subscribersDisplayFor (count: number) {
86 return this.i18n(`{count, plural, =1 {1 subscriber} other {${count} subscribers}}`, { count })
87 }
88
89 private getUserIfNeeded (account: Account) {
90 if (!account.userId) return
91 if (!this.authService.isLoggedIn()) return
92
93 const user = this.authService.getUser()
94 if (user.hasRight(UserRight.MANAGE_USERS)) {
95 this.userService.getUser(account.userId)
96 .subscribe(
97 user => this.user = user,
98
99 err => this.notifier.error(err.message)
100 )
101 }
102 }
103 }