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