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 { Account, Actor, VideoChannel } from '@app/shared/shared-main'
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>
58 originInstanceUrl: string
59 originInstanceHost: string
61 waitTranscoding?: boolean
62 state?: VideoConstant<VideoState>
63 scheduledUpdate?: VideoScheduleUpdate
65 blockedReason?: string
91 static buildClientUrl (videoUUID: string) {
92 return '/videos/watch/' + videoUUID
95 constructor (hash: VideoServerModel, translations = {}) {
96 const absoluteAPIUrl = getAbsoluteAPIUrl()
98 this.createdAt = new Date(hash.createdAt.toString())
99 this.publishedAt = new Date(hash.publishedAt.toString())
100 this.category = hash.category
101 this.licence = hash.licence
102 this.language = hash.language
103 this.privacy = hash.privacy
104 this.waitTranscoding = hash.waitTranscoding
105 this.state = hash.state
106 this.description = hash.description
108 this.isLive = hash.isLive
110 this.duration = hash.duration
111 this.durationLabel = durationToString(hash.duration)
114 this.uuid = hash.uuid
116 this.isLocal = hash.isLocal
117 this.name = hash.name
119 this.thumbnailPath = hash.thumbnailPath
120 this.thumbnailUrl = this.thumbnailPath
121 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
124 this.previewPath = hash.previewPath
125 this.previewUrl = this.previewPath
126 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
129 this.embedPath = hash.embedPath
130 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
134 this.views = hash.views
135 this.likes = hash.likes
136 this.dislikes = hash.dislikes
138 this.nsfw = hash.nsfw
140 this.account = hash.account
141 this.channel = hash.channel
143 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
144 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
145 this.accountAvatarUrl = Account.GET_ACTOR_AVATAR_URL(this.account)
146 this.videoChannelAvatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(this.channel)
148 this.category.label = peertubeTranslate(this.category.label, translations)
149 this.licence.label = peertubeTranslate(this.licence.label, translations)
150 this.language.label = peertubeTranslate(this.language.label, translations)
151 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
153 this.scheduledUpdate = hash.scheduledUpdate
154 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
156 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
158 this.blacklisted = hash.blacklisted
159 this.blockedReason = hash.blacklistedReason
161 this.userHistory = hash.userHistory
163 this.originInstanceHost = this.account.host
164 this.originInstanceUrl = 'https://' + this.originInstanceHost
166 this.pluginData = hash.pluginData
169 isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
170 // Video is not NSFW, skip
171 if (this.nsfw === false) return false
173 // Return user setting if logged in
174 if (user) return user.nsfwPolicy !== 'display'
176 // Return default instance config
177 return serverConfig.instance.defaultNSFWPolicy !== 'display'
180 isRemovableBy (user: AuthUser) {
181 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
184 isBlockableBy (user: AuthUser) {
185 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
188 isUnblockableBy (user: AuthUser) {
189 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
192 isUpdatableBy (user: AuthUser) {
193 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
196 isLiveInfoAvailableBy (user: AuthUser) {
197 return this.isLive &&
198 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
201 canBeDuplicatedBy (user: AuthUser) {
202 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)