PeerTube/client/src/app/+accounts/account-video-channels/account-video-channels.component.ts
Chocobozzz 3a4992633e
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:

 * Server can be faster at startup because imports() are async and we can
   easily lazy import big modules
 * Angular doesn't seem to support ES import (with .js extension), so we
   had to correctly organize peertube into a monorepo:
    * Use yarn workspace feature
    * Use typescript reference projects for dependencies
    * Shared projects have been moved into "packages", each one is now a
      node module (with a dedicated package.json/tsconfig.json)
    * server/tools have been moved into apps/ and is now a dedicated app
      bundled and published on NPM so users don't have to build peertube
      cli tools manually
    * server/tests have been moved into packages/ so we don't compile
      them every time we want to run the server
 * Use isolatedModule option:
   * Had to move from const enum to const
     (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
   * Had to explictely specify "type" imports when used in decorators
 * Prefer tsx (that uses esbuild under the hood) instead of ts-node to
   load typescript files (tests with mocha or scripts):
     * To reduce test complexity as esbuild doesn't support decorator
       metadata, we only test server files that do not import server
       models
     * We still build tests files into js files for a faster CI
 * Remove unmaintained peertube CLI import script
 * Removed some barrels to speed up execution (less imports)
2023-08-11 15:02:33 +02:00

154 lines
4.2 KiB
TypeScript

import { from, Subject, Subscription } from 'rxjs'
import { concatMap, map, switchMap, tap } from 'rxjs/operators'
import { Component, OnDestroy, OnInit } from '@angular/core'
import { ComponentPagination, hasMoreItems, MarkdownService, User, UserService } from '@app/core'
import { SimpleMemoize } from '@app/helpers'
import { Account, AccountService, Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'
import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature'
import { NSFWPolicyType, VideoSortField } from '@peertube/peertube-models'
@Component({
selector: 'my-account-video-channels',
templateUrl: './account-video-channels.component.html',
styleUrls: [ './account-video-channels.component.scss' ]
})
export class AccountVideoChannelsComponent implements OnInit, OnDestroy {
account: Account
videoChannels: VideoChannel[] = []
videos: { [id: number]: { total: number, videos: Video[] } } = {}
channelsDescriptionHTML: { [ id: number ]: string } = {}
channelPagination: ComponentPagination = {
currentPage: 1,
itemsPerPage: 2,
totalItems: null
}
videosPagination: ComponentPagination = {
currentPage: 1,
itemsPerPage: 5,
totalItems: null
}
videosSort: VideoSortField = '-publishedAt'
onChannelDataSubject = new Subject<any>()
userMiniature: User
nsfwPolicy: NSFWPolicyType
miniatureDisplayOptions: MiniatureDisplayOptions = {
date: true,
views: true,
by: false,
avatar: false,
privacyLabel: false,
privacyText: false,
state: false,
blacklistInfo: false
}
private accountSub: Subscription
constructor (
private accountService: AccountService,
private videoChannelService: VideoChannelService,
private videoService: VideoService,
private markdown: MarkdownService,
private userService: UserService
) { }
ngOnInit () {
// Parent get the account for us
this.accountSub = this.accountService.accountLoaded
.subscribe(account => {
this.account = account
this.videoChannels = []
this.loadMoreChannels()
})
this.userService.getAnonymousOrLoggedUser()
.subscribe(user => {
this.userMiniature = user
this.nsfwPolicy = user.nsfwPolicy
})
}
ngOnDestroy () {
if (this.accountSub) this.accountSub.unsubscribe()
}
loadMoreChannels () {
const options = {
account: this.account,
componentPagination: this.channelPagination,
sort: '-updatedAt'
}
this.videoChannelService.listAccountVideoChannels(options)
.pipe(
tap(res => {
this.channelPagination.totalItems = res.total
}),
switchMap(res => from(res.data)),
concatMap(videoChannel => {
const options = {
videoChannel,
videoPagination: this.videosPagination,
sort: this.videosSort,
nsfw: this.videoService.nsfwPolicyToParam(this.nsfwPolicy)
}
return this.videoService.getVideoChannelVideos(options)
.pipe(map(data => ({ videoChannel, videos: data.data, total: data.total })))
})
)
.subscribe(async ({ videoChannel, videos, total }) => {
this.channelsDescriptionHTML[videoChannel.id] = await this.markdown.textMarkdownToHTML({
markdown: videoChannel.description,
withEmoji: true,
withHtml: true
})
this.videoChannels.push(videoChannel)
this.videos[videoChannel.id] = { videos, total }
this.onChannelDataSubject.next([ videoChannel ])
})
}
getVideosOf (videoChannel: VideoChannel) {
const obj = this.videos[videoChannel.id]
if (!obj) return []
return obj.videos
}
getTotalVideosOf (videoChannel: VideoChannel) {
const obj = this.videos[videoChannel.id]
if (!obj) return undefined
return obj.total
}
getChannelDescription (videoChannel: VideoChannel) {
return this.channelsDescriptionHTML[videoChannel.id]
}
onNearOfBottom () {
if (!hasMoreItems(this.channelPagination)) return
this.channelPagination.currentPage += 1
this.loadMoreChannels()
}
@SimpleMemoize()
getVideoChannelLink (videoChannel: VideoChannel) {
return [ '/c', videoChannel.nameWithHost ]
}
}