]>
Commit | Line | Data |
---|---|---|
93e1258c | 1 | import { Video as VideoServerModel, VideoFile } from '../../../../../shared' |
df98563e | 2 | import { User } from '../../shared' |
aa8b6df4 | 3 | import { VideoResolution } from '../../../../../shared/models/videos/video-resolution.enum' |
92fb909c | 4 | |
69f616ab | 5 | export 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 | }) { |
c6e0bfbf C |
76 | let absoluteAPIUrl = API_URL |
77 | if (!absoluteAPIUrl) { | |
78 | // The API is on the same domain | |
79 | absoluteAPIUrl = window.location.origin | |
80 | } | |
81 | ||
df98563e | 82 | this.author = hash.author |
d592e0a9 | 83 | this.createdAt = new Date(hash.createdAt.toString()) |
df98563e C |
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 | |
0a6658fd | 94 | this.uuid = hash.uuid |
df98563e | 95 | this.isLocal = hash.isLocal |
df98563e C |
96 | this.name = hash.name |
97 | this.podHost = hash.podHost | |
98 | this.tags = hash.tags | |
99 | this.thumbnailPath = hash.thumbnailPath | |
c6e0bfbf | 100 | this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath |
43f61d26 | 101 | this.previewPath = hash.previewPath |
c6e0bfbf | 102 | this.previewUrl = absoluteAPIUrl + hash.previewPath |
d8755eed | 103 | this.embedPath = hash.embedPath |
c6e0bfbf | 104 | this.embedUrl = absoluteAPIUrl + hash.embedPath |
df98563e C |
105 | this.views = hash.views |
106 | this.likes = hash.likes | |
107 | this.dislikes = hash.dislikes | |
108 | this.nsfw = hash.nsfw | |
93e1258c | 109 | this.files = hash.files |
4fd8aa32 | 110 | |
df98563e | 111 | this.by = Video.createByString(hash.author, hash.podHost) |
501bc6c2 C |
112 | } |
113 | ||
df98563e C |
114 | isRemovableBy (user) { |
115 | return user && this.isLocal === true && (this.author === user.username || user.isAdmin() === true) | |
198b205c GS |
116 | } |
117 | ||
df98563e C |
118 | isBlackistableBy (user) { |
119 | return user && user.isAdmin() === true && this.isLocal === false | |
501bc6c2 | 120 | } |
92fb909c | 121 | |
df98563e C |
122 | isUpdatableBy (user) { |
123 | return user && this.isLocal === true && user.username === this.author | |
9eee32fc C |
124 | } |
125 | ||
df98563e | 126 | isVideoNSFWForUser (user: User) { |
92fb909c | 127 | // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos... |
df98563e | 128 | return (this.nsfw && (!user || user.displayNSFW === false)) |
92fb909c | 129 | } |
d8e689b8 | 130 | |
aa8b6df4 | 131 | getAppropriateMagnetUri (actualDownloadSpeed = 0) { |
93e1258c | 132 | if (this.files === undefined || this.files.length === 0) return '' |
aa8b6df4 | 133 | if (this.files.length === 1) return this.files[0].magnetUri |
93e1258c | 134 | |
aa8b6df4 C |
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 | |
93e1258c C |
144 | } |
145 | ||
df98563e | 146 | patch (values: Object) { |
d8e689b8 | 147 | Object.keys(values).forEach((key) => { |
df98563e C |
148 | this[key] = values[key] |
149 | }) | |
d8e689b8 C |
150 | } |
151 | ||
df98563e | 152 | toJSON () { |
d8e689b8 C |
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, | |
d8e689b8 C |
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, | |
93e1258c C |
170 | nsfw: this.nsfw, |
171 | files: this.files | |
df98563e | 172 | } |
d8e689b8 | 173 | } |
501bc6c2 | 174 | } |