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 { Actor } from '@app/shared/shared-main/account/actor.model'
5 import { peertubeTranslate } from '@shared/core-utils/i18n'
10 Video as VideoServerModel,
15 } from '@shared/models'
17 export class Video implements VideoServerModel {
18 byVideoChannel: string
24 originallyPublishedAt: Date | string
25 category: VideoConstant<number>
26 licence: VideoConstant<number>
27 language: VideoConstant<string>
28 privacy: VideoConstant<VideoPrivacy>
55 originInstanceUrl: string
56 originInstanceHost: string
58 waitTranscoding?: boolean
59 state?: VideoConstant<VideoState>
60 scheduledUpdate?: VideoScheduleUpdate
62 blockedReason?: string
88 static buildClientUrl (videoUUID: string) {
89 return '/w/' + videoUUID
92 constructor (hash: VideoServerModel, translations = {}) {
93 const absoluteAPIUrl = getAbsoluteAPIUrl()
95 this.createdAt = new Date(hash.createdAt.toString())
96 this.publishedAt = new Date(hash.publishedAt.toString())
97 this.category = hash.category
98 this.licence = hash.licence
99 this.language = hash.language
100 this.privacy = hash.privacy
101 this.waitTranscoding = hash.waitTranscoding
102 this.state = hash.state
103 this.description = hash.description
105 this.isLive = hash.isLive
107 this.duration = hash.duration
108 this.durationLabel = durationToString(hash.duration)
111 this.uuid = hash.uuid
113 this.isLocal = hash.isLocal
114 this.name = hash.name
116 this.thumbnailPath = hash.thumbnailPath
117 this.thumbnailUrl = this.thumbnailPath
118 ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath)
121 this.previewPath = hash.previewPath
122 this.previewUrl = this.previewPath
123 ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath)
126 this.embedPath = hash.embedPath
127 this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath)
131 this.views = hash.views
132 this.likes = hash.likes
133 this.dislikes = hash.dislikes
135 this.nsfw = hash.nsfw
137 this.account = hash.account
138 this.channel = hash.channel
140 this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
141 this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
143 this.category.label = peertubeTranslate(this.category.label, translations)
144 this.licence.label = peertubeTranslate(this.licence.label, translations)
145 this.language.label = peertubeTranslate(this.language.label, translations)
146 this.privacy.label = peertubeTranslate(this.privacy.label, translations)
148 this.scheduledUpdate = hash.scheduledUpdate
149 this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
151 if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
153 this.blacklisted = hash.blacklisted
154 this.blockedReason = hash.blacklistedReason
156 this.userHistory = hash.userHistory
158 this.originInstanceHost = this.account.host
159 this.originInstanceUrl = 'https://' + this.originInstanceHost
161 this.pluginData = hash.pluginData
164 isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) {
165 // Video is not NSFW, skip
166 if (this.nsfw === false) return false
168 // Return user setting if logged in
169 if (user) return user.nsfwPolicy !== 'display'
171 // Return default instance config
172 return serverConfig.instance.defaultNSFWPolicy !== 'display'
175 isRemovableBy (user: AuthUser) {
176 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
179 isBlockableBy (user: AuthUser) {
180 return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
183 isUnblockableBy (user: AuthUser) {
184 return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
187 isUpdatableBy (user: AuthUser) {
188 return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
191 isLiveInfoAvailableBy (user: AuthUser) {
192 return this.isLive &&
193 user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE))
196 canBeDuplicatedBy (user: AuthUser) {
197 return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
200 getExactNumberOfViews () {
201 if (this.views < 1000) return ''
204 return $localize`${this.views} viewers`
207 return $localize`${this.views} views`