1 import { AuthUser } from '@app/core'
2 import { User } from '@app/core/users/user.model'
3 import { durationToString, prepareIcu, 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 private static readonly viewsICU = prepareIcu($localize`{views, plural, =0 {No view} =1 {1 view} other {{views} views}}`)
23 private static readonly viewersICU = prepareIcu($localize`{viewers, plural, =0 {No viewers} =1 {1 viewer} other {{viewers} viewers}}`)
25 byVideoChannel: string
31 originallyPublishedAt: Date | string
32 category: VideoConstant<number>
33 licence: VideoConstant<number>
34 language: VideoConstant<string>
35 privacy: VideoConstant<VideoPrivacy>
70 originInstanceUrl: string
71 originInstanceHost: string
73 waitTranscoding?: boolean
74 state?: VideoConstant<VideoState>
75 scheduledUpdate?: VideoScheduleUpdate
78 blacklistedReason?: string
80 blockedOwner?: boolean
81 blockedServer?: boolean
90 // TODO: remove, deprecated in 4.2
103 // TODO: remove, deprecated in 4.2
106 avatars: ActorImage[]
115 streamingPlaylists?: VideoStreamingPlaylist[]
118 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
119 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
122 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
123 return '/videos/update/' + video.uuid
126 constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) {
127 const absoluteAPIUrl = getAbsoluteAPIUrl()
129 this.createdAt = new Date(hash.createdAt.toString())
130 this.publishedAt = new Date(hash.publishedAt.toString())
131 this.category = hash.category
132 this.licence = hash.licence
133 this.language = hash.language
134 this.privacy = hash.privacy
135 this.waitTranscoding = hash.waitTranscoding
136 this.state = hash.state
137 this.description = hash.description
139 this.isLive = hash.isLive
141 this.duration = hash.duration
142 this.durationLabel = durationToString(hash.duration)
145 this.uuid = hash.uuid
146 this.shortUUID = hash.shortUUID
148 this.isLocal = hash.isLocal
149 this.name = hash.name
151 this.thumbnailPath = hash.thumbnailPath
152 this.thumbnailUrl = this.thumbnailPath
153 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
156 this.previewPath = hash.previewPath
157 this.previewUrl = this.previewPath
158 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
161 this.embedPath = hash.embedPath
162 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
166 this.views = hash.views
167 this.viewers = hash.viewers
168 this.likes = hash.likes
169 this.dislikes = hash.dislikes
171 this.nsfw = hash.nsfw
173 this.account = hash.account
174 this.channel = hash.channel
176 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
177 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
179 this.category.label = peertubeTranslate(this.category.label, translations)
180 this.licence.label = peertubeTranslate(this.licence.label, translations)
181 this.language.label = peertubeTranslate(this.language.label, translations)
182 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
184 this.scheduledUpdate = hash.scheduledUpdate
185 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
187 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
189 this.blacklisted = hash.blacklisted
190 this.blacklistedReason = hash.blacklistedReason
192 this.blockedOwner = hash.blockedOwner
193 this.blockedServer = hash.blockedServer
195 this.streamingPlaylists = hash.streamingPlaylists
196 this.files = hash.files
198 this.userHistory = hash.userHistory
200 this.originInstanceHost = this.account.host
201 this.originInstanceUrl = 'https://' + this.originInstanceHost
203 this.pluginData = hash.pluginData
206 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
207 // Video is not NSFW, skip
208 if (this.nsfw === false) return false
210 // Return user setting if logged in
211 if (user) return user.nsfwPolicy !== 'display'
213 // Return default instance config
214 return serverConfig.instance.defaultNSFWPolicy !== 'display'
217 isRemovableBy (user: AuthUser) {
218 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
221 isBlockableBy (user: AuthUser) {
222 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
225 isUnblockableBy (user: AuthUser) {
226 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
229 isUpdatableBy (user: AuthUser) {
230 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
233 isEditableBy (user: AuthUser, videoStudioEnabled: boolean) {
234 return videoStudioEnabled &&
235 this.state?.id === VideoState.PUBLISHED &&
236 this.isUpdatableBy(user)
239 canSeeStats (user: AuthUser) {
240 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.SEE_ALL_VIDEOS))
243 canRemoveFiles (user: AuthUser) {
244 return this.isLocal &&
245 user && user.hasRight(UserRight.MANAGE_VIDEO_FILES) &&
246 this.state.id !== VideoState.TO_TRANSCODE &&
251 canRunTranscoding (user: AuthUser) {
252 return this.isLocal &&
253 user && user.hasRight(UserRight.RUN_VIDEO_TRANSCODING) &&
254 this.state.id !== VideoState.TO_TRANSCODE
258 return this.streamingPlaylists?.some(p => p.type === VideoStreamingPlaylistType.HLS)
262 return this.files && this.files.length !== 0
265 isLiveInfoAvailableBy (user: AuthUser) {
266 return this.isLive &&
267 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
270 canBeDuplicatedBy (user: AuthUser) {
271 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
274 getExactNumberOfViews () {
276 return Video.viewersICU({ viewers: this.viewers }, $localize`${this.viewers} viewer(s)`)
279 return Video.viewsICU({ views: this.views }, $localize`{${this.views} view(s)}`)