]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+accounts/account-video-channels/account-video-channels.component.ts
Skip videos count on client if we don't use it
[github/Chocobozzz/PeerTube.git] / client / src / app / +accounts / account-video-channels / account-video-channels.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute } from '@angular/router'
3 import { Account } from '@app/shared/account/account.model'
4 import { AccountService } from '@app/shared/account/account.service'
5 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
6 import { concatMap, map, switchMap, tap } from 'rxjs/operators'
7 import { from, Subject, Subscription } from 'rxjs'
8 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
9 import { Video } from '@app/shared/video/video.model'
10 import { AuthService } from '@app/core'
11 import { VideoService } from '@app/shared/video/video.service'
12 import { VideoSortField } from '@app/shared/video/sort-field.type'
13 import { ComponentPagination, hasMoreItems } from '@app/shared/rest/component-pagination.model'
14 import { ScreenService } from '@app/shared/misc/screen.service'
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 private accountSub: Subscription
42
43 constructor (
44 private route: ActivatedRoute,
45 private authService: AuthService,
46 private accountService: AccountService,
47 private videoChannelService: VideoChannelService,
48 private videoService: VideoService,
49 private screenService: ScreenService
50 ) { }
51
52 get user () {
53 return this.authService.getUser()
54 }
55
56 ngOnInit () {
57 // Parent get the account for us
58 this.accountSub = this.accountService.accountLoaded
59 .subscribe(account => {
60 this.account = account
61
62 this.loadMoreChannels()
63 })
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 }