]> 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 custom markup angular tags
[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'
bc48e33b 3import { ChangeDetectionStrategy, 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',
bc48e33b
C
17 styleUrls: [ 'channel-miniature-markup.component.scss' ],
18 changeDetection: ChangeDetectionStrategy.OnPush
2539932e 19})
0ca454e3 20export class ChannelMiniatureMarkupComponent implements CustomMarkupComponent, OnInit {
2539932e 21 @Input() name: string
61cbafc1
C
22 @Input() displayLatestVideo: boolean
23 @Input() displayDescription: boolean
2539932e 24
0ca454e3
C
25 @Output() loaded = new EventEmitter<boolean>()
26
2539932e 27 channel: VideoChannel
61cbafc1
C
28 descriptionHTML: string
29 totalVideos: number
30 video: Video
2539932e
C
31
32 constructor (
61cbafc1 33 private markdown: MarkdownService,
3da38d6e 34 private findInBulk: FindInBulkService,
61cbafc1 35 private videoService: VideoService,
8b61dcaf
C
36 private userService: UserService,
37 private notifier: Notifier
2539932e
C
38 ) { }
39
40 ngOnInit () {
3da38d6e 41 this.findInBulk.getChannel(this.name)
8b61dcaf 42 .pipe(
9df52d66
C
43 tap(channel => {
44 this.channel = channel
45 }),
0e45e336
C
46 switchMap(() => from(this.markdown.textMarkdownToHTML({
47 markdown: this.channel.description,
48 withEmoji: true,
49 withHtml: true
50 }))),
9df52d66
C
51 tap(html => {
52 this.descriptionHTML = html
53 }),
8b61dcaf
C
54 switchMap(() => this.loadVideosObservable()),
55 finalize(() => this.loaded.emit(true))
1378c0d3
C
56 ).subscribe({
57 next: ({ total, data }) => {
8b61dcaf
C
58 this.totalVideos = total
59 this.video = data[0]
60 },
61cbafc1 61
1378c0d3
C
62 error: err => this.notifier.error($localize`Error in channel miniature component: ${err.message}`)
63 })
61cbafc1
C
64 }
65
66 getVideoChannelLink () {
67 return [ '/c', this.channel.nameWithHost ]
68 }
69
8b61dcaf 70 private loadVideosObservable () {
61cbafc1
C
71 const videoOptions = {
72 videoChannel: this.channel,
73 videoPagination: {
74 currentPage: 1,
75 itemsPerPage: 1
76 },
77 sort: '-publishedAt' as VideoSortField,
78 count: 1
79 }
80
8b61dcaf 81 return this.userService.getAnonymousOrLoggedUser()
61cbafc1
C
82 .pipe(
83 map(user => user.nsfwPolicy),
84 switchMap(nsfwPolicy => this.videoService.getVideoChannelVideos({ ...videoOptions, nsfwPolicy }))
85 )
2539932e
C
86 }
87}