]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.model.ts
b315e59b184451990927505140fe070d7e14e91e
[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 views: number
30 likes: number
31 dislikes: number
32 nsfw: boolean
33 files: VideoFile[]
34
35 private static createByString (author: string, podHost: string) {
36 return author + '@' + podHost
37 }
38
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'
44
45 return minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
46 }
47
48 constructor (hash: {
49 author: string,
50 createdAt: Date | string,
51 categoryLabel: string,
52 category: number,
53 licenceLabel: string,
54 licence: number,
55 languageLabel: string
56 language: number
57 description: string,
58 duration: number
59 id: number,
60 uuid: string,
61 isLocal: boolean,
62 name: string,
63 podHost: string,
64 tags: string[],
65 thumbnailPath: string,
66 previewPath: string,
67 views: number,
68 likes: number,
69 dislikes: number,
70 nsfw: boolean,
71 files: VideoFile[]
72 }) {
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)
84 this.id = hash.id
85 this.uuid = hash.uuid
86 this.isLocal = hash.isLocal
87 this.name = hash.name
88 this.podHost = hash.podHost
89 this.tags = hash.tags
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
97 this.nsfw = hash.nsfw
98 this.files = hash.files
99
100 this.by = Video.createByString(hash.author, hash.podHost)
101 }
102
103 isRemovableBy (user) {
104 return user && this.isLocal === true && (this.author === user.username || user.isAdmin() === true)
105 }
106
107 isBlackistableBy (user) {
108 return user && user.isAdmin() === true && this.isLocal === false
109 }
110
111 isUpdatableBy (user) {
112 return user && this.isLocal === true && user.username === this.author
113 }
114
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))
118 }
119
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
123
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))
126
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)
130 }
131
132 return betterResolutionFile.magnetUri
133 }
134
135 patch (values: Object) {
136 Object.keys(values).forEach((key) => {
137 this[key] = values[key]
138 })
139 }
140
141 toJSON () {
142 return {
143 author: this.author,
144 createdAt: this.createdAt,
145 category: this.category,
146 licence: this.licence,
147 language: this.language,
148 description: this.description,
149 duration: this.duration,
150 id: this.id,
151 isLocal: this.isLocal,
152 name: this.name,
153 podHost: this.podHost,
154 tags: this.tags,
155 thumbnailPath: this.thumbnailPath,
156 views: this.views,
157 likes: this.likes,
158 dislikes: this.dislikes,
159 nsfw: this.nsfw,
160 files: this.files
161 }
162 }
163 }