]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+accounts/accounts.component.ts
Reduce dev commands RAM usage
[github/Chocobozzz/PeerTube.git] / client / src / app / +accounts / accounts.component.ts
CommitLineData
79bd2632 1import { Component, OnDestroy, OnInit } from '@angular/core'
0626e7af
C
2import { ActivatedRoute } from '@angular/router'
3import { AccountService } from '@app/shared/account/account.service'
4import { Account } from '@app/shared/account/account.model'
79bd2632
C
5import { RestExtractor, UserService } from '@app/shared'
6import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
734a5ceb 7import { Subscription } from 'rxjs'
f8b2c1b4 8import { AuthService, Notifier, RedirectService } from '@app/core'
79bd2632 9import { User, UserRight } from '../../../../shared'
ee1d0dfb 10import { I18n } from '@ngx-translate/i18n-polyfill'
41eb700f
RK
11import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
12import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
0626e7af
C
13
14@Component({
170726f5
C
15 templateUrl: './accounts.component.html',
16 styleUrls: [ './accounts.component.scss' ]
0626e7af 17})
3baf9be2 18export class AccountsComponent implements OnInit, OnDestroy {
6b738c7a 19 account: Account
79bd2632 20 user: User
a004ff17 21 videoChannels: VideoChannel[] = []
0626e7af 22
734a5ceb
C
23 private routeSub: Subscription
24
0626e7af
C
25 constructor (
26 private route: ActivatedRoute,
79bd2632 27 private userService: UserService,
a51bad1a 28 private accountService: AccountService,
41eb700f 29 private videoChannelService: VideoChannelService,
f8b2c1b4 30 private notifier: Notifier,
79bd2632
C
31 private restExtractor: RestExtractor,
32 private redirectService: RedirectService,
ee1d0dfb
C
33 private authService: AuthService,
34 private i18n: I18n
0626e7af
C
35 ) {}
36
37 ngOnInit () {
734a5ceb
C
38 this.routeSub = this.route.params
39 .pipe(
40 map(params => params[ 'accountId' ]),
41 distinctUntilChanged(),
42 switchMap(accountId => this.accountService.getAccount(accountId)),
9270ccf6
RK
43 tap(account => {
44 this.account = account
45 this.getUserIfNeeded(account)
46 }),
47 switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
734a5ceb
C
48 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
49 )
79bd2632 50 .subscribe(
9270ccf6 51 videoChannels => this.videoChannels = videoChannels.data,
79bd2632 52
f8b2c1b4 53 err => this.notifier.error(err.message)
79bd2632 54 )
734a5ceb 55 }
0626e7af 56
734a5ceb
C
57 ngOnDestroy () {
58 if (this.routeSub) this.routeSub.unsubscribe()
0626e7af 59 }
79bd2632 60
a004ff17
RK
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
aa0f1963
RK
68 get isManageable () {
69 if (!this.authService.isLoggedIn()) return false
70 return this.user.id === this.authService.getUser().id
71 }
72
79bd2632
C
73 onUserChanged () {
74 this.getUserIfNeeded(this.account)
75 }
76
77 onUserDeleted () {
78 this.redirectService.redirectToHomepage()
79 }
80
ee1d0dfb
C
81 activateCopiedMessage () {
82 this.notifier.success(this.i18n('Username copied'))
83 }
84
a004ff17
RK
85 subscribersDisplayFor (count: number) {
86 return this.i18n(`{count, plural, =1 {1 subscriber} other {${count} subscribers}}`, { count })
87 }
88
79bd2632
C
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
f8b2c1b4 99 err => this.notifier.error(err.message)
79bd2632
C
100 )
101 }
102 }
0626e7af 103}