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
23 accountAvatarUrl: string
24 videoChannelAvatarUrl: string
29 originallyPublishedAt: Date | string
30 category: VideoConstant<number>
31 licence: VideoConstant<number>
32 language: VideoConstant<string>
33 privacy: VideoConstant<VideoPrivacy>
60 originInstanceUrl: string
61 originInstanceHost: string
63 waitTranscoding?: boolean
64 state?: VideoConstant<VideoState>
65 scheduledUpdate?: VideoScheduleUpdate
67 blockedReason?: string
93 static buildClientUrl (videoUUID: string) {
94 return '/videos/watch/' + videoUUID
97 constructor (hash: VideoServerModel, translations = {}) {
98 const absoluteAPIUrl = getAbsoluteAPIUrl()
100 this.createdAt = new Date(hash.createdAt.toString())
101 this.publishedAt = new Date(hash.publishedAt.toString())
102 this.category = hash.category
103 this.licence = hash.licence
104 this.language = hash.language
105 this.privacy = hash.privacy
106 this.waitTranscoding = hash.waitTranscoding
107 this.state = hash.state
108 this.description = hash.description
110 this.isLive = hash.isLive
112 this.duration = hash.duration
113 this.durationLabel = durationToString(hash.duration)
116 this.uuid = hash.uuid
118 this.isLocal = hash.isLocal
119 this.name = hash.name
121 this.thumbnailPath = hash.thumbnailPath
122 this.thumbnailUrl = this.thumbnailPath
123 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
126 this.previewPath = hash.previewPath
127 this.previewUrl = this.previewPath
128 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
131 this.embedPath = hash.embedPath
132 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
136 this.views = hash.views
137 this.likes = hash.likes
138 this.dislikes = hash.dislikes
140 this.nsfw = hash.nsfw
142 this.account = hash.account
143 this.channel = hash.channel
145 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
146 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
147 this.accountAvatarUrl = Account.GET_ACTOR_AVATAR_URL(this.account)
148 this.videoChannelAvatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(this.channel)
150 this.category.label = peertubeTranslate(this.category.label, translations)
151 this.licence.label = peertubeTranslate(this.licence.label, translations)
152 this.language.label = peertubeTranslate(this.language.label, translations)
153 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
155 this.scheduledUpdate = hash.scheduledUpdate
156 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
158 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
160 this.blacklisted = hash.blacklisted
161 this.blockedReason = hash.blacklistedReason
163 this.userHistory = hash.userHistory
165 this.originInstanceHost = this.account.host
166 this.originInstanceUrl = 'https://' + this.originInstanceHost
168 this.pluginData = hash.pluginData
171 isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
172 // Video is not NSFW, skip
173 if (this.nsfw === false) return false
175 // Return user setting if logged in
176 if (user) return user.nsfwPolicy !== 'display'
178 // Return default instance config
179 return serverConfig.instance.defaultNSFWPolicy !== 'display'
182 isRemovableBy (user: AuthUser) {
183 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
186 isBlockableBy (user: AuthUser) {
187 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
190 isUnblockableBy (user: AuthUser) {
191 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
194 isUpdatableBy (user: AuthUser) {
195 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
198 isLiveInfoAvailableBy (user: AuthUser) {
199 return this.isLive &&
200 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
203 canBeDuplicatedBy (user: AuthUser) {
204 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
207 getExactNumberOfViews () {
208 if (this.views < 1000) return ''
211 return $localize`${this.views} viewers`
214 return $localize`${this.views} views`