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>
68 originInstanceUrl: string
69 originInstanceHost: string
71 waitTranscoding?: boolean
72 state?: VideoConstant<VideoState>
73 scheduledUpdate?: VideoScheduleUpdate
76 blacklistedReason?: string
78 blockedOwner?: boolean
79 blockedServer?: boolean
88 // TODO: remove, deprecated in 4.2
101 // TODO: remove, deprecated in 4.2
104 avatars: ActorImage[]
113 streamingPlaylists?: VideoStreamingPlaylist[]
116 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
117 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
120 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
121 return '/videos/update/' + video.uuid
124 constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) {
125 const absoluteAPIUrl = getAbsoluteAPIUrl()
127 this.createdAt = new Date(hash.createdAt.toString())
128 this.publishedAt = new Date(hash.publishedAt.toString())
129 this.category = hash.category
130 this.licence = hash.licence
131 this.language = hash.language
132 this.privacy = hash.privacy
133 this.waitTranscoding = hash.waitTranscoding
134 this.state = hash.state
135 this.description = hash.description
137 this.isLive = hash.isLive
139 this.duration = hash.duration
140 this.durationLabel = durationToString(hash.duration)
143 this.uuid = hash.uuid
144 this.shortUUID = hash.shortUUID
146 this.isLocal = hash.isLocal
147 this.name = hash.name
149 this.thumbnailPath = hash.thumbnailPath
150 this.thumbnailUrl = this.thumbnailPath
151 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
154 this.previewPath = hash.previewPath
155 this.previewUrl = this.previewPath
156 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
159 this.embedPath = hash.embedPath
160 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
164 this.views = hash.views
165 this.viewers = hash.viewers
166 this.likes = hash.likes
167 this.dislikes = hash.dislikes
169 this.nsfw = hash.nsfw
171 this.account = hash.account
172 this.channel = hash.channel
174 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
175 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
177 this.category.label = peertubeTranslate(this.category.label, translations)
178 this.licence.label = peertubeTranslate(this.licence.label, translations)
179 this.language.label = peertubeTranslate(this.language.label, translations)
180 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
182 this.scheduledUpdate = hash.scheduledUpdate
183 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
185 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
187 this.blacklisted = hash.blacklisted
188 this.blacklistedReason = hash.blacklistedReason
190 this.blockedOwner = hash.blockedOwner
191 this.blockedServer = hash.blockedServer
193 this.streamingPlaylists = hash.streamingPlaylists
194 this.files = hash.files
196 this.userHistory = hash.userHistory
198 this.originInstanceHost = this.account.host
199 this.originInstanceUrl = 'https://' + this.originInstanceHost
201 this.pluginData = hash.pluginData
204 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
205 // Video is not NSFW, skip
206 if (this.nsfw === false) return false
208 // Return user setting if logged in
209 if (user) return user.nsfwPolicy !== 'display'
211 // Return default instance config
212 return serverConfig.instance.defaultNSFWPolicy !== 'display'
215 isRemovableBy (user: AuthUser) {
216 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
219 isBlockableBy (user: AuthUser) {
220 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
223 isUnblockableBy (user: AuthUser) {
224 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
227 isUpdatableBy (user: AuthUser) {
228 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
231 isEditableBy (user: AuthUser, videoStudioEnabled: boolean) {
232 return videoStudioEnabled &&
233 this.state?.id === VideoState.PUBLISHED &&
234 this.isUpdatableBy(user)
237 canRemoveFiles (user: AuthUser) {
238 return this.isLocal &&
239 user.hasRight(UserRight.MANAGE_VIDEO_FILES) &&
240 this.state.id !== VideoState.TO_TRANSCODE &&
245 canRunTranscoding (user: AuthUser) {
246 return this.isLocal &&
247 user.hasRight(UserRight.RUN_VIDEO_TRANSCODING) &&
248 this.state.id !== VideoState.TO_TRANSCODE
252 return this.streamingPlaylists?.some(p => p.type === VideoStreamingPlaylistType.HLS)
256 return this.files && this.files.length !== 0
259 isLiveInfoAvailableBy (user: AuthUser) {
260 return this.isLive &&
261 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
264 canBeDuplicatedBy (user: AuthUser) {
265 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
268 getExactNumberOfViews () {
269 if (this.views < 1000) return ''
272 return $localize`${this.views} viewers`
275 return $localize`${this.views} views`