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
105 streamingPlaylists?: VideoStreamingPlaylist[]
108 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
109 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
112 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
113 return '/videos/update/' + video.uuid
116 constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) {
117 const absoluteAPIUrl = getAbsoluteAPIUrl()
119 this.createdAt = new Date(hash.createdAt.toString())
120 this.publishedAt = new Date(hash.publishedAt.toString())
121 this.category = hash.category
122 this.licence = hash.licence
123 this.language = hash.language
124 this.privacy = hash.privacy
125 this.waitTranscoding = hash.waitTranscoding
126 this.state = hash.state
127 this.description = hash.description
129 this.isLive = hash.isLive
131 this.duration = hash.duration
132 this.durationLabel = durationToString(hash.duration)
135 this.uuid = hash.uuid
136 this.shortUUID = hash.shortUUID
138 this.isLocal = hash.isLocal
139 this.name = hash.name
141 this.thumbnailPath = hash.thumbnailPath
142 this.thumbnailUrl = this.thumbnailPath
143 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
146 this.previewPath = hash.previewPath
147 this.previewUrl = this.previewPath
148 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
151 this.embedPath = hash.embedPath
152 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
156 this.views = hash.views
157 this.viewers = hash.viewers
158 this.likes = hash.likes
159 this.dislikes = hash.dislikes
161 this.nsfw = hash.nsfw
163 this.account = hash.account
164 this.channel = hash.channel
166 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
167 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
169 this.category.label = peertubeTranslate(this.category.label, translations)
170 this.licence.label = peertubeTranslate(this.licence.label, translations)
171 this.language.label = peertubeTranslate(this.language.label, translations)
172 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
174 this.scheduledUpdate = hash.scheduledUpdate
175 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
177 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
179 this.blacklisted = hash.blacklisted
180 this.blacklistedReason = hash.blacklistedReason
182 this.blockedOwner = hash.blockedOwner
183 this.blockedServer = hash.blockedServer
185 this.streamingPlaylists = hash.streamingPlaylists
186 this.files = hash.files
188 this.userHistory = hash.userHistory
190 this.originInstanceHost = this.account.host
191 this.originInstanceUrl = 'https://' + this.originInstanceHost
193 this.pluginData = hash.pluginData
196 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
197 // Video is not NSFW, skip
198 if (this.nsfw === false) return false
200 // Return user setting if logged in
201 if (user) return user.nsfwPolicy !== 'display'
203 // Return default instance config
204 return serverConfig.instance.defaultNSFWPolicy !== 'display'
207 isRemovableBy (user: AuthUser) {
208 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
211 isBlockableBy (user: AuthUser) {
212 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
215 isUnblockableBy (user: AuthUser) {
216 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
219 isUpdatableBy (user: AuthUser) {
220 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
223 canRemoveFiles (user: AuthUser) {
224 return user.hasRight(UserRight.MANAGE_VIDEO_FILES) &&
225 this.state.id !== VideoState.TO_TRANSCODE &&
230 canRunTranscoding (user: AuthUser) {
231 return user.hasRight(UserRight.RUN_VIDEO_TRANSCODING) &&
232 this.state.id !== VideoState.TO_TRANSCODE
236 return this.streamingPlaylists?.some(p => p.type === VideoStreamingPlaylistType.HLS)
240 return this.files && this.files.length !== 0
243 isLiveInfoAvailableBy (user: AuthUser) {
244 return this.isLive &&
245 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
248 canBeDuplicatedBy (user: AuthUser) {
249 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
252 getExactNumberOfViews () {
253 if (this.views < 1000) return ''
256 return $localize`${this.views} viewers`
259 return $localize`${this.views} views`