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