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, User, UserService } from '@app/core'
5 import { Account, AccountService, Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
6 import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
7 import { NSFWPolicyType, VideoSortField } from '@shared/models'
8 import { SimpleMemoize } from '@app/helpers'
11 selector: 'my-account-video-channels',
12 templateUrl: './account-video-channels.component.html',
13 styleUrls: [ './account-video-channels.component.scss' ]
15 export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
17 videoChannels: VideoChannel[] = []
19 videos: { [id: number]: { total: number, videos: Video[] } } = {}
21 channelsDescriptionHTML: { [ id: number ]: string } = {}
23 channelPagination: ComponentPagination = {
29 videosPagination: ComponentPagination = {
34 videosSort: VideoSortField = '-publishedAt'
36 onChannelDataSubject = new Subject<any>()
39 nsfwPolicy: NSFWPolicyType
40 miniatureDisplayOptions: MiniatureDisplayOptions = {
51 private accountSub: Subscription
54 private accountService: AccountService,
55 private videoChannelService: VideoChannelService,
56 private videoService: VideoService,
57 private markdown: MarkdownService,
58 private userService: UserService
62 // Parent get the account for us
63 this.accountSub = this.accountService.accountLoaded
64 .subscribe(account => {
65 this.account = account
66 this.videoChannels = []
68 this.loadMoreChannels()
71 this.userService.getAnonymousOrLoggedUser()
73 this.userMiniature = user
75 this.nsfwPolicy = user.nsfwPolicy
80 if (this.accountSub) this.accountSub.unsubscribe()
85 account: this.account,
86 componentPagination: this.channelPagination,
90 this.videoChannelService.listAccountVideoChannels(options)
93 this.channelPagination.totalItems = res.total
95 switchMap(res => from(res.data)),
96 concatMap(videoChannel => {
99 videoPagination: this.videosPagination,
100 sort: this.videosSort,
101 nsfwPolicy: this.nsfwPolicy
104 return this.videoService.getVideoChannelVideos(options)
105 .pipe(map(data => ({ videoChannel, videos: data.data, total: data.total })))
108 .subscribe(async ({ videoChannel, videos, total }) => {
109 this.channelsDescriptionHTML[videoChannel.id] = await this.markdown.textMarkdownToHTML({
110 markdown: videoChannel.description,
115 this.videoChannels.push(videoChannel)
117 this.videos[videoChannel.id] = { videos, total }
119 this.onChannelDataSubject.next([ videoChannel ])
123 getVideosOf (videoChannel: VideoChannel) {
124 const obj = this.videos[videoChannel.id]
130 getTotalVideosOf (videoChannel: VideoChannel) {
131 const obj = this.videos[videoChannel.id]
132 if (!obj) return undefined
137 getChannelDescription (videoChannel: VideoChannel) {
138 return this.channelsDescriptionHTML[videoChannel.id]
142 if (!hasMoreItems(this.channelPagination)) return
144 this.channelPagination.currentPage += 1
146 this.loadMoreChannels()
150 getVideoChannelLink (videoChannel: VideoChannel) {
151 return [ '/c', videoChannel.nameWithHost ]