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 } from '@shared/models'
20 export class Video implements VideoServerModel {
21 byVideoChannel: string
27 originallyPublishedAt: Date | string
28 category: VideoConstant<number>
29 licence: VideoConstant<number>
30 language: VideoConstant<string>
31 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
104 streamingPlaylists?: VideoStreamingPlaylist[]
107 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
108 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
111 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
112 return '/videos/update/' + video.uuid
115 constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) {
116 const absoluteAPIUrl = getAbsoluteAPIUrl()
118 this.createdAt = new Date(hash.createdAt.toString())
119 this.publishedAt = new Date(hash.publishedAt.toString())
120 this.category = hash.category
121 this.licence = hash.licence
122 this.language = hash.language
123 this.privacy = hash.privacy
124 this.waitTranscoding = hash.waitTranscoding
125 this.state = hash.state
126 this.description = hash.description
128 this.isLive = hash.isLive
130 this.duration = hash.duration
131 this.durationLabel = durationToString(hash.duration)
134 this.uuid = hash.uuid
135 this.shortUUID = hash.shortUUID
137 this.isLocal = hash.isLocal
138 this.name = hash.name
140 this.thumbnailPath = hash.thumbnailPath
141 this.thumbnailUrl = this.thumbnailPath
142 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
145 this.previewPath = hash.previewPath
146 this.previewUrl = this.previewPath
147 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
150 this.embedPath = hash.embedPath
151 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
155 this.views = hash.views
156 this.viewers = hash.viewers
157 this.likes = hash.likes
158 this.dislikes = hash.dislikes
160 this.nsfw = hash.nsfw
162 this.account = hash.account
163 this.channel = hash.channel
165 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
166 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
168 this.category.label = peertubeTranslate(this.category.label, translations)
169 this.licence.label = peertubeTranslate(this.licence.label, translations)
170 this.language.label = peertubeTranslate(this.language.label, translations)
171 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
173 this.scheduledUpdate = hash.scheduledUpdate
174 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
176 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
178 this.blacklisted = hash.blacklisted
179 this.blacklistedReason = hash.blacklistedReason
181 this.blockedOwner = hash.blockedOwner
182 this.blockedServer = hash.blockedServer
184 this.streamingPlaylists = hash.streamingPlaylists
185 this.files = hash.files
187 this.userHistory = hash.userHistory
189 this.originInstanceHost = this.account.host
190 this.originInstanceUrl = 'https://' + this.originInstanceHost
192 this.pluginData = hash.pluginData
195 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
196 // Video is not NSFW, skip
197 if (this.nsfw === false) return false
199 // Return user setting if logged in
200 if (user) return user.nsfwPolicy !== 'display'
202 // Return default instance config
203 return serverConfig.instance.defaultNSFWPolicy !== 'display'
206 isRemovableBy (user: AuthUser) {
207 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
210 isBlockableBy (user: AuthUser) {
211 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
214 isUnblockableBy (user: AuthUser) {
215 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
218 isUpdatableBy (user: AuthUser) {
219 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
222 isLiveInfoAvailableBy (user: AuthUser) {
223 return this.isLive &&
224 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
227 canBeDuplicatedBy (user: AuthUser) {
228 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
231 getExactNumberOfViews () {
232 if (this.views < 1000) return ''
235 return $localize`${this.views} viewers`
238 return $localize`${this.views} views`