5ca9581a8d32ae79327db680f7030639b6a2877b
[oweals/peertube.git] / client / src / app / +video-channels / video-channels.component.ts
1 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
4 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
5 import { RestExtractor } from '@app/shared'
6 import { catchError, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators'
7 import { Subscription } from 'rxjs'
8 import { AuthService, Notifier } from '@app/core'
9 import { Hotkey, HotkeysService } from 'angular2-hotkeys'
10 import { SubscribeButtonComponent } from '@app/shared/user-subscription/subscribe-button.component'
11 import { I18n } from '@ngx-translate/i18n-polyfill'
12
13 @Component({
14   templateUrl: './video-channels.component.html',
15   styleUrls: [ './video-channels.component.scss' ]
16 })
17 export class VideoChannelsComponent implements OnInit, OnDestroy {
18   @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent
19
20   videoChannel: VideoChannel
21   hotkeys: Hotkey[]
22   isChannelManageable = false
23
24   private routeSub: Subscription
25
26   constructor (
27     private i18n: I18n,
28     private route: ActivatedRoute,
29     private notifier: Notifier,
30     private authService: AuthService,
31     private videoChannelService: VideoChannelService,
32     private restExtractor: RestExtractor,
33     private hotkeysService: HotkeysService
34   ) { }
35
36   ngOnInit () {
37     this.routeSub = this.route.params
38                         .pipe(
39                           map(params => params[ 'videoChannelName' ]),
40                           distinctUntilChanged(),
41                           switchMap(videoChannelName => this.videoChannelService.getVideoChannel(videoChannelName)),
42                           catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))
43                         )
44                         .subscribe(videoChannel => {
45                           this.videoChannel = videoChannel
46
47                           if (this.authService.isLoggedIn()) {
48                             this.authService.userInformationLoaded
49                               .subscribe(() => {
50                                 const channelUserId = this.videoChannel.ownerAccount.userId
51                                 this.isChannelManageable = channelUserId && channelUserId === this.authService.getUser().id
52                               })
53                           }
54                         })
55
56     this.hotkeys = [
57       new Hotkey('S', (event: KeyboardEvent): boolean => {
58         this.subscribeButton.subscribed ?
59           this.subscribeButton.unsubscribe() :
60           this.subscribeButton.subscribe()
61         return false
62       }, undefined, this.i18n('Subscribe to the account'))
63     ]
64     if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys)
65   }
66
67   ngOnDestroy () {
68     if (this.routeSub) this.routeSub.unsubscribe()
69
70     // Unbind hotkeys
71     if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys)
72   }
73
74   isUserLoggedIn () {
75     return this.authService.isLoggedIn()
76   }
77
78   get isManageable () {
79     if (!this.isUserLoggedIn()) return false
80     return this.videoChannel.ownerAccount.userId === this.authService.getUser().id
81   }
82
83   activateCopiedMessage () {
84     this.notifier.success(this.i18n('Username copied'))
85   }
86 }