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