1 import { AuthUser } from '@app/core'
2 import { User } from '@app/core/users/user.model'
3 import { durationToString, getAbsoluteAPIUrl, getAbsoluteEmbedUrl, prepareIcu } from '@app/helpers'
4 import { Actor } from '@app/shared/shared-main/account/actor.model'
5 import { buildVideoWatchPath, getAllFiles } 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>
37 truncatedDescription: string
71 originInstanceUrl: string
72 originInstanceHost: string
74 waitTranscoding?: boolean
75 state?: VideoConstant<VideoState>
76 scheduledUpdate?: VideoScheduleUpdate
79 blacklistedReason?: string
81 blockedOwner?: boolean
82 blockedServer?: boolean
91 // TODO: remove, deprecated in 4.2
104 // TODO: remove, deprecated in 4.2
107 avatars: ActorImage[]
116 streamingPlaylists?: VideoStreamingPlaylist[]
119 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
120 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
123 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
124 return '/videos/update/' + video.uuid
127 constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) {
128 const absoluteAPIUrl = getAbsoluteAPIUrl()
130 this.createdAt = new Date(hash.createdAt.toString())
131 this.publishedAt = new Date(hash.publishedAt.toString())
132 this.category = hash.category
133 this.licence = hash.licence
134 this.language = hash.language
135 this.privacy = hash.privacy
136 this.waitTranscoding = hash.waitTranscoding
137 this.state = hash.state
139 this.truncatedDescription = hash.truncatedDescription
140 this.description = hash.description
142 this.isLive = hash.isLive
144 this.duration = hash.duration
145 this.durationLabel = durationToString(hash.duration)
148 this.uuid = hash.uuid
149 this.shortUUID = hash.shortUUID
151 this.isLocal = hash.isLocal
152 this.name = hash.name
154 this.thumbnailPath = hash.thumbnailPath
155 this.thumbnailUrl = this.thumbnailPath
156 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
159 this.previewPath = hash.previewPath
160 this.previewUrl = this.previewPath
161 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
164 this.embedPath = hash.embedPath
165 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
169 this.views = hash.views
170 this.viewers = hash.viewers
171 this.likes = hash.likes
172 this.dislikes = hash.dislikes
174 this.nsfw = hash.nsfw
176 this.account = hash.account
177 this.channel = hash.channel
179 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
180 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
182 this.category.label = peertubeTranslate(this.category.label, translations)
183 this.licence.label = peertubeTranslate(this.licence.label, translations)
184 this.language.label = peertubeTranslate(this.language.label, translations)
185 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
187 this.scheduledUpdate = hash.scheduledUpdate
188 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
190 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
192 this.blacklisted = hash.blacklisted
193 this.blacklistedReason = hash.blacklistedReason
195 this.blockedOwner = hash.blockedOwner
196 this.blockedServer = hash.blockedServer
198 this.streamingPlaylists = hash.streamingPlaylists
199 this.files = hash.files
201 this.userHistory = hash.userHistory
203 this.originInstanceHost = this.account.host
204 this.originInstanceUrl = 'https://' + this.originInstanceHost
206 this.pluginData = hash.pluginData
209 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
210 // Video is not NSFW, skip
211 if (this.nsfw === false) return false
213 // Return user setting if logged in
214 if (user) return user.nsfwPolicy !== 'display'
216 // Return default instance config
217 return serverConfig.instance.defaultNSFWPolicy !== 'display'
220 isRemovableBy (user: AuthUser) {
221 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
224 isBlockableBy (user: AuthUser) {
225 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
228 isUnblockableBy (user: AuthUser) {
229 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
232 isUpdatableBy (user: AuthUser) {
233 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
236 isEditableBy (user: AuthUser, videoStudioEnabled: boolean) {
237 return videoStudioEnabled &&
238 this.state?.id === VideoState.PUBLISHED &&
239 this.isUpdatableBy(user)
242 canSeeStats (user: AuthUser) {
243 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.SEE_ALL_VIDEOS))
246 canRemoveOneFile (user: AuthUser) {
247 return this.isLocal &&
248 user && user.hasRight(UserRight.MANAGE_VIDEO_FILES) &&
249 this.state.id !== VideoState.TO_TRANSCODE &&
250 getAllFiles(this).length > 1
253 canRemoveFiles (user: AuthUser) {
254 return this.isLocal &&
255 user && user.hasRight(UserRight.MANAGE_VIDEO_FILES) &&
256 this.state.id !== VideoState.TO_TRANSCODE &&
261 canRunTranscoding (user: AuthUser) {
262 return this.isLocal &&
263 user && user.hasRight(UserRight.RUN_VIDEO_TRANSCODING) &&
264 this.state.id !== VideoState.TO_TRANSCODE
268 return this.streamingPlaylists?.some(p => p.type === VideoStreamingPlaylistType.HLS)
272 return this.files && this.files.length !== 0
275 isLiveInfoAvailableBy (user: AuthUser) {
276 return this.isLive &&
277 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
280 canBeDuplicatedBy (user: AuthUser) {
281 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
284 getExactNumberOfViews () {
286 return Video.viewersICU({ viewers: this.viewers }, $localize`${this.viewers} viewer(s)`)
289 return Video.viewsICU({ views: this.views }, $localize`{${this.views} view(s)}`)