1 import { User } from '../'
2 import { UserRight, Video as VideoServerModel, VideoPrivacy, VideoState } from '../../../../../shared'
3 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
4 import { VideoConstant } from '../../../../../shared/models/videos/video-constant.model'
5 import { durationToString, getAbsoluteAPIUrl } from '../misc/utils'
6 import { peertubeTranslate, ServerConfig } from '../../../../../shared/models'
7 import { Actor } from '@app/shared/actor/actor.model'
8 import { VideoScheduleUpdate } from '../../../../../shared/models/videos/video-schedule-update.model'
9 import { AuthUser } from '@app/core'
11 export class Video implements VideoServerModel {
12 byVideoChannel: string
15 accountAvatarUrl: string
16 videoChannelAvatarUrl: string
21 originallyPublishedAt: Date | string
22 category: VideoConstant<number>
23 licence: VideoConstant<number>
24 language: VideoConstant<string>
25 privacy: VideoConstant<VideoPrivacy>
50 originInstanceUrl: string
51 originInstanceHost: string
53 waitTranscoding?: boolean
54 state?: VideoConstant<VideoState>
55 scheduledUpdate?: VideoScheduleUpdate
57 blockedReason?: string
81 static buildClientUrl (videoUUID: string) {
82 return '/videos/watch/' + videoUUID
85 constructor (hash: VideoServerModel, translations = {}) {
86 const absoluteAPIUrl = getAbsoluteAPIUrl()
88 this.createdAt = new Date(hash.createdAt.toString())
89 this.publishedAt = new Date(hash.publishedAt.toString())
90 this.category = hash.category
91 this.licence = hash.licence
92 this.language = hash.language
93 this.privacy = hash.privacy
94 this.waitTranscoding = hash.waitTranscoding
95 this.state = hash.state
96 this.description = hash.description
98 this.duration = hash.duration
99 this.durationLabel = durationToString(hash.duration)
102 this.uuid = hash.uuid
104 this.isLocal = hash.isLocal
105 this.name = hash.name
107 this.thumbnailPath = hash.thumbnailPath
108 this.thumbnailUrl = hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
110 this.previewPath = hash.previewPath
111 this.previewUrl = hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
113 this.embedPath = hash.embedPath
114 this.embedUrl = hash.embedUrl || (absoluteAPIUrl + hash.embedPath)
118 this.views = hash.views
119 this.likes = hash.likes
120 this.dislikes = hash.dislikes
122 this.nsfw = hash.nsfw
124 this.account = hash.account
125 this.channel = hash.channel
127 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
128 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
129 this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
130 this.videoChannelAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.channel)
132 this.category.label = peertubeTranslate(this.category.label, translations)
133 this.licence.label = peertubeTranslate(this.licence.label, translations)
134 this.language.label = peertubeTranslate(this.language.label, translations)
135 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
137 this.scheduledUpdate = hash.scheduledUpdate
138 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
140 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
142 this.blacklisted = hash.blacklisted
143 this.blockedReason = hash.blacklistedReason
145 this.userHistory = hash.userHistory
147 this.originInstanceHost = this.account.host
148 this.originInstanceUrl = 'https://' + this.originInstanceHost
151 isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
152 // Video is not NSFW, skip
153 if (this.nsfw === false) return false
155 // Return user setting if logged in
156 if (user) return user.nsfwPolicy !== 'display'
158 // Return default instance config
159 return serverConfig.instance.defaultNSFWPolicy !== 'display'
162 isRemovableBy (user: AuthUser) {
163 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
166 isBlockableBy (user: AuthUser) {
167 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLOCKS) === true
170 isUnblockableBy (user: AuthUser) {
171 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLOCKS) === true
174 isUpdatableBy (user: AuthUser) {
175 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
178 canBeDuplicatedBy (user: AuthUser) {
179 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)