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 { peertubeTranslate } from '@shared/core-utils/i18n'
10 Video as VideoServerModel,
15 } from '@shared/models'
17 export class Video implements VideoServerModel {
18 byVideoChannel: string
24 originallyPublishedAt: Date | string
25 category: VideoConstant<number>
26 licence: VideoConstant<number>
27 language: VideoConstant<string>
28 privacy: VideoConstant<VideoPrivacy>
61 originInstanceUrl: string
62 originInstanceHost: string
64 waitTranscoding?: boolean
65 state?: VideoConstant<VideoState>
66 scheduledUpdate?: VideoScheduleUpdate
68 blockedReason?: string
94 static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) {
95 return '/w/' + (video.shortUUID || video.uuid)
98 static buildUpdateUrl (video: Pick<Video, 'uuid'>) {
99 return '/videos/update/' + video.uuid
102 constructor (hash: VideoServerModel, translations = {}) {
103 const absoluteAPIUrl = getAbsoluteAPIUrl()
105 this.createdAt = new Date(hash.createdAt.toString())
106 this.publishedAt = new Date(hash.publishedAt.toString())
107 this.category = hash.category
108 this.licence = hash.licence
109 this.language = hash.language
110 this.privacy = hash.privacy
111 this.waitTranscoding = hash.waitTranscoding
112 this.state = hash.state
113 this.description = hash.description
115 this.isLive = hash.isLive
117 this.duration = hash.duration
118 this.durationLabel = durationToString(hash.duration)
121 this.uuid = hash.uuid
122 this.shortUUID = hash.shortUUID
124 this.isLocal = hash.isLocal
125 this.name = hash.name
127 this.thumbnailPath = hash.thumbnailPath
128 this.thumbnailUrl = this.thumbnailPath
129 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
132 this.previewPath = hash.previewPath
133 this.previewUrl = this.previewPath
134 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
137 this.embedPath = hash.embedPath
138 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
142 this.views = hash.views
143 this.likes = hash.likes
144 this.dislikes = hash.dislikes
146 this.nsfw = hash.nsfw
148 this.account = hash.account
149 this.channel = hash.channel
151 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
152 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
154 this.category.label = peertubeTranslate(this.category.label, translations)
155 this.licence.label = peertubeTranslate(this.licence.label, translations)
156 this.language.label = peertubeTranslate(this.language.label, translations)
157 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
159 this.scheduledUpdate = hash.scheduledUpdate
160 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
162 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
164 this.blacklisted = hash.blacklisted
165 this.blockedReason = hash.blacklistedReason
167 this.userHistory = hash.userHistory
169 this.originInstanceHost = this.account.host
170 this.originInstanceUrl = 'https://' + this.originInstanceHost
172 this.pluginData = hash.pluginData
175 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
176 // Video is not NSFW, skip
177 if (this.nsfw === false) return false
179 // Return user setting if logged in
180 if (user) return user.nsfwPolicy !== 'display'
182 // Return default instance config
183 return serverConfig.instance.defaultNSFWPolicy !== 'display'
186 isRemovableBy (user: AuthUser) {
187 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
190 isBlockableBy (user: AuthUser) {
191 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
194 isUnblockableBy (user: AuthUser) {
195 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
198 isUpdatableBy (user: AuthUser) {
199 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
202 isLiveInfoAvailableBy (user: AuthUser) {
203 return this.isLive &&
204 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
207 canBeDuplicatedBy (user: AuthUser) {
208 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
211 getExactNumberOfViews () {
212 if (this.views < 1000) return ''
215 return $localize`${this.views} viewers`
218 return $localize`${this.views} views`