]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/shared/video.model.ts
Add oembed endpoint
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.model.ts
CommitLineData
93e1258c 1import { Video as VideoServerModel, VideoFile } from '../../../../../shared'
df98563e 2import { User } from '../../shared'
aa8b6df4 3import { VideoResolution } from '../../../../../shared/models/videos/video-resolution.enum'
92fb909c 4
69f616ab 5export class Video implements VideoServerModel {
df98563e
C
6 author: string
7 by: string
8 createdAt: Date
6d33593a 9 updatedAt: Date
df98563e
C
10 categoryLabel: string
11 category: number
12 licenceLabel: string
13 licence: number
14 languageLabel: string
15 language: number
16 description: string
17 duration: number
18 durationLabel: string
0a6658fd
C
19 id: number
20 uuid: string
df98563e 21 isLocal: boolean
df98563e
C
22 name: string
23 podHost: string
24 tags: string[]
25 thumbnailPath: string
26 thumbnailUrl: string
43f61d26
C
27 previewPath: string
28 previewUrl: string
d8755eed
C
29 embedPath: string
30 embedUrl: string
df98563e
C
31 views: number
32 likes: number
33 dislikes: number
34 nsfw: boolean
93e1258c 35 files: VideoFile[]
aff038cd 36
df98563e
C
37 private static createByString (author: string, podHost: string) {
38 return author + '@' + podHost
aff038cd
C
39 }
40
df98563e
C
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'
4fd8aa32 46
df98563e 47 return minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
4fd8aa32
C
48 }
49
df98563e 50 constructor (hash: {
501bc6c2 51 author: string,
d592e0a9 52 createdAt: Date | string,
6e07c3de 53 categoryLabel: string,
69f616ab 54 category: number,
d07137b9 55 licenceLabel: string,
69f616ab 56 licence: number,
df98563e
C
57 languageLabel: string
58 language: number
4fd8aa32 59 description: string,
df98563e 60 duration: number
0a6658fd
C
61 id: number,
62 uuid: string,
4fd8aa32 63 isLocal: boolean,
4fd8aa32 64 name: string,
49abbbbe 65 podHost: string,
00a44645 66 tags: string[],
05a9feaa 67 thumbnailPath: string,
43f61d26 68 previewPath: string,
d8755eed 69 embedPath: string,
d38b8281
C
70 views: number,
71 likes: number,
72 dislikes: number,
93e1258c
C
73 nsfw: boolean,
74 files: VideoFile[]
501bc6c2 75 }) {
df98563e 76 this.author = hash.author
d592e0a9 77 this.createdAt = new Date(hash.createdAt.toString())
df98563e
C
78 this.categoryLabel = hash.categoryLabel
79 this.category = hash.category
80 this.licenceLabel = hash.licenceLabel
81 this.licence = hash.licence
82 this.languageLabel = hash.languageLabel
83 this.language = hash.language
84 this.description = hash.description
85 this.duration = hash.duration
86 this.durationLabel = Video.createDurationString(hash.duration)
87 this.id = hash.id
0a6658fd 88 this.uuid = hash.uuid
df98563e 89 this.isLocal = hash.isLocal
df98563e
C
90 this.name = hash.name
91 this.podHost = hash.podHost
92 this.tags = hash.tags
93 this.thumbnailPath = hash.thumbnailPath
94 this.thumbnailUrl = API_URL + hash.thumbnailPath
43f61d26
C
95 this.previewPath = hash.previewPath
96 this.previewUrl = API_URL + hash.previewPath
d8755eed
C
97 this.embedPath = hash.embedPath
98 this.embedUrl = API_URL + hash.embedPath
df98563e
C
99 this.views = hash.views
100 this.likes = hash.likes
101 this.dislikes = hash.dislikes
102 this.nsfw = hash.nsfw
93e1258c 103 this.files = hash.files
4fd8aa32 104
df98563e 105 this.by = Video.createByString(hash.author, hash.podHost)
501bc6c2
C
106 }
107
df98563e
C
108 isRemovableBy (user) {
109 return user && this.isLocal === true && (this.author === user.username || user.isAdmin() === true)
198b205c
GS
110 }
111
df98563e
C
112 isBlackistableBy (user) {
113 return user && user.isAdmin() === true && this.isLocal === false
501bc6c2 114 }
92fb909c 115
df98563e
C
116 isUpdatableBy (user) {
117 return user && this.isLocal === true && user.username === this.author
9eee32fc
C
118 }
119
df98563e 120 isVideoNSFWForUser (user: User) {
92fb909c 121 // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos...
df98563e 122 return (this.nsfw && (!user || user.displayNSFW === false))
92fb909c 123 }
d8e689b8 124
aa8b6df4 125 getAppropriateMagnetUri (actualDownloadSpeed = 0) {
93e1258c 126 if (this.files === undefined || this.files.length === 0) return ''
aa8b6df4 127 if (this.files.length === 1) return this.files[0].magnetUri
93e1258c 128
aa8b6df4
C
129 // Find first video that is good for our download speed (remember they are sorted)
130 let betterResolutionFile = this.files.find(f => actualDownloadSpeed > (f.size / this.duration))
131
132 // If the download speed is too bad, return the lowest resolution we have
133 if (betterResolutionFile === undefined) {
134 betterResolutionFile = this.files.find(f => f.resolution === VideoResolution.H_240P)
135 }
136
137 return betterResolutionFile.magnetUri
93e1258c
C
138 }
139
df98563e 140 patch (values: Object) {
d8e689b8 141 Object.keys(values).forEach((key) => {
df98563e
C
142 this[key] = values[key]
143 })
d8e689b8
C
144 }
145
df98563e 146 toJSON () {
d8e689b8
C
147 return {
148 author: this.author,
149 createdAt: this.createdAt,
150 category: this.category,
151 licence: this.licence,
152 language: this.language,
153 description: this.description,
154 duration: this.duration,
155 id: this.id,
156 isLocal: this.isLocal,
d8e689b8
C
157 name: this.name,
158 podHost: this.podHost,
159 tags: this.tags,
160 thumbnailPath: this.thumbnailPath,
161 views: this.views,
162 likes: this.likes,
163 dislikes: this.dislikes,
93e1258c
C
164 nsfw: this.nsfw,
165 files: this.files
df98563e 166 }
d8e689b8 167 }
501bc6c2 168}