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 { Account } from '@app/shared/shared-main/account/account.model'
5 import { Actor } from '@app/shared/shared-main/account/actor.model'
6 import { VideoChannel } from '@app/shared/shared-main/video-channel/video-channel.model'
7 import { peertubeTranslate } from '@shared/core-utils/i18n'
12 Video as VideoServerModel,
17 } from '@shared/models'
19 export class Video implements VideoServerModel {
20 byVideoChannel: string
26 originallyPublishedAt: Date | string
27 category: VideoConstant<number>
28 licence: VideoConstant<number>
29 language: VideoConstant<string>
30 privacy: VideoConstant<VideoPrivacy>
57 originInstanceUrl: string
58 originInstanceHost: string
60 waitTranscoding?: boolean
61 state?: VideoConstant<VideoState>
62 scheduledUpdate?: VideoScheduleUpdate
64 blockedReason?: string
90 static buildClientUrl (videoUUID: string) {
91 return '/w/' + videoUUID
94 constructor (hash: VideoServerModel, translations = {}) {
95 const absoluteAPIUrl = getAbsoluteAPIUrl()
97 this.createdAt = new Date(hash.createdAt.toString())
98 this.publishedAt = new Date(hash.publishedAt.toString())
99 this.category = hash.category
100 this.licence = hash.licence
101 this.language = hash.language
102 this.privacy = hash.privacy
103 this.waitTranscoding = hash.waitTranscoding
104 this.state = hash.state
105 this.description = hash.description
107 this.isLive = hash.isLive
109 this.duration = hash.duration
110 this.durationLabel = durationToString(hash.duration)
113 this.uuid = hash.uuid
115 this.isLocal = hash.isLocal
116 this.name = hash.name
118 this.thumbnailPath = hash.thumbnailPath
119 this.thumbnailUrl = this.thumbnailPath
120 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
123 this.previewPath = hash.previewPath
124 this.previewUrl = this.previewPath
125 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
128 this.embedPath = hash.embedPath
129 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
133 this.views = hash.views
134 this.likes = hash.likes
135 this.dislikes = hash.dislikes
137 this.nsfw = hash.nsfw
139 this.account = hash.account
140 this.channel = hash.channel
142 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
143 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
145 this.category.label = peertubeTranslate(this.category.label, translations)
146 this.licence.label = peertubeTranslate(this.licence.label, translations)
147 this.language.label = peertubeTranslate(this.language.label, translations)
148 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
150 this.scheduledUpdate = hash.scheduledUpdate
151 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
153 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
155 this.blacklisted = hash.blacklisted
156 this.blockedReason = hash.blacklistedReason
158 this.userHistory = hash.userHistory
160 this.originInstanceHost = this.account.host
161 this.originInstanceUrl = 'https://' + this.originInstanceHost
163 this.pluginData = hash.pluginData
166 isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
167 // Video is not NSFW, skip
168 if (this.nsfw === false) return false
170 // Return user setting if logged in
171 if (user) return user.nsfwPolicy !== 'display'
173 // Return default instance config
174 return serverConfig.instance.defaultNSFWPolicy !== 'display'
177 isRemovableBy (user: AuthUser) {
178 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
181 isBlockableBy (user: AuthUser) {
182 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
185 isUnblockableBy (user: AuthUser) {
186 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
189 isUpdatableBy (user: AuthUser) {
190 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
193 isLiveInfoAvailableBy (user: AuthUser) {
194 return this.isLive &&
195 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
198 canBeDuplicatedBy (user: AuthUser) {
199 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
202 getExactNumberOfViews () {
203 if (this.views < 1000) return ''
206 return $localize`${this.views} viewers`
209 return $localize`${this.views} views`