]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/video/video-edit.model.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / video / video-edit.model.ts
1 import { getAbsoluteAPIUrl } from '@app/helpers'
2 import { VideoPrivacy, VideoScheduleUpdate, VideoUpdate } from '@shared/models'
3 import { VideoDetails } from './video-details.model'
4 import { objectKeysTyped } from '@shared/core-utils'
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 scheduleUpdate?: VideoScheduleUpdate
27 originallyPublishedAt?: Date | string
28
29 id?: number
30 uuid?: string
31 shortUUID?: string
32
33 pluginData?: any
34
35 constructor (video?: VideoDetails) {
36 if (!video) return
37
38 this.id = video.id
39 this.uuid = video.uuid
40 this.shortUUID = video.shortUUID
41 this.category = video.category.id
42 this.licence = video.licence.id
43 this.language = video.language.id
44 this.description = video.description
45 this.name = video.name
46 this.tags = video.tags
47 this.nsfw = video.nsfw
48 this.waitTranscoding = video.waitTranscoding
49 this.channelId = video.channel.id
50 this.privacy = video.privacy.id
51
52 this.support = video.support
53
54 this.commentsEnabled = video.commentsEnabled
55 this.downloadEnabled = video.downloadEnabled
56
57 if (video.thumbnailPath) this.thumbnailUrl = getAbsoluteAPIUrl() + video.thumbnailPath
58 if (video.previewPath) this.previewUrl = getAbsoluteAPIUrl() + video.previewPath
59
60 this.scheduleUpdate = video.scheduledUpdate
61 this.originallyPublishedAt = video.originallyPublishedAt
62 ? new Date(video.originallyPublishedAt)
63 : null
64
65 this.pluginData = video.pluginData
66 }
67
68 patch (values: { [ id: string ]: any }) {
69 objectKeysTyped(values).forEach(key => {
70 // FIXME: typings
71 (this as any)[key] = values[key]
72 })
73
74 // If schedule publication, the video is private and will be changed to public privacy
75 if (parseInt(values['privacy'], 10) === VideoEdit.SPECIAL_SCHEDULED_PRIVACY) {
76 const updateAt = new Date(values['schedulePublicationAt'])
77 updateAt.setSeconds(0)
78
79 this.privacy = VideoPrivacy.PRIVATE
80 this.scheduleUpdate = {
81 updateAt: updateAt.toISOString(),
82 privacy: VideoPrivacy.PUBLIC
83 }
84 } else {
85 this.scheduleUpdate = null
86 }
87
88 // Convert originallyPublishedAt to string so that function objectToFormData() works correctly
89 if (this.originallyPublishedAt) {
90 const originallyPublishedAt = new Date(this.originallyPublishedAt)
91 this.originallyPublishedAt = originallyPublishedAt.toISOString()
92 }
93
94 // Use the same file than the preview for the thumbnail
95 if (this.previewfile) {
96 this.thumbnailfile = this.previewfile
97 }
98 }
99
100 toFormPatch () {
101 const json = {
102 category: this.category,
103 licence: this.licence,
104 language: this.language,
105 description: this.description,
106 support: this.support,
107 name: this.name,
108 tags: this.tags,
109 nsfw: this.nsfw,
110 commentsEnabled: this.commentsEnabled,
111 downloadEnabled: this.downloadEnabled,
112 waitTranscoding: this.waitTranscoding,
113 channelId: this.channelId,
114 privacy: this.privacy,
115 originallyPublishedAt: this.originallyPublishedAt
116 }
117
118 // Special case if we scheduled an update
119 if (this.scheduleUpdate) {
120 Object.assign(json, {
121 privacy: VideoEdit.SPECIAL_SCHEDULED_PRIVACY,
122 schedulePublicationAt: new Date(this.scheduleUpdate.updateAt.toString())
123 })
124 }
125
126 return json
127 }
128 }