PeerTube/client/src/app/shared/shared-main/video-channel/video-channel.model.ts

112 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-04-07 17:01:29 +02:00
import { getAbsoluteAPIUrl } from '@app/helpers'
import { Account as ServerAccount, ActorImage, VideoChannel as ServerVideoChannel, ViewsPerDate } from '@peertube/peertube-models'
2020-06-23 14:10:17 +02:00
import { Actor } from '../account/actor.model'
2024-03-27 14:00:40 +01:00
import { maxBy } from '@peertube/peertube-core-utils'
2018-04-25 15:43:19 +02:00
export class VideoChannel extends Actor implements ServerVideoChannel {
displayName: string
description: string
support: string
2021-04-07 17:01:29 +02:00
2018-04-25 15:43:19 +02:00
isLocal: boolean
2021-04-07 17:01:29 +02:00
2018-08-17 15:45:42 +02:00
nameWithHost: string
nameWithHostForced: string
2020-06-16 14:13:01 +02:00
banners: ActorImage[]
2021-04-07 17:01:29 +02:00
bannerUrl: string
2021-05-10 09:31:33 +02:00
updatedAt: Date | string
ownerAccount?: ServerAccount
2018-05-23 11:38:00 +02:00
ownerBy?: string
2020-06-16 14:13:01 +02:00
videosCount?: number
viewsPerDay?: ViewsPerDate[]
totalViews?: number
2018-04-25 15:43:19 +02:00
static GET_ACTOR_AVATAR_URL (actor: { avatars: { width: number, url?: string, path: string }[] }, size: number) {
return Actor.GET_ACTOR_AVATAR_URL(actor, size)
}
2021-04-07 17:01:29 +02:00
static GET_ACTOR_BANNER_URL (channel: ServerVideoChannel) {
2022-03-21 09:11:11 +01:00
if (!channel || channel.banners.length === 0) {
return ''
}
2021-04-07 17:01:29 +02:00
2024-03-27 14:00:40 +01:00
const banner = maxBy(channel.banners, 'width')
if (!banner) return ''
2021-04-07 17:01:29 +02:00
if (banner.url) return banner.url
return getAbsoluteAPIUrl() + banner.path
2021-04-07 17:01:29 +02:00
}
static GET_DEFAULT_AVATAR_URL (size: number) {
if (size <= 48) {
return `${window.location.origin}/client/assets/images/default-avatar-video-channel-48x48.png`
}
return `${window.location.origin}/client/assets/images/default-avatar-video-channel.png`
}
constructor (hash: Partial<ServerVideoChannel>) {
2018-04-25 15:43:19 +02:00
super(hash)
this.displayName = hash.displayName
this.description = hash.description
this.support = hash.support
2021-04-07 17:01:29 +02:00
2022-03-21 09:11:11 +01:00
this.banners = hash.banners || []
2021-04-07 17:01:29 +02:00
2018-04-25 15:43:19 +02:00
this.isLocal = hash.isLocal
2021-04-07 17:01:29 +02:00
2018-08-17 15:45:42 +02:00
this.nameWithHost = Actor.CREATE_BY_STRING(this.name, this.host)
this.nameWithHostForced = Actor.CREATE_BY_STRING(this.name, this.host, true)
2018-05-23 11:38:00 +02:00
2020-06-16 14:13:01 +02:00
this.videosCount = hash.videosCount
2021-05-10 09:31:33 +02:00
if (hash.updatedAt) this.updatedAt = new Date(hash.updatedAt.toString())
2020-03-23 10:14:05 +01:00
if (hash.viewsPerDay) {
this.viewsPerDay = hash.viewsPerDay.map(v => ({ ...v, date: new Date(v.date) }))
2020-03-23 10:14:05 +01:00
}
if (hash.totalViews !== null && hash.totalViews !== undefined) {
this.totalViews = hash.totalViews
}
2018-05-23 11:38:00 +02:00
if (hash.ownerAccount) {
this.ownerAccount = hash.ownerAccount
this.ownerBy = Actor.CREATE_BY_STRING(hash.ownerAccount.name, hash.ownerAccount.host)
}
2021-04-07 17:01:29 +02:00
this.updateComputedAttributes()
2018-04-25 15:43:19 +02:00
}
updateAvatar (newAvatars: ActorImage[]) {
this.avatars = newAvatars
this.updateComputedAttributes()
}
resetAvatar () {
this.updateAvatar([])
2021-04-07 17:01:29 +02:00
}
updateBanner (newBanners: ActorImage[]) {
this.banners = newBanners
2021-04-07 17:01:29 +02:00
this.updateComputedAttributes()
}
resetBanner () {
this.updateBanner([])
}
updateComputedAttributes () {
2021-04-07 17:01:29 +02:00
this.bannerUrl = VideoChannel.GET_ACTOR_BANNER_URL(this)
}
2018-04-25 15:43:19 +02:00
}