]>
Commit | Line | Data |
---|---|---|
8b61dcaf C |
1 | import { from } from 'rxjs' |
2 | import { finalize, map, switchMap, tap } from 'rxjs/operators' | |
0ca454e3 | 3 | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' |
8b61dcaf | 4 | import { MarkdownService, Notifier, UserService } from '@app/core' |
3da38d6e | 5 | import { FindInBulkService } from '@app/shared/shared-search' |
61cbafc1 | 6 | import { Video, VideoSortField } from '@shared/models/videos' |
3da38d6e | 7 | import { VideoChannel, VideoService } from '../../shared-main' |
0ca454e3 | 8 | import { 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 | 19 | export 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 C |
41 | .pipe( |
42 | tap(channel => this.channel = channel), | |
43 | switchMap(() => from(this.markdown.textMarkdownToHTML(this.channel.description))), | |
44 | tap(html => this.descriptionHTML = html), | |
45 | switchMap(() => this.loadVideosObservable()), | |
46 | finalize(() => this.loaded.emit(true)) | |
47 | ).subscribe( | |
48 | ({ total, data }) => { | |
49 | this.totalVideos = total | |
50 | this.video = data[0] | |
51 | }, | |
61cbafc1 | 52 | |
8b61dcaf C |
53 | err => this.notifier.error('Error in channel miniature component: ' + err.message) |
54 | ) | |
61cbafc1 C |
55 | } |
56 | ||
57 | getVideoChannelLink () { | |
58 | return [ '/c', this.channel.nameWithHost ] | |
59 | } | |
60 | ||
8b61dcaf | 61 | private loadVideosObservable () { |
61cbafc1 C |
62 | const videoOptions = { |
63 | videoChannel: this.channel, | |
64 | videoPagination: { | |
65 | currentPage: 1, | |
66 | itemsPerPage: 1 | |
67 | }, | |
68 | sort: '-publishedAt' as VideoSortField, | |
69 | count: 1 | |
70 | } | |
71 | ||
8b61dcaf | 72 | return this.userService.getAnonymousOrLoggedUser() |
61cbafc1 C |
73 | .pipe( |
74 | map(user => user.nsfwPolicy), | |
75 | switchMap(nsfwPolicy => this.videoService.getVideoChannelVideos({ ...videoOptions, nsfwPolicy })) | |
76 | ) | |
2539932e C |
77 | } |
78 | } |