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