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,
16 } from '@shared/models'
18 export class Video implements VideoServerModel {
19 byVideoChannel: string
25 originallyPublishedAt: Date | string
26 category: VideoConstant<number>
27 licence: VideoConstant<number>
28 language: VideoConstant<string>
29 privacy: VideoConstant<VideoPrivacy>
62 originInstanceUrl: string
63 originInstanceHost: string
65 waitTranscoding?: boolean
66 state?: VideoConstant<VideoState>
67 scheduledUpdate?: VideoScheduleUpdate
69 blockedReason?: string
95 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
96 return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid })
99 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
100 return '/videos/update/' + video.uuid
103 constructor (hash: VideoServerModel, translations = {}) {
104 const absoluteAPIUrl = getAbsoluteAPIUrl()
106 this.createdAt = new Date(hash.createdAt.toString())
107 this.publishedAt = new Date(hash.publishedAt.toString())
108 this.category = hash.category
109 this.licence = hash.licence
110 this.language = hash.language
111 this.privacy = hash.privacy
112 this.waitTranscoding = hash.waitTranscoding
113 this.state = hash.state
114 this.description = hash.description
116 this.isLive = hash.isLive
118 this.duration = hash.duration
119 this.durationLabel = durationToString(hash.duration)
122 this.uuid = hash.uuid
123 this.shortUUID = hash.shortUUID
125 this.isLocal = hash.isLocal
126 this.name = hash.name
128 this.thumbnailPath = hash.thumbnailPath
129 this.thumbnailUrl = this.thumbnailPath
130 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
133 this.previewPath = hash.previewPath
134 this.previewUrl = this.previewPath
135 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
138 this.embedPath = hash.embedPath
139 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
143 this.views = hash.views
144 this.likes = hash.likes
145 this.dislikes = hash.dislikes
147 this.nsfw = hash.nsfw
149 this.account = hash.account
150 this.channel = hash.channel
152 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
153 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
155 this.category.label = peertubeTranslate(this.category.label, translations)
156 this.licence.label = peertubeTranslate(this.licence.label, translations)
157 this.language.label = peertubeTranslate(this.language.label, translations)
158 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
160 this.scheduledUpdate = hash.scheduledUpdate
161 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
163 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
165 this.blacklisted = hash.blacklisted
166 this.blockedReason = hash.blacklistedReason
168 this.userHistory = hash.userHistory
170 this.originInstanceHost = this.account.host
171 this.originInstanceUrl = 'https://' + this.originInstanceHost
173 this.pluginData = hash.pluginData
176 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
177 // Video is not NSFW, skip
178 if (this.nsfw === false) return false
180 // Return user setting if logged in
181 if (user) return user.nsfwPolicy !== 'display'
183 // Return default instance config
184 return serverConfig.instance.defaultNSFWPolicy !== 'display'
187 isRemovableBy (user: AuthUser) {
188 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
191 isBlockableBy (user: AuthUser) {
192 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
195 isUnblockableBy (user: AuthUser) {
196 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
199 isUpdatableBy (user: AuthUser) {
200 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
203 isLiveInfoAvailableBy (user: AuthUser) {
204 return this.isLive &&
205 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
208 canBeDuplicatedBy (user: AuthUser) {
209 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
212 getExactNumberOfViews () {
213 if (this.views < 1000) return ''
216 return $localize`${this.views} viewers`
219 return $localize`${this.views} views`