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>
64 originInstanceUrl: string
65 originInstanceHost: string
67 waitTranscoding?: boolean
68 state?: VideoConstant<VideoState>
69 scheduledUpdate?: VideoScheduleUpdate
72 blacklistedReason?: string
74 blockedOwner?: boolean
75 blockedServer?: boolean
101 streamingPlaylists?: VideoStreamingPlaylist[]
104 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
105 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
108 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
109 return '/videos/update/' + video.uuid
112 constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) {
113 const absoluteAPIUrl = getAbsoluteAPIUrl()
115 this.createdAt = new Date(hash.createdAt.toString())
116 this.publishedAt = new Date(hash.publishedAt.toString())
117 this.category = hash.category
118 this.licence = hash.licence
119 this.language = hash.language
120 this.privacy = hash.privacy
121 this.waitTranscoding = hash.waitTranscoding
122 this.state = hash.state
123 this.description = hash.description
125 this.isLive = hash.isLive
127 this.duration = hash.duration
128 this.durationLabel = durationToString(hash.duration)
131 this.uuid = hash.uuid
132 this.shortUUID = hash.shortUUID
134 this.isLocal = hash.isLocal
135 this.name = hash.name
137 this.thumbnailPath = hash.thumbnailPath
138 this.thumbnailUrl = this.thumbnailPath
139 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
142 this.previewPath = hash.previewPath
143 this.previewUrl = this.previewPath
144 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
147 this.embedPath = hash.embedPath
148 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
152 this.views = hash.views
153 this.likes = hash.likes
154 this.dislikes = hash.dislikes
156 this.nsfw = hash.nsfw
158 this.account = hash.account
159 this.channel = hash.channel
161 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
162 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
164 this.category.label = peertubeTranslate(this.category.label, translations)
165 this.licence.label = peertubeTranslate(this.licence.label, translations)
166 this.language.label = peertubeTranslate(this.language.label, translations)
167 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
169 this.scheduledUpdate = hash.scheduledUpdate
170 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
172 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
174 this.blacklisted = hash.blacklisted
175 this.blacklistedReason = hash.blacklistedReason
177 this.blockedOwner = hash.blockedOwner
178 this.blockedServer = hash.blockedServer
180 this.streamingPlaylists = hash.streamingPlaylists
181 this.files = hash.files
183 this.userHistory = hash.userHistory
185 this.originInstanceHost = this.account.host
186 this.originInstanceUrl = 'https://' + this.originInstanceHost
188 this.pluginData = hash.pluginData
191 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
192 // Video is not NSFW, skip
193 if (this.nsfw === false) return false
195 // Return user setting if logged in
196 if (user) return user.nsfwPolicy !== 'display'
198 // Return default instance config
199 return serverConfig.instance.defaultNSFWPolicy !== 'display'
202 isRemovableBy (user: AuthUser) {
203 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
206 isBlockableBy (user: AuthUser) {
207 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
210 isUnblockableBy (user: AuthUser) {
211 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
214 isUpdatableBy (user: AuthUser) {
215 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
218 isLiveInfoAvailableBy (user: AuthUser) {
219 return this.isLive &&
220 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
223 canBeDuplicatedBy (user: AuthUser) {
224 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
227 getExactNumberOfViews () {
228 if (this.views < 1000) return ''
231 return $localize`${this.views} viewers`
234 return $localize`${this.views} views`