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