]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-custom-markup/peertube-custom-tags/channel-miniature-markup.component.ts
Optimize channel with video in homepage
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-custom-markup / peertube-custom-tags / channel-miniature-markup.component.ts
CommitLineData
8b61dcaf
C
1import { from } from 'rxjs'
2import { finalize, map, switchMap, tap } from 'rxjs/operators'
0ca454e3 3import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
8b61dcaf 4import { MarkdownService, Notifier, UserService } from '@app/core'
3da38d6e 5import { FindInBulkService } from '@app/shared/shared-search'
e9609325
C
6import { VideoSortField } from '@shared/models'
7import { Video, VideoChannel, VideoService } from '../../shared-main'
0ca454e3 8import { CustomMarkupComponent } from './shared'
2539932e
C
9
10/*
11 * Markup component that creates a channel miniature only
12*/
13
14@Component({
15 selector: 'my-channel-miniature-markup',
16 templateUrl: 'channel-miniature-markup.component.html',
17 styleUrls: [ 'channel-miniature-markup.component.scss' ]
18})
0ca454e3 19export class ChannelMiniatureMarkupComponent implements CustomMarkupComponent, OnInit {
2539932e 20 @Input() name: string
61cbafc1
C
21 @Input() displayLatestVideo: boolean
22 @Input() displayDescription: boolean
2539932e 23
0ca454e3
C
24 @Output() loaded = new EventEmitter<boolean>()
25
2539932e 26 channel: VideoChannel
61cbafc1
C
27 descriptionHTML: string
28 totalVideos: number
29 video: Video
2539932e
C
30
31 constructor (
61cbafc1 32 private markdown: MarkdownService,
3da38d6e 33 private findInBulk: FindInBulkService,
61cbafc1 34 private videoService: VideoService,
8b61dcaf
C
35 private userService: UserService,
36 private notifier: Notifier
2539932e
C
37 ) { }
38
39 ngOnInit () {
3da38d6e 40 this.findInBulk.getChannel(this.name)
8b61dcaf 41 .pipe(
9df52d66
C
42 tap(channel => {
43 this.channel = channel
44 }),
8b61dcaf 45 switchMap(() => from(this.markdown.textMarkdownToHTML(this.channel.description))),
9df52d66
C
46 tap(html => {
47 this.descriptionHTML = html
48 }),
8b61dcaf
C
49 switchMap(() => this.loadVideosObservable()),
50 finalize(() => this.loaded.emit(true))
1378c0d3
C
51 ).subscribe({
52 next: ({ total, data }) => {
8b61dcaf
C
53 this.totalVideos = total
54 this.video = data[0]
55 },
61cbafc1 56
1378c0d3
C
57 error: err => this.notifier.error($localize`Error in channel miniature component: ${err.message}`)
58 })
61cbafc1
C
59 }
60
61 getVideoChannelLink () {
62 return [ '/c', this.channel.nameWithHost ]
63 }
64
8b61dcaf 65 private loadVideosObservable () {
61cbafc1
C
66 const videoOptions = {
67 videoChannel: this.channel,
68 videoPagination: {
69 currentPage: 1,
70 itemsPerPage: 1
71 },
72 sort: '-publishedAt' as VideoSortField,
73 count: 1
74 }
75
8b61dcaf 76 return this.userService.getAnonymousOrLoggedUser()
61cbafc1
C
77 .pipe(
78 map(user => user.nsfwPolicy),
79 switchMap(nsfwPolicy => this.videoService.getVideoChannelVideos({ ...videoOptions, nsfwPolicy }))
80 )
2539932e
C
81 }
82}