]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.model.ts
Fix share embed iframe link
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.model.ts
1 import { Video as VideoServerModel, VideoFile } from '../../../../../shared'
2 import { User } from '../../shared'
3 import { VideoResolution } from '../../../../../shared/models/videos/video-resolution.enum'
4
5 export class Video implements VideoServerModel {
6 author: string
7 by: string
8 createdAt: Date
9 updatedAt: Date
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
19 id: number
20 uuid: string
21 isLocal: boolean
22 name: string
23 podHost: string
24 tags: string[]
25 thumbnailPath: string
26 thumbnailUrl: string
27 previewPath: string
28 previewUrl: string
29 embedPath: string
30 embedUrl: string
31 views: number
32 likes: number
33 dislikes: number
34 nsfw: boolean
35 files: VideoFile[]
36
37 private static createByString (author: string, podHost: string) {
38 return author + '@' + podHost
39 }
40
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'
46
47 return minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
48 }
49
50 constructor (hash: {
51 author: string,
52 createdAt: Date | string,
53 categoryLabel: string,
54 category: number,
55 licenceLabel: string,
56 licence: number,
57 languageLabel: string
58 language: number
59 description: string,
60 duration: number
61 id: number,
62 uuid: string,
63 isLocal: boolean,
64 name: string,
65 podHost: string,
66 tags: string[],
67 thumbnailPath: string,
68 previewPath: string,
69 embedPath: string,
70 views: number,
71 likes: number,
72 dislikes: number,
73 nsfw: boolean,
74 files: VideoFile[]
75 }) {
76 let absoluteAPIUrl = API_URL
77 if (!absoluteAPIUrl) {
78 // The API is on the same domain
79 absoluteAPIUrl = window.location.origin
80 }
81
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)
93 this.id = hash.id
94 this.uuid = hash.uuid
95 this.isLocal = hash.isLocal
96 this.name = hash.name
97 this.podHost = hash.podHost
98 this.tags = hash.tags
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
110
111 this.by = Video.createByString(hash.author, hash.podHost)
112 }
113
114 isRemovableBy (user) {
115 return user && this.isLocal === true && (this.author === user.username || user.isAdmin() === true)
116 }
117
118 isBlackistableBy (user) {
119 return user && user.isAdmin() === true && this.isLocal === false
120 }
121
122 isUpdatableBy (user) {
123 return user && this.isLocal === true && user.username === this.author
124 }
125
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))
129 }
130
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
134
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))
137
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)
141 }
142
143 return betterResolutionFile.magnetUri
144 }
145
146 patch (values: Object) {
147 Object.keys(values).forEach((key) => {
148 this[key] = values[key]
149 })
150 }
151
152 toJSON () {
153 return {
154 author: this.author,
155 createdAt: this.createdAt,
156 category: this.category,
157 licence: this.licence,
158 language: this.language,
159 description: this.description,
160 duration: this.duration,
161 id: this.id,
162 isLocal: this.isLocal,
163 name: this.name,
164 podHost: this.podHost,
165 tags: this.tags,
166 thumbnailPath: this.thumbnailPath,
167 views: this.views,
168 likes: this.likes,
169 dislikes: this.dislikes,
170 nsfw: this.nsfw,
171 files: this.files
172 }
173 }
174 }