]>
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 { Video, VideoSortField } from '@shared/models/videos' | |
6 | import { VideoChannel, VideoChannelService, VideoService } from '../../shared-main' | |
7 | import { CustomMarkupComponent } from './shared' | |
8 | ||
9 | /* | |
10 | * Markup component that creates a channel miniature only | |
11 | */ | |
12 | ||
13 | @Component({ | |
14 | selector: 'my-channel-miniature-markup', | |
15 | templateUrl: 'channel-miniature-markup.component.html', | |
16 | styleUrls: [ 'channel-miniature-markup.component.scss' ] | |
17 | }) | |
18 | export class ChannelMiniatureMarkupComponent implements CustomMarkupComponent, OnInit { | |
19 | @Input() name: string | |
20 | @Input() displayLatestVideo: boolean | |
21 | @Input() displayDescription: boolean | |
22 | ||
23 | @Output() loaded = new EventEmitter<boolean>() | |
24 | ||
25 | channel: VideoChannel | |
26 | descriptionHTML: string | |
27 | totalVideos: number | |
28 | video: Video | |
29 | ||
30 | constructor ( | |
31 | private markdown: MarkdownService, | |
32 | private channelService: VideoChannelService, | |
33 | private videoService: VideoService, | |
34 | private userService: UserService, | |
35 | private notifier: Notifier | |
36 | ) { } | |
37 | ||
38 | ngOnInit () { | |
39 | this.channelService.getVideoChannel(this.name) | |
40 | .pipe( | |
41 | tap(channel => this.channel = channel), | |
42 | switchMap(() => from(this.markdown.textMarkdownToHTML(this.channel.description))), | |
43 | tap(html => this.descriptionHTML = html), | |
44 | switchMap(() => this.loadVideosObservable()), | |
45 | finalize(() => this.loaded.emit(true)) | |
46 | ).subscribe( | |
47 | ({ total, data }) => { | |
48 | this.totalVideos = total | |
49 | this.video = data[0] | |
50 | }, | |
51 | ||
52 | err => this.notifier.error('Error in channel miniature component: ' + err.message) | |
53 | ) | |
54 | } | |
55 | ||
56 | getVideoChannelLink () { | |
57 | return [ '/c', this.channel.nameWithHost ] | |
58 | } | |
59 | ||
60 | private loadVideosObservable () { | |
61 | const videoOptions = { | |
62 | videoChannel: this.channel, | |
63 | videoPagination: { | |
64 | currentPage: 1, | |
65 | itemsPerPage: 1 | |
66 | }, | |
67 | sort: '-publishedAt' as VideoSortField, | |
68 | count: 1 | |
69 | } | |
70 | ||
71 | return this.userService.getAnonymousOrLoggedUser() | |
72 | .pipe( | |
73 | map(user => user.nsfwPolicy), | |
74 | switchMap(nsfwPolicy => this.videoService.getVideoChannelVideos({ ...videoOptions, nsfwPolicy })) | |
75 | ) | |
76 | } | |
77 | } |