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