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 { peertubeTranslate } from '@shared/core-utils/i18n'
9 Video as VideoServerModel,
14 } from '@shared/models'
15 import { Actor } from '../account/actor.model'
17 export class Video implements VideoServerModel {
18 byVideoChannel: string
21 accountAvatarUrl: string
22 videoChannelAvatarUrl: string
27 originallyPublishedAt: Date | string
28 category: VideoConstant<number>
29 licence: VideoConstant<number>
30 language: VideoConstant<string>
31 privacy: VideoConstant<VideoPrivacy>
56 originInstanceUrl: string
57 originInstanceHost: string
59 waitTranscoding?: boolean
60 state?: VideoConstant<VideoState>
61 scheduledUpdate?: VideoScheduleUpdate
63 blockedReason?: string
87 static buildClientUrl (videoUUID: string) {
88 return '/videos/watch/' + videoUUID
91 constructor (hash: VideoServerModel, translations = {}) {
92 const absoluteAPIUrl = getAbsoluteAPIUrl()
94 this.createdAt = new Date(hash.createdAt.toString())
95 this.publishedAt = new Date(hash.publishedAt.toString())
96 this.category = hash.category
97 this.licence = hash.licence
98 this.language = hash.language
99 this.privacy = hash.privacy
100 this.waitTranscoding = hash.waitTranscoding
101 this.state = hash.state
102 this.description = hash.description
104 this.duration = hash.duration
105 this.durationLabel = durationToString(hash.duration)
108 this.uuid = hash.uuid
110 this.isLocal = hash.isLocal
111 this.name = hash.name
113 this.thumbnailPath = hash.thumbnailPath
114 this.thumbnailUrl = hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
116 this.previewPath = hash.previewPath
117 this.previewUrl = hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
119 this.embedPath = hash.embedPath
120 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
124 this.views = hash.views
125 this.likes = hash.likes
126 this.dislikes = hash.dislikes
128 this.nsfw = hash.nsfw
130 this.account = hash.account
131 this.channel = hash.channel
133 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
134 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
135 this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
136 this.videoChannelAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.channel)
138 this.category.label = peertubeTranslate(this.category.label, translations)
139 this.licence.label = peertubeTranslate(this.licence.label, translations)
140 this.language.label = peertubeTranslate(this.language.label, translations)
141 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
143 this.scheduledUpdate = hash.scheduledUpdate
144 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
146 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
148 this.blacklisted = hash.blacklisted
149 this.blockedReason = hash.blacklistedReason
151 this.userHistory = hash.userHistory
153 this.originInstanceHost = this.account.host
154 this.originInstanceUrl = 'https://' + this.originInstanceHost
157 isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
158 // Video is not NSFW, skip
159 if (this.nsfw === false) return false
161 // Return user setting if logged in
162 if (user) return user.nsfwPolicy !== 'display'
164 // Return default instance config
165 return serverConfig.instance.defaultNSFWPolicy !== 'display'
168 isRemovableBy (user: AuthUser) {
169 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
172 isBlockableBy (user: AuthUser) {
173 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
176 isUnblockableBy (user: AuthUser) {
177 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
180 isUpdatableBy (user: AuthUser) {
181 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
184 canBeDuplicatedBy (user: AuthUser) {
185 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)