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