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'
11 Video as VideoServerModel,
17 VideoStreamingPlaylist,
18 VideoStreamingPlaylistType
19 } from '@shared/models'
21 export class Video implements VideoServerModel {
22 byVideoChannel: string
28 originallyPublishedAt: Date | string
29 category: VideoConstant<number>
30 licence: VideoConstant<number>
31 language: VideoConstant<string>
32 privacy: VideoConstant<VideoPrivacy>
67 originInstanceUrl: string
68 originInstanceHost: string
70 waitTranscoding?: boolean
71 state?: VideoConstant<VideoState>
72 scheduledUpdate?: VideoScheduleUpdate
75 blacklistedReason?: string
77 blockedOwner?: boolean
78 blockedServer?: boolean
87 // TODO: remove, deprecated in 4.2
100 // TODO: remove, deprecated in 4.2
103 avatars: ActorImage[]
112 streamingPlaylists?: VideoStreamingPlaylist[]
115 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
116 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
119 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
120 return '/videos/update/' + video.uuid
123 constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) {
124 const absoluteAPIUrl = getAbsoluteAPIUrl()
126 this.createdAt = new Date(hash.createdAt.toString())
127 this.publishedAt = new Date(hash.publishedAt.toString())
128 this.category = hash.category
129 this.licence = hash.licence
130 this.language = hash.language
131 this.privacy = hash.privacy
132 this.waitTranscoding = hash.waitTranscoding
133 this.state = hash.state
134 this.description = hash.description
136 this.isLive = hash.isLive
138 this.duration = hash.duration
139 this.durationLabel = durationToString(hash.duration)
142 this.uuid = hash.uuid
143 this.shortUUID = hash.shortUUID
145 this.isLocal = hash.isLocal
146 this.name = hash.name
148 this.thumbnailPath = hash.thumbnailPath
149 this.thumbnailUrl = this.thumbnailPath
150 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
153 this.previewPath = hash.previewPath
154 this.previewUrl = this.previewPath
155 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
158 this.embedPath = hash.embedPath
159 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
163 this.views = hash.views
164 this.viewers = hash.viewers
165 this.likes = hash.likes
166 this.dislikes = hash.dislikes
168 this.nsfw = hash.nsfw
170 this.account = hash.account
171 this.channel = hash.channel
173 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
174 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
176 this.category.label = peertubeTranslate(this.category.label, translations)
177 this.licence.label = peertubeTranslate(this.licence.label, translations)
178 this.language.label = peertubeTranslate(this.language.label, translations)
179 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
181 this.scheduledUpdate = hash.scheduledUpdate
182 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
184 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
186 this.blacklisted = hash.blacklisted
187 this.blacklistedReason = hash.blacklistedReason
189 this.blockedOwner = hash.blockedOwner
190 this.blockedServer = hash.blockedServer
192 this.streamingPlaylists = hash.streamingPlaylists
193 this.files = hash.files
195 this.userHistory = hash.userHistory
197 this.originInstanceHost = this.account.host
198 this.originInstanceUrl = 'https://' + this.originInstanceHost
200 this.pluginData = hash.pluginData
203 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
204 // Video is not NSFW, skip
205 if (this.nsfw === false) return false
207 // Return user setting if logged in
208 if (user) return user.nsfwPolicy !== 'display'
210 // Return default instance config
211 return serverConfig.instance.defaultNSFWPolicy !== 'display'
214 isRemovableBy (user: AuthUser) {
215 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
218 isBlockableBy (user: AuthUser) {
219 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
222 isUnblockableBy (user: AuthUser) {
223 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
226 isUpdatableBy (user: AuthUser) {
227 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
230 isEditableBy (user: AuthUser, videoStudioEnabled: boolean) {
231 return videoStudioEnabled &&
232 this.state?.id === VideoState.PUBLISHED &&
233 this.isUpdatableBy(user)
236 canSeeStats (user: AuthUser) {
237 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.SEE_ALL_VIDEOS))
240 canRemoveFiles (user: AuthUser) {
241 return this.isLocal &&
242 user && user.hasRight(UserRight.MANAGE_VIDEO_FILES) &&
243 this.state.id !== VideoState.TO_TRANSCODE &&
248 canRunTranscoding (user: AuthUser) {
249 return this.isLocal &&
250 user && user.hasRight(UserRight.RUN_VIDEO_TRANSCODING) &&
251 this.state.id !== VideoState.TO_TRANSCODE
255 return this.streamingPlaylists?.some(p => p.type === VideoStreamingPlaylistType.HLS)
259 return this.files && this.files.length !== 0
262 isLiveInfoAvailableBy (user: AuthUser) {
263 return this.isLive &&
264 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
267 canBeDuplicatedBy (user: AuthUser) {
268 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
271 getExactNumberOfViews () {
272 if (this.views < 1000) return ''
275 return $localize`${this.views} viewers`
278 return $localize`${this.views} views`