]>
Commit | Line | Data |
---|---|---|
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 | import { ListOverflowItem } from '@app/shared/misc/list-overflow.component' | |
13 | import { ScreenService } from '@app/shared/misc/screen.service' | |
14 | ||
15 | @Component({ | |
16 | templateUrl: './video-channels.component.html', | |
17 | styleUrls: [ './video-channels.component.scss' ] | |
18 | }) | |
19 | export class VideoChannelsComponent implements OnInit, OnDestroy { | |
20 | @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent | |
21 | ||
22 | videoChannel: VideoChannel | |
23 | hotkeys: Hotkey[] | |
24 | links: ListOverflowItem[] = [] | |
25 | isChannelManageable = false | |
26 | ||
27 | private routeSub: Subscription | |
28 | ||
29 | constructor ( | |
30 | private i18n: I18n, | |
31 | private route: ActivatedRoute, | |
32 | private notifier: Notifier, | |
33 | private authService: AuthService, | |
34 | private videoChannelService: VideoChannelService, | |
35 | private restExtractor: RestExtractor, | |
36 | private hotkeysService: HotkeysService, | |
37 | private screenService: ScreenService | |
38 | ) { } | |
39 | ||
40 | ngOnInit () { | |
41 | this.routeSub = this.route.params | |
42 | .pipe( | |
43 | map(params => params[ 'videoChannelName' ]), | |
44 | distinctUntilChanged(), | |
45 | switchMap(videoChannelName => this.videoChannelService.getVideoChannel(videoChannelName)), | |
46 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])) | |
47 | ) | |
48 | .subscribe(videoChannel => { | |
49 | this.videoChannel = videoChannel | |
50 | ||
51 | if (this.authService.isLoggedIn()) { | |
52 | this.authService.userInformationLoaded | |
53 | .subscribe(() => { | |
54 | const channelUserId = this.videoChannel.ownerAccount.userId | |
55 | this.isChannelManageable = channelUserId && channelUserId === this.authService.getUser().id | |
56 | }) | |
57 | } | |
58 | }) | |
59 | ||
60 | this.hotkeys = [ | |
61 | new Hotkey('S', (event: KeyboardEvent): boolean => { | |
62 | this.subscribeButton.subscribed ? | |
63 | this.subscribeButton.unsubscribe() : | |
64 | this.subscribeButton.subscribe() | |
65 | return false | |
66 | }, undefined, this.i18n('Subscribe to the account')) | |
67 | ] | |
68 | if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys) | |
69 | ||
70 | this.links = [ | |
71 | { label: this.i18n('VIDEOS'), routerLink: 'videos' }, | |
72 | { label: this.i18n('VIDEO PLAYLISTS'), routerLink: 'video-playlists' }, | |
73 | { label: this.i18n('ABOUT'), routerLink: 'about' } | |
74 | ] | |
75 | } | |
76 | ||
77 | ngOnDestroy () { | |
78 | if (this.routeSub) this.routeSub.unsubscribe() | |
79 | ||
80 | // Unbind hotkeys | |
81 | if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys) | |
82 | } | |
83 | ||
84 | get isInSmallView () { | |
85 | return this.screenService.isInSmallView() | |
86 | } | |
87 | ||
88 | isUserLoggedIn () { | |
89 | return this.authService.isLoggedIn() | |
90 | } | |
91 | ||
92 | get isManageable () { | |
93 | if (!this.isUserLoggedIn()) return false | |
94 | return this.videoChannel.ownerAccount.userId === this.authService.getUser().id | |
95 | } | |
96 | ||
97 | activateCopiedMessage () { | |
98 | this.notifier.success(this.i18n('Username copied')) | |
99 | } | |
100 | } |