1 import { from, Subject, Subscription } from 'rxjs'
2 import { concatMap, map, switchMap, tap } from 'rxjs/operators'
3 import { Component, OnDestroy, OnInit } from '@angular/core'
4 import { ComponentPagination, hasMoreItems, MarkdownService, ScreenService, User, UserService } from '@app/core'
5 import { Account, AccountService, Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
6 import { NSFWPolicyType, VideoSortField } from '@shared/models'
7 import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
10 selector: 'my-account-video-channels',
11 templateUrl: './account-video-channels.component.html',
12 styleUrls: [ './account-video-channels.component.scss' ]
14 export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
16 videoChannels: VideoChannel[] = []
18 videos: { [id: number]: { total: number, videos: Video[] } } = {}
20 channelsDescriptionHTML: { [ id: number ]: string } = {}
22 channelPagination: ComponentPagination = {
28 videosPagination: ComponentPagination = {
33 videosSort: VideoSortField = '-publishedAt'
35 onChannelDataSubject = new Subject<any>()
38 nsfwPolicy: NSFWPolicyType
39 miniatureDisplayOptions: MiniatureDisplayOptions = {
50 private accountSub: Subscription
53 private accountService: AccountService,
54 private videoChannelService: VideoChannelService,
55 private videoService: VideoService,
56 private markdown: MarkdownService,
57 private userService: UserService
61 // Parent get the account for us
62 this.accountSub = this.accountService.accountLoaded
63 .subscribe(account => {
64 this.account = account
66 this.loadMoreChannels()
69 this.userService.getAnonymousOrLoggedUser()
71 this.userMiniature = user
73 this.nsfwPolicy = user.nsfwPolicy
78 if (this.accountSub) this.accountSub.unsubscribe()
83 account: this.account,
84 componentPagination: this.channelPagination,
88 this.videoChannelService.listAccountVideoChannels(options)
90 tap(res => this.channelPagination.totalItems = res.total),
91 switchMap(res => from(res.data)),
92 concatMap(videoChannel => {
95 videoPagination: this.videosPagination,
96 sort: this.videosSort,
97 nsfwPolicy: this.nsfwPolicy
100 return this.videoService.getVideoChannelVideos(options)
101 .pipe(map(data => ({ videoChannel, videos: data.data, total: data.total })))
104 .subscribe(async ({ videoChannel, videos, total }) => {
105 this.channelsDescriptionHTML[videoChannel.id] = await this.markdown.textMarkdownToHTML(videoChannel.description)
107 this.videoChannels.push(videoChannel)
109 this.videos[videoChannel.id] = { videos, total }
111 this.onChannelDataSubject.next([ videoChannel ])
115 getVideosOf (videoChannel: VideoChannel) {
116 const obj = this.videos[ videoChannel.id ]
122 getTotalVideosOf (videoChannel: VideoChannel) {
123 const obj = this.videos[ videoChannel.id ]
124 if (!obj) return undefined
129 getChannelDescription (videoChannel: VideoChannel) {
130 return this.channelsDescriptionHTML[videoChannel.id]
134 if (!hasMoreItems(this.channelPagination)) return
136 this.channelPagination.currentPage += 1
138 this.loadMoreChannels()
141 getVideoChannelLink (videoChannel: VideoChannel) {
142 return [ '/video-channels', videoChannel.nameWithHost ]