]>
Commit | Line | Data |
---|---|---|
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.shortUUID = video.shortUUID | |
45 | this.category = video.category.id | |
46 | this.licence = video.licence.id | |
47 | this.language = video.language.id | |
48 | this.description = video.description | |
49 | this.name = video.name | |
50 | this.tags = video.tags | |
51 | this.nsfw = video.nsfw | |
52 | this.commentsEnabled = video.commentsEnabled | |
53 | this.downloadEnabled = video.downloadEnabled | |
54 | this.waitTranscoding = video.waitTranscoding | |
55 | this.channelId = video.channel.id | |
56 | this.privacy = video.privacy.id | |
57 | this.support = video.support | |
58 | this.thumbnailUrl = video.thumbnailUrl | |
59 | this.previewUrl = video.previewUrl | |
60 | ||
61 | this.scheduleUpdate = video.scheduledUpdate | |
62 | this.originallyPublishedAt = video.originallyPublishedAt ? new Date(video.originallyPublishedAt) : null | |
63 | ||
64 | this.pluginData = video.pluginData | |
65 | } | |
66 | } | |
67 | ||
68 | patch (values: { [ id: string ]: any }) { | |
69 | Object.keys(values).forEach((key) => { | |
70 | this[key] = values[key] | |
71 | }) | |
72 | ||
73 | // If schedule publication, the video is private and will be changed to public privacy | |
74 | if (parseInt(values['privacy'], 10) === VideoEdit.SPECIAL_SCHEDULED_PRIVACY) { | |
75 | const updateAt = new Date(values['schedulePublicationAt']) | |
76 | updateAt.setSeconds(0) | |
77 | ||
78 | this.privacy = VideoPrivacy.PRIVATE | |
79 | this.scheduleUpdate = { | |
80 | updateAt: updateAt.toISOString(), | |
81 | privacy: VideoPrivacy.PUBLIC | |
82 | } | |
83 | } else { | |
84 | this.scheduleUpdate = null | |
85 | } | |
86 | ||
87 | // Convert originallyPublishedAt to string so that function objectToFormData() works correctly | |
88 | if (this.originallyPublishedAt) { | |
89 | const originallyPublishedAt = new Date(values['originallyPublishedAt']) | |
90 | this.originallyPublishedAt = originallyPublishedAt.toISOString() | |
91 | } | |
92 | ||
93 | // Use the same file than the preview for the thumbnail | |
94 | if (this.previewfile) { | |
95 | this.thumbnailfile = this.previewfile | |
96 | } | |
97 | } | |
98 | ||
99 | toFormPatch () { | |
100 | const json = { | |
101 | category: this.category, | |
102 | licence: this.licence, | |
103 | language: this.language, | |
104 | description: this.description, | |
105 | support: this.support, | |
106 | name: this.name, | |
107 | tags: this.tags, | |
108 | nsfw: this.nsfw, | |
109 | commentsEnabled: this.commentsEnabled, | |
110 | downloadEnabled: this.downloadEnabled, | |
111 | waitTranscoding: this.waitTranscoding, | |
112 | channelId: this.channelId, | |
113 | privacy: this.privacy, | |
114 | originallyPublishedAt: this.originallyPublishedAt | |
115 | } | |
116 | ||
117 | // Special case if we scheduled an update | |
118 | if (this.scheduleUpdate) { | |
119 | Object.assign(json, { | |
120 | privacy: VideoEdit.SPECIAL_SCHEDULED_PRIVACY, | |
121 | schedulePublicationAt: new Date(this.scheduleUpdate.updateAt.toString()) | |
122 | }) | |
123 | } | |
124 | ||
125 | return json | |
126 | } | |
127 | } |