]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+accounts/account-video-channels/account-video-channels.component.ts
Move to stylelint
[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'
900f7820 4import { ComponentPagination, hasMoreItems, MarkdownService, ScreenService, User, UserService } from '@app/core'
67ed6552 5import { Account, AccountService, Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
0aa52e17 6import { NSFWPolicyType, VideoSortField } from '@shared/models'
900f7820 7import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
d3e91a5f
C
8
9@Component({
10 selector: 'my-account-video-channels',
11 templateUrl: './account-video-channels.component.html',
12 styleUrls: [ './account-video-channels.component.scss' ]
13})
734a5ceb 14export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
d3e91a5f
C
15 account: Account
16 videoChannels: VideoChannel[] = []
900f7820
C
17
18 videos: { [id: number]: { total: number, videos: Video[] } } = {}
19
20 channelsDescriptionHTML: { [ id: number ]: string } = {}
c8487f3f
C
21
22 channelPagination: ComponentPagination = {
23 currentPage: 1,
440d39c5
C
24 itemsPerPage: 2,
25 totalItems: null
c8487f3f
C
26 }
27
28 videosPagination: ComponentPagination = {
29 currentPage: 1,
900f7820 30 itemsPerPage: 5,
440d39c5 31 totalItems: null
c8487f3f
C
32 }
33 videosSort: VideoSortField = '-publishedAt'
d3e91a5f 34
ad453580
C
35 onChannelDataSubject = new Subject<any>()
36
5c20a455 37 userMiniature: User
0aa52e17 38 nsfwPolicy: NSFWPolicyType
900f7820
C
39 miniatureDisplayOptions: MiniatureDisplayOptions = {
40 date: true,
41 views: true,
42 by: false,
43 avatar: false,
44 privacyLabel: false,
45 privacyText: false,
46 state: false,
47 blacklistInfo: false
48 }
5c20a455 49
734a5ceb
C
50 private accountSub: Subscription
51
d3e91a5f 52 constructor (
d3e91a5f 53 private accountService: AccountService,
c8487f3f 54 private videoChannelService: VideoChannelService,
6eb62c33 55 private videoService: VideoService,
900f7820 56 private markdown: MarkdownService,
5c20a455 57 private userService: UserService
d3e91a5f
C
58 ) { }
59
60 ngOnInit () {
61 // Parent get the account for us
734a5ceb 62 this.accountSub = this.accountService.accountLoaded
c8487f3f
C
63 .subscribe(account => {
64 this.account = account
65
66 this.loadMoreChannels()
67 })
5c20a455
C
68
69 this.userService.getAnonymousOrLoggedUser()
0aa52e17
C
70 .subscribe(user => {
71 this.userMiniature = user
72
73 this.nsfwPolicy = user.nsfwPolicy
74 })
d3e91a5f 75 }
734a5ceb
C
76
77 ngOnDestroy () {
78 if (this.accountSub) this.accountSub.unsubscribe()
79 }
c8487f3f
C
80
81 loadMoreChannels () {
82 this.videoChannelService.listAccountVideoChannels(this.account, this.channelPagination)
83 .pipe(
84 tap(res => this.channelPagination.totalItems = res.total),
85 switchMap(res => from(res.data)),
86 concatMap(videoChannel => {
0aa52e17
C
87 const options = {
88 videoChannel,
89 videoPagination: this.videosPagination,
90 sort: this.videosSort,
91 nsfwPolicy: this.nsfwPolicy
92 }
93
94 return this.videoService.getVideoChannelVideos(options)
900f7820 95 .pipe(map(data => ({ videoChannel, videos: data.data, total: data.total })))
c8487f3f
C
96 })
97 )
900f7820
C
98 .subscribe(async ({ videoChannel, videos, total }) => {
99 this.channelsDescriptionHTML[videoChannel.id] = await this.markdown.textMarkdownToHTML(videoChannel.description)
100
c8487f3f
C
101 this.videoChannels.push(videoChannel)
102
900f7820 103 this.videos[videoChannel.id] = { videos, total }
ad453580
C
104
105 this.onChannelDataSubject.next([ videoChannel ])
c8487f3f
C
106 })
107 }
108
109 getVideosOf (videoChannel: VideoChannel) {
900f7820
C
110 const obj = this.videos[ videoChannel.id ]
111 if (!obj) return []
112
113 return obj.videos
114 }
115
116 getTotalVideosOf (videoChannel: VideoChannel) {
117 const obj = this.videos[ videoChannel.id ]
118 if (!obj) return undefined
119
120 return obj.total
121 }
6eb62c33 122
900f7820
C
123 getChannelDescription (videoChannel: VideoChannel) {
124 return this.channelsDescriptionHTML[videoChannel.id]
c8487f3f
C
125 }
126
127 onNearOfBottom () {
128 if (!hasMoreItems(this.channelPagination)) return
129
130 this.channelPagination.currentPage += 1
131
132 this.loadMoreChannels()
133 }
dc890263
C
134
135 getVideoChannelLink (videoChannel: VideoChannel) {
136 return [ '/video-channels', videoChannel.nameWithHost ]
137 }
d3e91a5f 138}