]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/shared/video.model.ts
Add preview to embed
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.model.ts
1 import { Video as VideoServerModel } 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 magnetUri: string
21 name: string
22 podHost: string
23 tags: string[]
24 thumbnailPath: string
25 thumbnailUrl: string
26 previewPath: string
27 previewUrl: string
28 views: number
29 likes: number
30 dislikes: number
31 nsfw: boolean
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 magnetUri: string,
61 name: string,
62 podHost: string,
63 tags: string[],
64 thumbnailPath: string,
65 previewPath: string,
66 views: number,
67 likes: number,
68 dislikes: number,
69 nsfw: boolean
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.magnetUri = hash.magnetUri
86 this.name = hash.name
87 this.podHost = hash.podHost
88 this.tags = hash.tags
89 this.thumbnailPath = hash.thumbnailPath
90 this.thumbnailUrl = API_URL + hash.thumbnailPath
91 this.previewPath = hash.previewPath
92 this.previewUrl = API_URL + hash.previewPath
93 this.views = hash.views
94 this.likes = hash.likes
95 this.dislikes = hash.dislikes
96 this.nsfw = hash.nsfw
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 patch (values: Object) {
119 Object.keys(values).forEach((key) => {
120 this[key] = values[key]
121 })
122 }
123
124 toJSON () {
125 return {
126 author: this.author,
127 createdAt: this.createdAt,
128 category: this.category,
129 licence: this.licence,
130 language: this.language,
131 description: this.description,
132 duration: this.duration,
133 id: this.id,
134 isLocal: this.isLocal,
135 magnetUri: this.magnetUri,
136 name: this.name,
137 podHost: this.podHost,
138 tags: this.tags,
139 thumbnailPath: this.thumbnailPath,
140 views: this.views,
141 likes: this.likes,
142 dislikes: this.dislikes,
143 nsfw: this.nsfw
144 }
145 }
146 }