]>
Commit | Line | Data |
---|---|---|
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 | totalViews?: number | |
31 | ||
32 | static GET_ACTOR_AVATAR_URL (actor: { avatars: { width: number, url?: string, path: string }[] }, size: number) { | |
33 | return Actor.GET_ACTOR_AVATAR_URL(actor, size) | |
34 | } | |
35 | ||
36 | static GET_ACTOR_BANNER_URL (channel: ServerVideoChannel) { | |
37 | if (!channel || channel.banners.length === 0) { | |
38 | return '' | |
39 | } | |
40 | ||
41 | const banner = channel.banners[0] | |
42 | if (!banner) return '' | |
43 | ||
44 | if (banner.url) return banner.url | |
45 | return getAbsoluteAPIUrl() + banner.path | |
46 | } | |
47 | ||
48 | static GET_DEFAULT_AVATAR_URL (size: number) { | |
49 | if (size <= 48) { | |
50 | return `${window.location.origin}/client/assets/images/default-avatar-video-channel-48x48.png` | |
51 | } | |
52 | ||
53 | return `${window.location.origin}/client/assets/images/default-avatar-video-channel.png` | |
54 | } | |
55 | ||
56 | constructor (hash: Partial<ServerVideoChannel>) { | |
57 | super(hash) | |
58 | ||
59 | this.displayName = hash.displayName | |
60 | this.description = hash.description | |
61 | this.support = hash.support | |
62 | ||
63 | this.banners = hash.banners || [] | |
64 | ||
65 | this.isLocal = hash.isLocal | |
66 | ||
67 | this.nameWithHost = Actor.CREATE_BY_STRING(this.name, this.host) | |
68 | this.nameWithHostForced = Actor.CREATE_BY_STRING(this.name, this.host, true) | |
69 | ||
70 | this.videosCount = hash.videosCount | |
71 | ||
72 | if (hash.updatedAt) this.updatedAt = new Date(hash.updatedAt.toString()) | |
73 | ||
74 | if (hash.viewsPerDay) { | |
75 | this.viewsPerDay = hash.viewsPerDay.map(v => ({ ...v, date: new Date(v.date) })) | |
76 | } | |
77 | ||
78 | if (hash.totalViews !== null && hash.totalViews !== undefined) { | |
79 | this.totalViews = hash.totalViews | |
80 | } | |
81 | ||
82 | if (hash.ownerAccount) { | |
83 | this.ownerAccount = hash.ownerAccount | |
84 | this.ownerBy = Actor.CREATE_BY_STRING(hash.ownerAccount.name, hash.ownerAccount.host) | |
85 | } | |
86 | ||
87 | this.updateComputedAttributes() | |
88 | } | |
89 | ||
90 | updateAvatar (newAvatars: ActorImage[]) { | |
91 | this.avatars = newAvatars | |
92 | ||
93 | this.updateComputedAttributes() | |
94 | } | |
95 | ||
96 | resetAvatar () { | |
97 | this.updateAvatar([]) | |
98 | } | |
99 | ||
100 | updateBanner (newBanners: ActorImage[]) { | |
101 | this.banners = newBanners | |
102 | ||
103 | this.updateComputedAttributes() | |
104 | } | |
105 | ||
106 | resetBanner () { | |
107 | this.updateBanner([]) | |
108 | } | |
109 | ||
110 | updateComputedAttributes () { | |
111 | this.bannerUrl = VideoChannel.GET_ACTOR_BANNER_URL(this) | |
112 | } | |
113 | } |