78aed4f9f8d92523f9b9d9cd42686213233f8901
[oweals/peertube.git] / client / src / app / shared / video / video-edit.model.ts
1 import { VideoDetails } from './video-details.model'
2 import { VideoPrivacy } from '../../../../../shared/models/videos/video-privacy.enum'
3 import { VideoUpdate } from '../../../../../shared/models/videos'
4 import { VideoScheduleUpdate } from '../../../../../shared/models/videos/video-schedule-update.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
29   constructor (videoDetails?: VideoDetails) {
30     if (videoDetails) {
31       this.id = videoDetails.id
32       this.uuid = videoDetails.uuid
33       this.category = videoDetails.category.id
34       this.licence = videoDetails.licence.id
35       this.language = videoDetails.language.id
36       this.description = videoDetails.description
37       this.name = videoDetails.name
38       this.tags = videoDetails.tags
39       this.nsfw = videoDetails.nsfw
40       this.commentsEnabled = videoDetails.commentsEnabled
41       this.waitTranscoding = videoDetails.waitTranscoding
42       this.channelId = videoDetails.channel.id
43       this.privacy = videoDetails.privacy.id
44       this.support = videoDetails.support
45       this.thumbnailUrl = videoDetails.thumbnailUrl
46       this.previewUrl = videoDetails.previewUrl
47
48       this.scheduleUpdate = videoDetails.scheduledUpdate
49     }
50   }
51
52   patch (values: Object) {
53     Object.keys(values).forEach((key) => {
54       this[ key ] = values[ key ]
55     })
56
57     // If schedule publication, the video is private and will be changed to public privacy
58     if (values['schedulePublicationAt']) {
59       const updateAt = (values['schedulePublicationAt'] as Date)
60       updateAt.setSeconds(0)
61
62       this.privacy = VideoPrivacy.PRIVATE
63       this.scheduleUpdate = {
64         updateAt: updateAt.toISOString(),
65         privacy: VideoPrivacy.PUBLIC
66       }
67     }
68   }
69
70   toFormPatch () {
71     const json = {
72       category: this.category,
73       licence: this.licence,
74       language: this.language,
75       description: this.description,
76       support: this.support,
77       name: this.name,
78       tags: this.tags,
79       nsfw: this.nsfw,
80       commentsEnabled: this.commentsEnabled,
81       waitTranscoding: this.waitTranscoding,
82       channelId: this.channelId,
83       privacy: this.privacy
84     }
85
86     // Special case if we scheduled an update
87     if (this.scheduleUpdate) {
88       Object.assign(json, {
89         privacy: VideoEdit.SPECIAL_SCHEDULED_PRIVACY,
90         schedulePublicationAt: new Date(this.scheduleUpdate.updateAt.toString())
91       })
92     }
93
94     return json
95   }
96 }