1 import { Video as VideoServerModel, VideoFile } from '../../../../../shared'
2 import { User } from '../../shared'
3 import { VideoResolution } from '../../../../../shared/models/videos/video-resolution.enum'
5 export class Video implements VideoServerModel {
37 private static createByString (author: string, podHost: string) {
38 return author + '@' + podHost
41 private static createDurationString (duration: number) {
42 const minutes = Math.floor(duration / 60)
43 const seconds = duration % 60
44 const minutesPadding = minutes >= 10 ? '' : '0'
45 const secondsPadding = seconds >= 10 ? '' : '0'
47 return minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
52 createdAt: Date | string,
53 categoryLabel: string,
67 thumbnailPath: string,
76 let absoluteAPIUrl = API_URL
77 if (!absoluteAPIUrl) {
78 // The API is on the same domain
79 absoluteAPIUrl = window.location.origin
82 this.author = hash.author
83 this.createdAt = new Date(hash.createdAt.toString())
84 this.categoryLabel = hash.categoryLabel
85 this.category = hash.category
86 this.licenceLabel = hash.licenceLabel
87 this.licence = hash.licence
88 this.languageLabel = hash.languageLabel
89 this.language = hash.language
90 this.description = hash.description
91 this.duration = hash.duration
92 this.durationLabel = Video.createDurationString(hash.duration)
95 this.isLocal = hash.isLocal
97 this.podHost = hash.podHost
99 this.thumbnailPath = hash.thumbnailPath
100 this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
101 this.previewPath = hash.previewPath
102 this.previewUrl = absoluteAPIUrl + hash.previewPath
103 this.embedPath = hash.embedPath
104 this.embedUrl = absoluteAPIUrl + hash.embedPath
105 this.views = hash.views
106 this.likes = hash.likes
107 this.dislikes = hash.dislikes
108 this.nsfw = hash.nsfw
109 this.files = hash.files
111 this.by = Video.createByString(hash.author, hash.podHost)
114 isRemovableBy (user) {
115 return user && this.isLocal === true && (this.author === user.username || user.isAdmin() === true)
118 isBlackistableBy (user) {
119 return user && user.isAdmin() === true && this.isLocal === false
122 isUpdatableBy (user) {
123 return user && this.isLocal === true && user.username === this.author
126 isVideoNSFWForUser (user: User) {
127 // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos...
128 return (this.nsfw && (!user || user.displayNSFW === false))
131 getAppropriateMagnetUri (actualDownloadSpeed = 0) {
132 if (this.files === undefined || this.files.length === 0) return ''
133 if (this.files.length === 1) return this.files[0].magnetUri
135 // Find first video that is good for our download speed (remember they are sorted)
136 let betterResolutionFile = this.files.find(f => actualDownloadSpeed > (f.size / this.duration))
138 // If the download speed is too bad, return the lowest resolution we have
139 if (betterResolutionFile === undefined) {
140 betterResolutionFile = this.files.find(f => f.resolution === VideoResolution.H_240P)
143 return betterResolutionFile.magnetUri
146 patch (values: Object) {
147 Object.keys(values).forEach((key) => {
148 this[key] = values[key]
155 createdAt: this.createdAt,
156 category: this.category,
157 licence: this.licence,
158 language: this.language,
159 description: this.description,
160 duration: this.duration,
162 isLocal: this.isLocal,
164 podHost: this.podHost,
166 thumbnailPath: this.thumbnailPath,
169 dislikes: this.dislikes,