]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/video-edit.model.ts
436598af66dd15d18c41d4c9a1e2eb47c0f8aeb6
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video / video-edit.model.ts
1 import { Video, VideoPrivacy, VideoScheduleUpdate, VideoUpdate } from '@shared/models'
2
3 export class VideoEdit implements VideoUpdate {
4 static readonly SPECIAL_SCHEDULED_PRIVACY = -1
5
6 category: number
7 licence: number
8 language: string
9 description: string
10 name: string
11 tags: string[]
12 nsfw: boolean
13 commentsEnabled: boolean
14 downloadEnabled: boolean
15 waitTranscoding: boolean
16 channelId: number
17 privacy: VideoPrivacy
18 support: string
19 thumbnailfile?: any
20 previewfile?: any
21 thumbnailUrl: string
22 previewUrl: string
23 scheduleUpdate?: VideoScheduleUpdate
24 originallyPublishedAt?: Date | string
25
26 id?: number
27 uuid?: string
28 shortUUID?: string
29
30 pluginData?: any
31
32 constructor (
33 video?: Video & {
34 tags: string[]
35 commentsEnabled: boolean
36 downloadEnabled: boolean
37 support: string
38 thumbnailUrl: string
39 previewUrl: string
40 }) {
41 if (video) {
42 this.id = video.id
43 this.uuid = video.uuid
44 this.category = video.category.id
45 this.licence = video.licence.id
46 this.language = video.language.id
47 this.description = video.description
48 this.name = video.name
49 this.tags = video.tags
50 this.nsfw = video.nsfw
51 this.commentsEnabled = video.commentsEnabled
52 this.downloadEnabled = video.downloadEnabled
53 this.waitTranscoding = video.waitTranscoding
54 this.channelId = video.channel.id
55 this.privacy = video.privacy.id
56 this.support = video.support
57 this.thumbnailUrl = video.thumbnailUrl
58 this.previewUrl = video.previewUrl
59
60 this.scheduleUpdate = video.scheduledUpdate
61 this.originallyPublishedAt = video.originallyPublishedAt ? new Date(video.originallyPublishedAt) : null
62
63 this.pluginData = video.pluginData
64 }
65 }
66
67 patch (values: { [ id: string ]: any }) {
68 Object.keys(values).forEach((key) => {
69 this[key] = values[key]
70 })
71
72 // If schedule publication, the video is private and will be changed to public privacy
73 if (parseInt(values['privacy'], 10) === VideoEdit.SPECIAL_SCHEDULED_PRIVACY) {
74 const updateAt = new Date(values['schedulePublicationAt'])
75 updateAt.setSeconds(0)
76
77 this.privacy = VideoPrivacy.PRIVATE
78 this.scheduleUpdate = {
79 updateAt: updateAt.toISOString(),
80 privacy: VideoPrivacy.PUBLIC
81 }
82 } else {
83 this.scheduleUpdate = null
84 }
85
86 // Convert originallyPublishedAt to string so that function objectToFormData() works correctly
87 if (this.originallyPublishedAt) {
88 const originallyPublishedAt = new Date(values['originallyPublishedAt'])
89 this.originallyPublishedAt = originallyPublishedAt.toISOString()
90 }
91
92 // Use the same file than the preview for the thumbnail
93 if (this.previewfile) {
94 this.thumbnailfile = this.previewfile
95 }
96 }
97
98 toFormPatch () {
99 const json = {
100 category: this.category,
101 licence: this.licence,
102 language: this.language,
103 description: this.description,
104 support: this.support,
105 name: this.name,
106 tags: this.tags,
107 nsfw: this.nsfw,
108 commentsEnabled: this.commentsEnabled,
109 downloadEnabled: this.downloadEnabled,
110 waitTranscoding: this.waitTranscoding,
111 channelId: this.channelId,
112 privacy: this.privacy,
113 originallyPublishedAt: this.originallyPublishedAt
114 }
115
116 // Special case if we scheduled an update
117 if (this.scheduleUpdate) {
118 Object.assign(json, {
119 privacy: VideoEdit.SPECIAL_SCHEDULED_PRIVACY,
120 schedulePublicationAt: new Date(this.scheduleUpdate.updateAt.toString())
121 })
122 }
123
124 return json
125 }
126 }