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