Properly hide Watch Later when user logs out
[oweals/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, first, map, switchMap, tap } from 'rxjs/operators'
7 import { forkJoin, 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 import { ListOverflowItem } from '@app/shared/misc/list-overflow.component'
14
15 @Component({
16   templateUrl: './accounts.component.html',
17   styleUrls: [ './accounts.component.scss' ]
18 })
19 export class AccountsComponent implements OnInit, OnDestroy {
20   account: Account
21   accountUser: User
22   videoChannels: VideoChannel[] = []
23   links: ListOverflowItem[] = []
24
25   isAccountManageable = false
26   accountFollowerTitle = ''
27
28   private routeSub: Subscription
29
30   constructor (
31     private route: ActivatedRoute,
32     private userService: UserService,
33     private accountService: AccountService,
34     private videoChannelService: VideoChannelService,
35     private notifier: Notifier,
36     private restExtractor: RestExtractor,
37     private redirectService: RedirectService,
38     private authService: AuthService,
39     private i18n: I18n
40   ) {
41   }
42
43   ngOnInit () {
44     this.routeSub = this.route.params
45                         .pipe(
46                           map(params => params[ 'accountId' ]),
47                           distinctUntilChanged(),
48                           switchMap(accountId => this.accountService.getAccount(accountId)),
49                           tap(account => {
50                             this.account = account
51
52                             if (this.authService.isLoggedIn()) {
53                               this.authService.userInformationLoaded.subscribe(
54                                 () => {
55                                   this.isAccountManageable = this.account.userId && this.account.userId === this.authService.getUser().id
56
57                                   this.accountFollowerTitle = this.i18n(
58                                     '{{followers}} direct account followers',
59                                     { followers: this.subscribersDisplayFor(account.followersCount) }
60                                   )
61                                 }
62                               )
63                             }
64
65                             this.getUserIfNeeded(account)
66                           }),
67                           switchMap(account => this.videoChannelService.listAccountVideoChannels(account)),
68                           catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
69                         )
70                         .subscribe(
71                           videoChannels => this.videoChannels = videoChannels.data,
72
73                           err => this.notifier.error(err.message)
74                         )
75
76     this.links = [
77       { label: this.i18n('Video channels'), routerLink: 'video-channels' },
78       { label: this.i18n('Videos'), routerLink: 'videos' },
79       { label: this.i18n('About'), routerLink: 'about' }
80     ]
81   }
82
83   ngOnDestroy () {
84     if (this.routeSub) this.routeSub.unsubscribe()
85   }
86
87   get naiveAggregatedSubscribers () {
88     return this.videoChannels.reduce(
89       (acc, val) => acc + val.followersCount,
90       this.account.followersCount // accumulator starts with the base number of subscribers the account has
91     )
92   }
93
94   onUserChanged () {
95     this.getUserIfNeeded(this.account)
96   }
97
98   onUserDeleted () {
99     this.redirectService.redirectToHomepage()
100   }
101
102   activateCopiedMessage () {
103     this.notifier.success(this.i18n('Username copied'))
104   }
105
106   subscribersDisplayFor (count: number) {
107     return this.i18n('{count, plural, =1 {1 subscriber} other {{{count}} subscribers}}', { count })
108   }
109
110   private getUserIfNeeded (account: Account) {
111     if (!account.userId || !this.authService.isLoggedIn()) return
112
113     const user = this.authService.getUser()
114     if (user.hasRight(UserRight.MANAGE_USERS)) {
115       this.userService.getUser(account.userId).subscribe(
116         accountUser => this.accountUser = accountUser,
117
118         err => this.notifier.error(err.message)
119       )
120     }
121   }
122 }