]>
Commit | Line | Data |
---|---|---|
1 | import { from } from 'rxjs' | |
2 | import { finalize, map, switchMap, tap } from 'rxjs/operators' | |
3 | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' | |
4 | import { MarkdownService, Notifier, UserService } from '@app/core' | |
5 | import { FindInBulkService } from '@app/shared/shared-search' | |
6 | import { VideoSortField } from '@shared/models' | |
7 | import { Video, VideoChannel, VideoService } from '../../shared-main' | |
8 | import { CustomMarkupComponent } from './shared' | |
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 | }) | |
19 | export class ChannelMiniatureMarkupComponent implements CustomMarkupComponent, OnInit { | |
20 | @Input() name: string | |
21 | @Input() displayLatestVideo: boolean | |
22 | @Input() displayDescription: boolean | |
23 | ||
24 | @Output() loaded = new EventEmitter<boolean>() | |
25 | ||
26 | channel: VideoChannel | |
27 | descriptionHTML: string | |
28 | totalVideos: number | |
29 | video: Video | |
30 | ||
31 | constructor ( | |
32 | private markdown: MarkdownService, | |
33 | private findInBulk: FindInBulkService, | |
34 | private videoService: VideoService, | |
35 | private userService: UserService, | |
36 | private notifier: Notifier | |
37 | ) { } | |
38 | ||
39 | ngOnInit () { | |
40 | this.findInBulk.getChannel(this.name) | |
41 | .pipe( | |
42 | tap(channel => { | |
43 | this.channel = channel | |
44 | }), | |
45 | switchMap(() => from(this.markdown.textMarkdownToHTML(this.channel.description))), | |
46 | tap(html => { | |
47 | this.descriptionHTML = html | |
48 | }), | |
49 | switchMap(() => this.loadVideosObservable()), | |
50 | finalize(() => this.loaded.emit(true)) | |
51 | ).subscribe({ | |
52 | next: ({ total, data }) => { | |
53 | this.totalVideos = total | |
54 | this.video = data[0] | |
55 | }, | |
56 | ||
57 | error: err => this.notifier.error($localize`Error in channel miniature component: ${err.message}`) | |
58 | }) | |
59 | } | |
60 | ||
61 | getVideoChannelLink () { | |
62 | return [ '/c', this.channel.nameWithHost ] | |
63 | } | |
64 | ||
65 | private loadVideosObservable () { | |
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 | ||
76 | return this.userService.getAnonymousOrLoggedUser() | |
77 | .pipe( | |
78 | map(user => user.nsfwPolicy), | |
79 | switchMap(nsfwPolicy => this.videoService.getVideoChannelVideos({ ...videoOptions, nsfwPolicy })) | |
80 | ) | |
81 | } | |
82 | } |