From e94fc29706cd8e8fd892182d4de0a3ae80a3820f Mon Sep 17 00:00:00 2001
From: Chocobozzz <me@florianbigard.com>
Date: Mon, 18 Jun 2018 10:24:53 +0200
Subject: [PATCH] Fix removing scheduled update

---
 client/src/app/shared/video/video-edit.model.ts |  4 +++-
 client/src/app/shared/video/video.service.ts    |  3 ++-
 server/controllers/api/videos/index.ts          |  4 +++-
 server/middlewares/validators/videos.ts         |  6 ++++++
 server/models/video/schedule-video-update.ts    | 11 +++++++++++
 5 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/client/src/app/shared/video/video-edit.model.ts b/client/src/app/shared/video/video-edit.model.ts
index 78aed4f9f8..8562f8d254 100644
--- a/client/src/app/shared/video/video-edit.model.ts
+++ b/client/src/app/shared/video/video-edit.model.ts
@@ -55,7 +55,7 @@ export class VideoEdit implements VideoUpdate {
     })
 
     // If schedule publication, the video is private and will be changed to public privacy
-    if (values['schedulePublicationAt']) {
+    if (parseInt(values['privacy'], 10) === VideoEdit.SPECIAL_SCHEDULED_PRIVACY) {
       const updateAt = (values['schedulePublicationAt'] as Date)
       updateAt.setSeconds(0)
 
@@ -64,6 +64,8 @@ export class VideoEdit implements VideoUpdate {
         updateAt: updateAt.toISOString(),
         privacy: VideoPrivacy.PUBLIC
       }
+    } else {
+      this.scheduleUpdate = null
     }
   }
 
diff --git a/client/src/app/shared/video/video.service.ts b/client/src/app/shared/video/video.service.ts
index 3af90e7ad6..2da36ff1b7 100644
--- a/client/src/app/shared/video/video.service.ts
+++ b/client/src/app/shared/video/video.service.ts
@@ -68,6 +68,7 @@ export class VideoService {
     const category = video.category || null
     const description = video.description || null
     const support = video.support || null
+    const scheduleUpdate = video.scheduleUpdate || null
 
     const body: VideoUpdate = {
       name: video.name,
@@ -84,7 +85,7 @@ export class VideoService {
       commentsEnabled: video.commentsEnabled,
       thumbnailfile: video.thumbnailfile,
       previewfile: video.previewfile,
-      scheduleUpdate: video.scheduleUpdate || undefined
+      scheduleUpdate
     }
 
     const data = objectToFormData(body)
diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts
index 79ca4699ff..ca800a9a8b 100644
--- a/server/controllers/api/videos/index.ts
+++ b/server/controllers/api/videos/index.ts
@@ -342,10 +342,12 @@ async function updateVideo (req: express.Request, res: express.Response) {
           updateAt: videoInfoToUpdate.scheduleUpdate.updateAt,
           privacy: videoInfoToUpdate.scheduleUpdate.privacy || null
         }, { transaction: t })
+      } else if (videoInfoToUpdate.scheduleUpdate === null) {
+        await ScheduleVideoUpdateModel.deleteByVideoId(videoInstanceUpdated.id, t)
       }
 
       const isNewVideo = wasPrivateVideo && videoInstanceUpdated.privacy !== VideoPrivacy.PRIVATE
-      await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo)
+      await federateVideoIfNeeded(videoInstanceUpdated, isNewVideo, t)
     })
 
     logger.info('Video with name %s and uuid %s updated.', videoInstance.name, videoInstance.uuid)
diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts
index da17b4a68d..5595edf179 100644
--- a/server/middlewares/validators/videos.ts
+++ b/server/middlewares/validators/videos.ts
@@ -94,6 +94,9 @@ const videosAddValidator = [
   body('channelId')
     .toInt()
     .custom(isIdValid).withMessage('Should have correct video channel id'),
+  body('scheduleUpdate')
+    .optional()
+    .customSanitizer(toValueOrNull),
   body('scheduleUpdate.updateAt')
     .optional()
     .custom(isDateValid).withMessage('Should have a valid schedule update date'),
@@ -199,6 +202,9 @@ const videosUpdateValidator = [
     .optional()
     .toInt()
     .custom(isIdValid).withMessage('Should have correct video channel id'),
+  body('scheduleUpdate')
+    .optional()
+    .customSanitizer(toValueOrNull),
   body('scheduleUpdate.updateAt')
     .optional()
     .custom(isDateValid).withMessage('Should have a valid schedule update date'),
diff --git a/server/models/video/schedule-video-update.ts b/server/models/video/schedule-video-update.ts
index 3cf5f6c99b..1e56562e19 100644
--- a/server/models/video/schedule-video-update.ts
+++ b/server/models/video/schedule-video-update.ts
@@ -83,6 +83,17 @@ export class ScheduleVideoUpdateModel extends Model<ScheduleVideoUpdateModel> {
     return ScheduleVideoUpdateModel.findAll(query)
   }
 
+  static deleteByVideoId (videoId: number, t: Transaction) {
+    const query = {
+      where: {
+        videoId
+      },
+      transaction: t
+    }
+
+    return ScheduleVideoUpdateModel.destroy(query)
+  }
+
   toFormattedJSON () {
     return {
       updateAt: this.updateAt,
-- 
GitLab