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 {
35 private static createByString (author: string, podHost: string) {
36 return author + '@' + podHost
39 private static createDurationString (duration: number) {
40 const minutes = Math.floor(duration / 60)
41 const seconds = duration % 60
42 const minutesPadding = minutes >= 10 ? '' : '0'
43 const secondsPadding = seconds >= 10 ? '' : '0'
45 return minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
50 createdAt: Date | string,
51 categoryLabel: string,
65 thumbnailPath: string,
73 this.author = hash.author
74 this.createdAt = new Date(hash.createdAt.toString())
75 this.categoryLabel = hash.categoryLabel
76 this.category = hash.category
77 this.licenceLabel = hash.licenceLabel
78 this.licence = hash.licence
79 this.languageLabel = hash.languageLabel
80 this.language = hash.language
81 this.description = hash.description
82 this.duration = hash.duration
83 this.durationLabel = Video.createDurationString(hash.duration)
86 this.isLocal = hash.isLocal
88 this.podHost = hash.podHost
90 this.thumbnailPath = hash.thumbnailPath
91 this.thumbnailUrl = API_URL + hash.thumbnailPath
92 this.previewPath = hash.previewPath
93 this.previewUrl = API_URL + hash.previewPath
94 this.views = hash.views
95 this.likes = hash.likes
96 this.dislikes = hash.dislikes
98 this.files = hash.files
100 this.by = Video.createByString(hash.author, hash.podHost)
103 isRemovableBy (user) {
104 return user && this.isLocal === true && (this.author === user.username || user.isAdmin() === true)
107 isBlackistableBy (user) {
108 return user && user.isAdmin() === true && this.isLocal === false
111 isUpdatableBy (user) {
112 return user && this.isLocal === true && user.username === this.author
115 isVideoNSFWForUser (user: User) {
116 // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos...
117 return (this.nsfw && (!user || user.displayNSFW === false))
120 getAppropriateMagnetUri (actualDownloadSpeed = 0) {
121 if (this.files === undefined || this.files.length === 0) return ''
122 if (this.files.length === 1) return this.files[0].magnetUri
124 // Find first video that is good for our download speed (remember they are sorted)
125 let betterResolutionFile = this.files.find(f => actualDownloadSpeed > (f.size / this.duration))
127 // If the download speed is too bad, return the lowest resolution we have
128 if (betterResolutionFile === undefined) {
129 betterResolutionFile = this.files.find(f => f.resolution === VideoResolution.H_240P)
132 return betterResolutionFile.magnetUri
135 patch (values: Object) {
136 Object.keys(values).forEach((key) => {
137 this[key] = values[key]
144 createdAt: this.createdAt,
145 category: this.category,
146 licence: this.licence,
147 language: this.language,
148 description: this.description,
149 duration: this.duration,
151 isLocal: this.isLocal,
153 podHost: this.podHost,
155 thumbnailPath: this.thumbnailPath,
158 dislikes: this.dislikes,