5bb0510f91546be137f8fd4dbbe0c8012e3a85e0
[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   downloadEnabled: boolean
18   waitTranscoding: boolean
19   channelId: number
20   privacy: VideoPrivacy
21   support: string
22   thumbnailfile?: any
23   previewfile?: any
24   thumbnailUrl: string
25   previewUrl: string
26   uuid?: string
27   id?: number
28   scheduleUpdate?: VideoScheduleUpdate
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     }
60   }
61
62   patch (values: Object) {
63     Object.keys(values).forEach((key) => {
64       this[ key ] = values[ key ]
65     })
66
67     // If schedule publication, the video is private and will be changed to public privacy
68     if (parseInt(values['privacy'], 10) === VideoEdit.SPECIAL_SCHEDULED_PRIVACY) {
69       const updateAt = (values['schedulePublicationAt'] as Date)
70       updateAt.setSeconds(0)
71
72       this.privacy = VideoPrivacy.PRIVATE
73       this.scheduleUpdate = {
74         updateAt: updateAt.toISOString(),
75         privacy: VideoPrivacy.PUBLIC
76       }
77     } else {
78       this.scheduleUpdate = null
79     }
80   }
81
82   toFormPatch () {
83     const json = {
84       category: this.category,
85       licence: this.licence,
86       language: this.language,
87       description: this.description,
88       support: this.support,
89       name: this.name,
90       tags: this.tags,
91       nsfw: this.nsfw,
92       commentsEnabled: this.commentsEnabled,
93       downloadEnabled: this.downloadEnabled,
94       waitTranscoding: this.waitTranscoding,
95       channelId: this.channelId,
96       privacy: this.privacy
97     }
98
99     // Special case if we scheduled an update
100     if (this.scheduleUpdate) {
101       Object.assign(json, {
102         privacy: VideoEdit.SPECIAL_SCHEDULED_PRIVACY,
103         schedulePublicationAt: new Date(this.scheduleUpdate.updateAt.toString())
104       })
105     }
106
107     return json
108   }
109 }