]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video-channel/video-channel.model.ts
Fix channel edition page
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video-channel / video-channel.model.ts
1 import { getAbsoluteAPIUrl } from '@app/helpers'
2 import { Account as ServerAccount, ActorImage, VideoChannel as ServerVideoChannel, ViewsPerDate } from '@shared/models'
3 import { Actor } from '../account/actor.model'
4
5 export class VideoChannel extends Actor implements ServerVideoChannel {
6 displayName: string
7 description: string
8 support: string
9
10 isLocal: boolean
11
12 nameWithHost: string
13 nameWithHostForced: string
14
15 // TODO: remove, deprecated in 4.2
16 banner: never
17
18 banners: ActorImage[]
19
20 bannerUrl: string
21
22 updatedAt: Date | string
23
24 ownerAccount?: ServerAccount
25 ownerBy?: string
26
27 videosCount?: number
28
29 viewsPerDay?: ViewsPerDate[]
30
31 static GET_ACTOR_AVATAR_URL (actor: { avatars: { width: number, url?: string, path: string }[] }, size: number) {
32 return Actor.GET_ACTOR_AVATAR_URL(actor, size)
33 }
34
35 static GET_ACTOR_BANNER_URL (channel: ServerVideoChannel) {
36 if (!channel || channel.banners.length === 0) {
37 return ''
38 }
39
40 const banner = channel.banners[0]
41 if (!banner) return ''
42
43 if (banner.url) return banner.url
44 return getAbsoluteAPIUrl() + banner.path
45 }
46
47 static GET_DEFAULT_AVATAR_URL (size: number) {
48 if (size <= 48) {
49 return `${window.location.origin}/client/assets/images/default-avatar-video-channel-48x48.png`
50 }
51
52 return `${window.location.origin}/client/assets/images/default-avatar-video-channel.png`
53 }
54
55 constructor (hash: Partial<ServerVideoChannel>) {
56 super(hash)
57
58 this.displayName = hash.displayName
59 this.description = hash.description
60 this.support = hash.support
61
62 this.banners = hash.banners || []
63
64 this.isLocal = hash.isLocal
65
66 this.nameWithHost = Actor.CREATE_BY_STRING(this.name, this.host)
67 this.nameWithHostForced = Actor.CREATE_BY_STRING(this.name, this.host, true)
68
69 this.videosCount = hash.videosCount
70
71 if (hash.updatedAt) this.updatedAt = new Date(hash.updatedAt.toString())
72
73 if (hash.viewsPerDay) {
74 this.viewsPerDay = hash.viewsPerDay.map(v => ({ ...v, date: new Date(v.date) }))
75 }
76
77 if (hash.ownerAccount) {
78 this.ownerAccount = hash.ownerAccount
79 this.ownerBy = Actor.CREATE_BY_STRING(hash.ownerAccount.name, hash.ownerAccount.host)
80 }
81
82 this.updateComputedAttributes()
83 }
84
85 updateAvatar (newAvatars: ActorImage[]) {
86 this.avatars = newAvatars
87
88 this.updateComputedAttributes()
89 }
90
91 resetAvatar () {
92 this.updateAvatar([])
93 }
94
95 updateBanner (newBanners: ActorImage[]) {
96 this.banners = newBanners
97
98 this.updateComputedAttributes()
99 }
100
101 resetBanner () {
102 this.updateBanner([])
103 }
104
105 updateComputedAttributes () {
106 this.bannerUrl = VideoChannel.GET_ACTOR_BANNER_URL(this)
107 }
108 }