]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video-channel/video-channel.model.ts
e22b0cfd0360c4f37485dfa7ea4e0e3ab866bd0d
[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) return ''
37
38 const banner = channel.banners[0]
39 if (!banner) return ''
40
41 if (banner.url) return banner.url
42 return getAbsoluteAPIUrl() + banner.path
43 }
44
45 static GET_DEFAULT_AVATAR_URL (size: number) {
46 if (size <= 48) {
47 return `${window.location.origin}/client/assets/images/default-avatar-video-channel-48x48.png`
48 }
49
50 return `${window.location.origin}/client/assets/images/default-avatar-video-channel.png`
51 }
52
53 constructor (hash: Partial<ServerVideoChannel>) {
54 super(hash)
55
56 this.displayName = hash.displayName
57 this.description = hash.description
58 this.support = hash.support
59
60 this.banners = hash.banners
61
62 this.isLocal = hash.isLocal
63
64 this.nameWithHost = Actor.CREATE_BY_STRING(this.name, this.host)
65 this.nameWithHostForced = Actor.CREATE_BY_STRING(this.name, this.host, true)
66
67 this.videosCount = hash.videosCount
68
69 if (hash.updatedAt) this.updatedAt = new Date(hash.updatedAt.toString())
70
71 if (hash.viewsPerDay) {
72 this.viewsPerDay = hash.viewsPerDay.map(v => ({ ...v, date: new Date(v.date) }))
73 }
74
75 if (hash.ownerAccount) {
76 this.ownerAccount = hash.ownerAccount
77 this.ownerBy = Actor.CREATE_BY_STRING(hash.ownerAccount.name, hash.ownerAccount.host)
78 }
79
80 this.updateComputedAttributes()
81 }
82
83 updateAvatar (newAvatars: ActorImage[]) {
84 this.avatars = newAvatars
85
86 this.updateComputedAttributes()
87 }
88
89 resetAvatar () {
90 this.updateAvatar([])
91 }
92
93 updateBanner (newBanners: ActorImage[]) {
94 this.banners = newBanners
95
96 this.updateComputedAttributes()
97 }
98
99 resetBanner () {
100 this.updateBanner([])
101 }
102
103 updateComputedAttributes () {
104 this.bannerUrl = VideoChannel.GET_ACTOR_BANNER_URL(this)
105 }
106 }