]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
2 | import { Subscription } from 'rxjs' | |
3 | import { catchError, distinctUntilChanged, map, switchMap } from 'rxjs/operators' | |
20d21199 | 4 | import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core' |
170726f5 | 5 | import { ActivatedRoute } from '@angular/router' |
60c35932 C |
6 | import { AuthService, MarkdownService, Notifier, RestExtractor, ScreenService } from '@app/core' |
7 | import { ListOverflowItem, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main' | |
100d9ce2 | 8 | import { SupportModalComponent } from '@app/shared/shared-support-modal' |
67ed6552 | 9 | import { SubscribeButtonComponent } from '@app/shared/shared-user-subscription' |
c0e8b12e | 10 | import { HttpStatusCode } from '@shared/models' |
170726f5 C |
11 | |
12 | @Component({ | |
13 | templateUrl: './video-channels.component.html', | |
14 | styleUrls: [ './video-channels.component.scss' ] | |
15 | }) | |
734a5ceb | 16 | export class VideoChannelsComponent implements OnInit, OnDestroy { |
2f5d2ec5 | 17 | @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent |
100d9ce2 | 18 | @ViewChild('supportModal') supportModal: SupportModalComponent |
20d21199 | 19 | |
170726f5 | 20 | videoChannel: VideoChannel |
20d21199 | 21 | hotkeys: Hotkey[] |
24e7916c | 22 | links: ListOverflowItem[] = [] |
fef213ca | 23 | isChannelManageable = false |
170726f5 | 24 | |
60c35932 C |
25 | channelVideosCount: number |
26 | ownerDescriptionHTML = '' | |
27 | channelDescriptionHTML = '' | |
28 | channelDescriptionExpanded = false | |
29 | ||
734a5ceb C |
30 | private routeSub: Subscription |
31 | ||
170726f5 C |
32 | constructor ( |
33 | private route: ActivatedRoute, | |
a0dedc02 | 34 | private notifier: Notifier, |
2d3741d6 | 35 | private authService: AuthService, |
a51bad1a | 36 | private videoChannelService: VideoChannelService, |
60c35932 | 37 | private videoService: VideoService, |
20d21199 | 38 | private restExtractor: RestExtractor, |
937b7a6a | 39 | private hotkeysService: HotkeysService, |
60c35932 C |
40 | private screenService: ScreenService, |
41 | private markdown: MarkdownService | |
734a5ceb | 42 | ) { } |
170726f5 C |
43 | |
44 | ngOnInit () { | |
734a5ceb C |
45 | this.routeSub = this.route.params |
46 | .pipe( | |
9df52d66 | 47 | map(params => params['videoChannelName']), |
734a5ceb | 48 | distinctUntilChanged(), |
2f1548fd | 49 | switchMap(videoChannelName => this.videoChannelService.getVideoChannel(videoChannelName)), |
ab398a05 | 50 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [ |
f2eb23cd RK |
51 | HttpStatusCode.BAD_REQUEST_400, |
52 | HttpStatusCode.NOT_FOUND_404 | |
53 | ])) | |
734a5ceb | 54 | ) |
60c35932 C |
55 | .subscribe(async videoChannel => { |
56 | this.channelDescriptionHTML = await this.markdown.textMarkdownToHTML(videoChannel.description) | |
57 | this.ownerDescriptionHTML = await this.markdown.textMarkdownToHTML(videoChannel.ownerAccount.description) | |
58 | ||
59 | // After the markdown renderer to avoid layout changes | |
fef213ca C |
60 | this.videoChannel = videoChannel |
61 | ||
60c35932 | 62 | this.loadChannelVideosCount() |
fef213ca | 63 | }) |
734a5ceb | 64 | |
20d21199 RK |
65 | this.hotkeys = [ |
66 | new Hotkey('S', (event: KeyboardEvent): boolean => { | |
9df52d66 C |
67 | if (this.subscribeButton.subscribed) this.subscribeButton.unsubscribe() |
68 | else this.subscribeButton.subscribe() | |
69 | ||
20d21199 | 70 | return false |
66357162 | 71 | }, undefined, $localize`Subscribe to the account`) |
20d21199 RK |
72 | ] |
73 | if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys) | |
24e7916c RK |
74 | |
75 | this.links = [ | |
66357162 | 76 | { label: $localize`VIDEOS`, routerLink: 'videos' }, |
0a25749f | 77 | { label: $localize`PLAYLISTS`, routerLink: 'video-playlists' } |
24e7916c | 78 | ] |
734a5ceb | 79 | } |
170726f5 | 80 | |
734a5ceb C |
81 | ngOnDestroy () { |
82 | if (this.routeSub) this.routeSub.unsubscribe() | |
20d21199 RK |
83 | |
84 | // Unbind hotkeys | |
85 | if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys) | |
170726f5 | 86 | } |
2d3741d6 | 87 | |
60c35932 | 88 | isInSmallView () { |
937b7a6a RK |
89 | return this.screenService.isInSmallView() |
90 | } | |
91 | ||
2d3741d6 C |
92 | isUserLoggedIn () { |
93 | return this.authService.isLoggedIn() | |
94 | } | |
a0dedc02 | 95 | |
60c35932 | 96 | isManageable () { |
aa0f1963 | 97 | if (!this.isUserLoggedIn()) return false |
60c35932 | 98 | |
67264e06 | 99 | return this.videoChannel?.ownerAccount.userId === this.authService.getUser().id |
aa0f1963 RK |
100 | } |
101 | ||
a0dedc02 | 102 | activateCopiedMessage () { |
66357162 | 103 | this.notifier.success($localize`Username copied`) |
a0dedc02 | 104 | } |
60c35932 | 105 | |
733dbc53 C |
106 | hasShowMoreDescription () { |
107 | return !this.channelDescriptionExpanded && this.channelDescriptionHTML.length > 100 | |
108 | } | |
109 | ||
100d9ce2 C |
110 | showSupportModal () { |
111 | this.supportModal.show() | |
112 | } | |
113 | ||
733dbc53 | 114 | getAccountUrl () { |
71887396 | 115 | return [ '/a', this.videoChannel.ownerBy ] |
733dbc53 C |
116 | } |
117 | ||
60c35932 C |
118 | private loadChannelVideosCount () { |
119 | this.videoService.getVideoChannelVideos({ | |
120 | videoChannel: this.videoChannel, | |
121 | videoPagination: { | |
122 | currentPage: 1, | |
123 | itemsPerPage: 0 | |
124 | }, | |
125 | sort: '-publishedAt' | |
126 | }).subscribe(res => this.channelVideosCount = res.total) | |
127 | } | |
170726f5 | 128 | } |