]>
Commit | Line | Data |
---|---|---|
1 | import { AuthUser } from '@app/core' | |
2 | import { User } from '@app/core/users/user.model' | |
3 | import { durationToString, getAbsoluteAPIUrl, getAbsoluteEmbedUrl } from '@app/helpers' | |
4 | import { Actor } from '@app/shared/shared-main/account/actor.model' | |
5 | import { buildVideoWatchPath } from '@shared/core-utils' | |
6 | import { peertubeTranslate } from '@shared/core-utils/i18n' | |
7 | import { | |
8 | ActorImage, | |
9 | HTMLServerConfig, | |
10 | UserRight, | |
11 | Video as VideoServerModel, | |
12 | VideoConstant, | |
13 | VideoPrivacy, | |
14 | VideoScheduleUpdate, | |
15 | VideoState | |
16 | } from '@shared/models' | |
17 | ||
18 | export class Video implements VideoServerModel { | |
19 | byVideoChannel: string | |
20 | byAccount: string | |
21 | ||
22 | createdAt: Date | |
23 | updatedAt: Date | |
24 | publishedAt: Date | |
25 | originallyPublishedAt: Date | string | |
26 | category: VideoConstant<number> | |
27 | licence: VideoConstant<number> | |
28 | language: VideoConstant<string> | |
29 | privacy: VideoConstant<VideoPrivacy> | |
30 | ||
31 | description: string | |
32 | ||
33 | duration: number | |
34 | durationLabel: string | |
35 | ||
36 | id: number | |
37 | uuid: string | |
38 | shortUUID: string | |
39 | ||
40 | isLocal: boolean | |
41 | ||
42 | name: string | |
43 | serverHost: string | |
44 | thumbnailPath: string | |
45 | thumbnailUrl: string | |
46 | ||
47 | isLive: boolean | |
48 | ||
49 | previewPath: string | |
50 | previewUrl: string | |
51 | ||
52 | embedPath: string | |
53 | embedUrl: string | |
54 | ||
55 | url: string | |
56 | ||
57 | views: number | |
58 | likes: number | |
59 | dislikes: number | |
60 | nsfw: boolean | |
61 | ||
62 | originInstanceUrl: string | |
63 | originInstanceHost: string | |
64 | ||
65 | waitTranscoding?: boolean | |
66 | state?: VideoConstant<VideoState> | |
67 | scheduledUpdate?: VideoScheduleUpdate | |
68 | blacklisted?: boolean | |
69 | blockedReason?: string | |
70 | ||
71 | account: { | |
72 | id: number | |
73 | name: string | |
74 | displayName: string | |
75 | url: string | |
76 | host: string | |
77 | avatar?: ActorImage | |
78 | } | |
79 | ||
80 | channel: { | |
81 | id: number | |
82 | name: string | |
83 | displayName: string | |
84 | url: string | |
85 | host: string | |
86 | avatar?: ActorImage | |
87 | } | |
88 | ||
89 | userHistory?: { | |
90 | currentTime: number | |
91 | } | |
92 | ||
93 | pluginData?: any | |
94 | ||
95 | static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) { | |
96 | return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid }) | |
97 | } | |
98 | ||
99 | static buildUpdateUrl (video: Pick<Video, 'uuid'>) { | |
100 | return '/videos/update/' + video.uuid | |
101 | } | |
102 | ||
103 | constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) { | |
104 | const absoluteAPIUrl = getAbsoluteAPIUrl() | |
105 | ||
106 | this.createdAt = new Date(hash.createdAt.toString()) | |
107 | this.publishedAt = new Date(hash.publishedAt.toString()) | |
108 | this.category = hash.category | |
109 | this.licence = hash.licence | |
110 | this.language = hash.language | |
111 | this.privacy = hash.privacy | |
112 | this.waitTranscoding = hash.waitTranscoding | |
113 | this.state = hash.state | |
114 | this.description = hash.description | |
115 | ||
116 | this.isLive = hash.isLive | |
117 | ||
118 | this.duration = hash.duration | |
119 | this.durationLabel = durationToString(hash.duration) | |
120 | ||
121 | this.id = hash.id | |
122 | this.uuid = hash.uuid | |
123 | this.shortUUID = hash.shortUUID | |
124 | ||
125 | this.isLocal = hash.isLocal | |
126 | this.name = hash.name | |
127 | ||
128 | this.thumbnailPath = hash.thumbnailPath | |
129 | this.thumbnailUrl = this.thumbnailPath | |
130 | ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath) | |
131 | : null | |
132 | ||
133 | this.previewPath = hash.previewPath | |
134 | this.previewUrl = this.previewPath | |
135 | ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath) | |
136 | : null | |
137 | ||
138 | this.embedPath = hash.embedPath | |
139 | this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath) | |
140 | ||
141 | this.url = hash.url | |
142 | ||
143 | this.views = hash.views | |
144 | this.likes = hash.likes | |
145 | this.dislikes = hash.dislikes | |
146 | ||
147 | this.nsfw = hash.nsfw | |
148 | ||
149 | this.account = hash.account | |
150 | this.channel = hash.channel | |
151 | ||
152 | this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host) | |
153 | this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host) | |
154 | ||
155 | this.category.label = peertubeTranslate(this.category.label, translations) | |
156 | this.licence.label = peertubeTranslate(this.licence.label, translations) | |
157 | this.language.label = peertubeTranslate(this.language.label, translations) | |
158 | this.privacy.label = peertubeTranslate(this.privacy.label, translations) | |
159 | ||
160 | this.scheduledUpdate = hash.scheduledUpdate | |
161 | this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null | |
162 | ||
163 | if (this.state) this.state.label = peertubeTranslate(this.state.label, translations) | |
164 | ||
165 | this.blacklisted = hash.blacklisted | |
166 | this.blockedReason = hash.blacklistedReason | |
167 | ||
168 | this.userHistory = hash.userHistory | |
169 | ||
170 | this.originInstanceHost = this.account.host | |
171 | this.originInstanceUrl = 'https://' + this.originInstanceHost | |
172 | ||
173 | this.pluginData = hash.pluginData | |
174 | } | |
175 | ||
176 | isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) { | |
177 | // Video is not NSFW, skip | |
178 | if (this.nsfw === false) return false | |
179 | ||
180 | // Return user setting if logged in | |
181 | if (user) return user.nsfwPolicy !== 'display' | |
182 | ||
183 | // Return default instance config | |
184 | return serverConfig.instance.defaultNSFWPolicy !== 'display' | |
185 | } | |
186 | ||
187 | isRemovableBy (user: AuthUser) { | |
188 | return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO)) | |
189 | } | |
190 | ||
191 | isBlockableBy (user: AuthUser) { | |
192 | return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true | |
193 | } | |
194 | ||
195 | isUnblockableBy (user: AuthUser) { | |
196 | return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true | |
197 | } | |
198 | ||
199 | isUpdatableBy (user: AuthUser) { | |
200 | return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO)) | |
201 | } | |
202 | ||
203 | isLiveInfoAvailableBy (user: AuthUser) { | |
204 | return this.isLive && | |
205 | user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE)) | |
206 | } | |
207 | ||
208 | canBeDuplicatedBy (user: AuthUser) { | |
209 | return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES) | |
210 | } | |
211 | ||
212 | getExactNumberOfViews () { | |
213 | if (this.views < 1000) return '' | |
214 | ||
215 | if (this.isLive) { | |
216 | return $localize`${this.views} viewers` | |
217 | } | |
218 | ||
219 | return $localize`${this.views} views` | |
220 | } | |
221 | } |