2e5c5aae216f33f3a8adfd1368607f0c028dd6a4
[oweals/peertube.git] / client / src / app / +accounts / account-video-channels / account-video-channels.component.ts
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 { UserService } from '@app/shared'
5 import { Account } from '@app/shared/account/account.model'
6 import { AccountService } from '@app/shared/account/account.service'
7 import { ScreenService } from '@app/shared/misc/screen.service'
8 import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
9 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
10 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
11 import { VideoSortField } from '@app/shared/video/sort-field.type'
12 import { Video } from '@app/shared/video/video.model'
13 import { VideoService } from '@app/shared/video/video.service'
14 import { User } from '@shared/models'
15
16 @Component({
17   selector: 'my-account-video-channels',
18   templateUrl: './account-video-channels.component.html',
19   styleUrls: [ './account-video-channels.component.scss' ]
20 })
21 export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
22   account: Account
23   videoChannels: VideoChannel[] = []
24   videos: { [id: number]: Video[] } = {}
25
26   channelPagination: ComponentPagination = {
27     currentPage: 1,
28     itemsPerPage: 2,
29     totalItems: null
30   }
31
32   videosPagination: ComponentPagination = {
33     currentPage: 1,
34     itemsPerPage: 12,
35     totalItems: null
36   }
37   videosSort: VideoSortField = '-publishedAt'
38
39   onChannelDataSubject = new Subject<any>()
40
41   userMiniature: User
42
43   private accountSub: Subscription
44
45   constructor (
46     private accountService: AccountService,
47     private videoChannelService: VideoChannelService,
48     private videoService: VideoService,
49     private screenService: ScreenService,
50     private userService: UserService
51   ) { }
52
53   ngOnInit () {
54     // Parent get the account for us
55     this.accountSub = this.accountService.accountLoaded
56         .subscribe(account => {
57           this.account = account
58
59           this.loadMoreChannels()
60         })
61
62     this.userService.getAnonymousOrLoggedUser()
63       .subscribe(user => this.userMiniature = user)
64   }
65
66   ngOnDestroy () {
67     if (this.accountSub) this.accountSub.unsubscribe()
68   }
69
70   loadMoreChannels () {
71     this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination)
72       .pipe(
73         tap(res => this.channelPagination.totalItems = res.total),
74         switchMap(res => from(res.data)),
75         concatMap(videoChannel => {
76           return this.videoService.getVideoChannelVideos(videoChannel, this.videosPagination, this.videosSort)
77             .pipe(map(data => ({ videoChannel, videos: data.data })))
78         })
79       )
80       .subscribe(({ videoChannel, videos }) => {
81         this.videoChannels.push(videoChannel)
82
83         this.videos[videoChannel.id] = videos
84
85         this.onChannelDataSubject.next([ videoChannel ])
86       })
87   }
88
89   getVideosOf (videoChannel: VideoChannel) {
90     const numberOfVideos = this.screenService.getNumberOfAvailableMiniatures()
91
92     // 2 rows
93     return this.videos[ videoChannel.id ].slice(0, numberOfVideos * 2)
94   }
95
96   onNearOfBottom () {
97     if (!hasMoreItems(this.channelPagination)) return
98
99     this.channelPagination.currentPage += 1
100
101     this.loadMoreChannels()
102   }
103
104   getVideoChannelLink (videoChannel: VideoChannel) {
105     return [ '/video-channels', videoChannel.nameWithHost ]
106   }
107 }