]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.model.ts
Move video file metadata in their own table
[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
4 export class Video implements VideoServerModel {
5 author: string
6 by: string
7 createdAt: Date
8 categoryLabel: string
9 category: number
10 licenceLabel: string
11 licence: number
12 languageLabel: string
13 language: number
14 description: string
15 duration: number
16 durationLabel: string
17 id: number
18 uuid: string
19 isLocal: boolean
20 name: string
21 podHost: string
22 tags: string[]
23 thumbnailPath: string
24 thumbnailUrl: string
25 previewPath: string
26 previewUrl: string
27 views: number
28 likes: number
29 dislikes: number
30 nsfw: boolean
31 files: VideoFile[]
32
33 private static createByString (author: string, podHost: string) {
34 return author + '@' + podHost
35 }
36
37 private static createDurationString (duration: number) {
38 const minutes = Math.floor(duration / 60)
39 const seconds = duration % 60
40 const minutesPadding = minutes >= 10 ? '' : '0'
41 const secondsPadding = seconds >= 10 ? '' : '0'
42
43 return minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
44 }
45
46 constructor (hash: {
47 author: string,
48 createdAt: string,
49 categoryLabel: string,
50 category: number,
51 licenceLabel: string,
52 licence: number,
53 languageLabel: string
54 language: number
55 description: string,
56 duration: number
57 id: number,
58 uuid: string,
59 isLocal: boolean,
60 name: string,
61 podHost: string,
62 tags: string[],
63 thumbnailPath: string,
64 previewPath: string,
65 views: number,
66 likes: number,
67 dislikes: number,
68 nsfw: boolean,
69 files: VideoFile[]
70 }) {
71 this.author = hash.author
72 this.createdAt = new Date(hash.createdAt)
73 this.categoryLabel = hash.categoryLabel
74 this.category = hash.category
75 this.licenceLabel = hash.licenceLabel
76 this.licence = hash.licence
77 this.languageLabel = hash.languageLabel
78 this.language = hash.language
79 this.description = hash.description
80 this.duration = hash.duration
81 this.durationLabel = Video.createDurationString(hash.duration)
82 this.id = hash.id
83 this.uuid = hash.uuid
84 this.isLocal = hash.isLocal
85 this.name = hash.name
86 this.podHost = hash.podHost
87 this.tags = hash.tags
88 this.thumbnailPath = hash.thumbnailPath
89 this.thumbnailUrl = API_URL + hash.thumbnailPath
90 this.previewPath = hash.previewPath
91 this.previewUrl = API_URL + hash.previewPath
92 this.views = hash.views
93 this.likes = hash.likes
94 this.dislikes = hash.dislikes
95 this.nsfw = hash.nsfw
96 this.files = hash.files
97
98 this.by = Video.createByString(hash.author, hash.podHost)
99 }
100
101 isRemovableBy (user) {
102 return user && this.isLocal === true && (this.author === user.username || user.isAdmin() === true)
103 }
104
105 isBlackistableBy (user) {
106 return user && user.isAdmin() === true && this.isLocal === false
107 }
108
109 isUpdatableBy (user) {
110 return user && this.isLocal === true && user.username === this.author
111 }
112
113 isVideoNSFWForUser (user: User) {
114 // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos...
115 return (this.nsfw && (!user || user.displayNSFW === false))
116 }
117
118 getDefaultMagnetUri () {
119 if (this.files === undefined || this.files.length === 0) return ''
120
121 // TODO: choose the original file
122 return this.files[0].magnetUri
123 }
124
125 patch (values: Object) {
126 Object.keys(values).forEach((key) => {
127 this[key] = values[key]
128 })
129 }
130
131 toJSON () {
132 return {
133 author: this.author,
134 createdAt: this.createdAt,
135 category: this.category,
136 licence: this.licence,
137 language: this.language,
138 description: this.description,
139 duration: this.duration,
140 id: this.id,
141 isLocal: this.isLocal,
142 name: this.name,
143 podHost: this.podHost,
144 tags: this.tags,
145 thumbnailPath: this.thumbnailPath,
146 views: this.views,
147 likes: this.likes,
148 dislikes: this.dislikes,
149 nsfw: this.nsfw,
150 files: this.files
151 }
152 }
153 }