1 import { Hotkey, HotkeysService } from 'angular2-hotkeys'
2 import { Subscription } from 'rxjs'
3 import { catchError, distinctUntilChanged, map, switchMap } from 'rxjs/operators'
4 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
5 import { ActivatedRoute } from '@angular/router'
6 import { AuthService, MarkdownService, Notifier, RestExtractor, ScreenService } from '@app/core'
7 import { Account, ListOverflowItem, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
8 import { BlocklistService } from '@app/shared/shared-moderation'
9 import { SupportModalComponent } from '@app/shared/shared-support-modal'
10 import { SubscribeButtonComponent } from '@app/shared/shared-user-subscription'
11 import { HttpStatusCode, UserRight } from '@shared/models'
14 templateUrl: './video-channels.component.html',
15 styleUrls: [ './video-channels.component.scss' ]
17 export class VideoChannelsComponent implements OnInit, OnDestroy {
18 @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent
19 @ViewChild('supportModal') supportModal: SupportModalComponent
21 videoChannel: VideoChannel
24 links: ListOverflowItem[] = []
25 isChannelManageable = false
27 channelVideosCount: number
28 ownerDescriptionHTML = ''
29 channelDescriptionHTML = ''
30 channelDescriptionExpanded = false
32 private routeSub: Subscription
35 private route: ActivatedRoute,
36 private notifier: Notifier,
37 private authService: AuthService,
38 private videoChannelService: VideoChannelService,
39 private videoService: VideoService,
40 private restExtractor: RestExtractor,
41 private hotkeysService: HotkeysService,
42 private screenService: ScreenService,
43 private markdown: MarkdownService,
44 private blocklist: BlocklistService
48 this.routeSub = this.route.params
50 map(params => params['videoChannelName']),
51 distinctUntilChanged(),
52 switchMap(videoChannelName => this.videoChannelService.getVideoChannel(videoChannelName)),
53 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, 'other', [
54 HttpStatusCode.BAD_REQUEST_400,
55 HttpStatusCode.NOT_FOUND_404
58 .subscribe(async videoChannel => {
59 this.channelDescriptionHTML = await this.markdown.textMarkdownToHTML({
60 markdown: videoChannel.description,
65 this.ownerDescriptionHTML = await this.markdown.textMarkdownToHTML({
66 markdown: videoChannel.ownerAccount.description,
71 // After the markdown renderer to avoid layout changes
72 this.videoChannel = videoChannel
73 this.ownerAccount = new Account(this.videoChannel.ownerAccount)
75 this.loadChannelVideosCount()
76 this.loadOwnerBlockStatus()
80 new Hotkey('S', (event: KeyboardEvent): boolean => {
81 if (this.subscribeButton.subscribed) this.subscribeButton.unsubscribe()
82 else this.subscribeButton.subscribe()
85 }, undefined, $localize`Subscribe to the account`)
87 if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys)
90 { label: $localize`VIDEOS`, routerLink: 'videos' },
91 { label: $localize`PLAYLISTS`, routerLink: 'video-playlists' }
96 if (this.routeSub) this.routeSub.unsubscribe()
99 if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys)
103 return this.screenService.isInSmallView()
107 return this.authService.isLoggedIn()
111 if (!this.isUserLoggedIn()) return false
113 return this.videoChannel?.ownerAccount.userId === this.authService.getUser().id
117 if (!this.videoChannel.isLocal) return false
118 if (!this.isUserLoggedIn()) return false
120 return this.isOwner() || this.authService.getUser().hasRight(UserRight.MANAGE_ANY_VIDEO_CHANNEL)
123 activateCopiedMessage () {
124 this.notifier.success($localize`Username copied`)
127 hasShowMoreDescription () {
128 return !this.channelDescriptionExpanded && this.channelDescriptionHTML.length > 100
131 showSupportModal () {
132 this.supportModal.show()
136 return [ '/a', this.videoChannel.ownerBy ]
139 private loadChannelVideosCount () {
140 this.videoService.getVideoChannelVideos({
141 videoChannel: this.videoChannel,
147 }).subscribe(res => this.channelVideosCount = res.total)
150 private loadOwnerBlockStatus () {
151 this.blocklist.getStatus({ accounts: [ this.ownerAccount.nameWithHostForced ], hosts: [ this.ownerAccount.host ] })
152 .subscribe(status => this.ownerAccount.updateBlockStatus(status))