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
89 static buildClientUrl (videoUUID: string) {
90 return '/videos/watch/' + videoUUID
93 constructor (hash: VideoServerModel, translations = {}) {
94 const absoluteAPIUrl = getAbsoluteAPIUrl()
96 this.createdAt = new Date(hash.createdAt.toString())
97 this.publishedAt = new Date(hash.publishedAt.toString())
98 this.category = hash.category
99 this.licence = hash.licence
100 this.language = hash.language
101 this.privacy = hash.privacy
102 this.waitTranscoding = hash.waitTranscoding
103 this.state = hash.state
104 this.description = hash.description
106 this.duration = hash.duration
107 this.durationLabel = durationToString(hash.duration)
110 this.uuid = hash.uuid
112 this.isLocal = hash.isLocal
113 this.name = hash.name
115 this.thumbnailPath = hash.thumbnailPath
116 this.thumbnailUrl = hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
118 this.previewPath = hash.previewPath
119 this.previewUrl = hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
121 this.embedPath = hash.embedPath
122 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
126 this.views = hash.views
127 this.likes = hash.likes
128 this.dislikes = hash.dislikes
130 this.nsfw = hash.nsfw
132 this.account = hash.account
133 this.channel = hash.channel
135 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
136 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
137 this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
138 this.videoChannelAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.channel)
140 this.category.label = peertubeTranslate(this.category.label, translations)
141 this.licence.label = peertubeTranslate(this.licence.label, translations)
142 this.language.label = peertubeTranslate(this.language.label, translations)
143 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
145 this.scheduledUpdate = hash.scheduledUpdate
146 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
148 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
150 this.blacklisted = hash.blacklisted
151 this.blockedReason = hash.blacklistedReason
153 this.userHistory = hash.userHistory
155 this.originInstanceHost = this.account.host
156 this.originInstanceUrl = 'https://' + this.originInstanceHost
158 this.pluginData = hash.pluginData
161 isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
162 // Video is not NSFW, skip
163 if (this.nsfw === false) return false
165 // Return user setting if logged in
166 if (user) return user.nsfwPolicy !== 'display'
168 // Return default instance config
169 return serverConfig.instance.defaultNSFWPolicy !== 'display'
172 isRemovableBy (user: AuthUser) {
173 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
176 isBlockableBy (user: AuthUser) {
177 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
180 isUnblockableBy (user: AuthUser) {
181 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
184 isUpdatableBy (user: AuthUser) {
185 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
188 canBeDuplicatedBy (user: AuthUser) {
189 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)