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, 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'
9 selector: 'my-account-video-channels',
10 templateUrl: './account-video-channels.component.html',
11 styleUrls: [ './account-video-channels.component.scss' ]
13 export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
15 videoChannels: VideoChannel[] = []
16 videos: { [id: number]: Video[] } = {}
18 channelPagination: ComponentPagination = {
24 videosPagination: ComponentPagination = {
29 videosSort: VideoSortField = '-publishedAt'
31 onChannelDataSubject = new Subject<any>()
34 nsfwPolicy: NSFWPolicyType
36 private accountSub: Subscription
39 private accountService: AccountService,
40 private videoChannelService: VideoChannelService,
41 private videoService: VideoService,
42 private screenService: ScreenService,
43 private userService: UserService
47 // Parent get the account for us
48 this.accountSub = this.accountService.accountLoaded
49 .subscribe(account => {
50 this.account = account
52 this.loadMoreChannels()
55 this.userService.getAnonymousOrLoggedUser()
57 this.userMiniature = user
59 this.nsfwPolicy = user.nsfwPolicy
64 if (this.accountSub) this.accountSub.unsubscribe()
68 this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination)
70 tap(res => this.channelPagination.totalItems = res.total),
71 switchMap(res => from(res.data)),
72 concatMap(videoChannel => {
75 videoPagination: this.videosPagination,
76 sort: this.videosSort,
77 nsfwPolicy: this.nsfwPolicy
80 return this.videoService.getVideoChannelVideos(options)
81 .pipe(map(data => ({ videoChannel, videos: data.data })))
84 .subscribe(({ videoChannel, videos }) => {
85 this.videoChannels.push(videoChannel)
87 this.videos[videoChannel.id] = videos
89 this.onChannelDataSubject.next([ videoChannel ])
93 getVideosOf (videoChannel: VideoChannel) {
94 const numberOfVideos = this.screenService.getNumberOfAvailableMiniatures()
97 return this.videos[ videoChannel.id ].slice(0, numberOfVideos * 2)
101 if (!hasMoreItems(this.channelPagination)) return
103 this.channelPagination.currentPage += 1
105 this.loadMoreChannels()
108 getVideoChannelLink (videoChannel: VideoChannel) {
109 return [ '/video-channels', videoChannel.nameWithHost ]