From bbe0f0645ca958d33a3f409b15166609733b663f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 15 Jun 2018 16:52:15 +0200 Subject: Add ability to schedule video publication --- server/models/video/schedule-video-update.ts | 23 ++++++++++++++++++- server/models/video/video.ts | 33 +++++++++++++++++++--------- 2 files changed, 45 insertions(+), 11 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/schedule-video-update.ts b/server/models/video/schedule-video-update.ts index d4e37beb5..3cf5f6c99 100644 --- a/server/models/video/schedule-video-update.ts +++ b/server/models/video/schedule-video-update.ts @@ -25,7 +25,7 @@ export class ScheduleVideoUpdateModel extends Model { @AllowNull(true) @Default(null) @Column - privacy: VideoPrivacy + privacy: VideoPrivacy.PUBLIC | VideoPrivacy.UNLISTED @CreatedAt createdAt: Date @@ -45,6 +45,21 @@ export class ScheduleVideoUpdateModel extends Model { }) Video: VideoModel + static areVideosToUpdate () { + const query = { + logging: false, + attributes: [ 'id' ], + where: { + updateAt: { + [Sequelize.Op.lte]: new Date() + } + } + } + + return ScheduleVideoUpdateModel.findOne(query) + .then(res => !!res) + } + static listVideosToUpdate (t: Transaction) { const query = { where: { @@ -68,4 +83,10 @@ export class ScheduleVideoUpdateModel extends Model { return ScheduleVideoUpdateModel.findAll(query) } + toFormattedJSON () { + return { + updateAt: this.updateAt, + privacy: this.privacy || undefined + } + } } diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 440f4d171..0041e4d38 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -97,7 +97,8 @@ export enum ScopeNames { AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS', WITH_TAGS = 'WITH_TAGS', - WITH_FILES = 'WITH_FILES' + WITH_FILES = 'WITH_FILES', + WITH_SCHEDULED_UPDATE = 'WITH_SCHEDULED_UPDATE' } @Scopes({ @@ -286,6 +287,14 @@ export enum ScopeNames { required: true } ] + }, + [ScopeNames.WITH_SCHEDULED_UPDATE]: { + include: [ + { + model: () => ScheduleVideoUpdateModel.unscoped(), + required: false + } + ] } }) @Table({ @@ -843,7 +852,7 @@ export class VideoModel extends Model { } return VideoModel - .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS ]) + .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_SCHEDULED_UPDATE ]) .findById(id, options) } @@ -869,7 +878,7 @@ export class VideoModel extends Model { } return VideoModel - .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS ]) + .scope([ ScopeNames.WITH_TAGS, ScopeNames.WITH_FILES, ScopeNames.WITH_ACCOUNT_DETAILS, ScopeNames.WITH_SCHEDULED_UPDATE ]) .findOne(options) } @@ -1022,9 +1031,9 @@ export class VideoModel extends Model { toFormattedJSON (options?: { additionalAttributes: { - state: boolean, - waitTranscoding: boolean, - scheduledUpdate: boolean + state?: boolean, + waitTranscoding?: boolean, + scheduledUpdate?: boolean } }): Video { const formattedAccount = this.VideoChannel.Account.toFormattedJSON() @@ -1084,18 +1093,18 @@ export class VideoModel extends Model { } if (options) { - if (options.additionalAttributes.state) { + if (options.additionalAttributes.state === true) { videoObject.state = { id: this.state, label: VideoModel.getStateLabel(this.state) } } - if (options.additionalAttributes.waitTranscoding) { + if (options.additionalAttributes.waitTranscoding === true) { videoObject.waitTranscoding = this.waitTranscoding } - if (options.additionalAttributes.scheduledUpdate && this.ScheduleVideoUpdate) { + if (options.additionalAttributes.scheduledUpdate === true && this.ScheduleVideoUpdate) { videoObject.scheduledUpdate = { updateAt: this.ScheduleVideoUpdate.updateAt, privacy: this.ScheduleVideoUpdate.privacy || undefined @@ -1107,7 +1116,11 @@ export class VideoModel extends Model { } toFormattedDetailsJSON (): VideoDetails { - const formattedJson = this.toFormattedJSON() + const formattedJson = this.toFormattedJSON({ + additionalAttributes: { + scheduledUpdate: true + } + }) const detailsJson = { support: this.support, -- cgit v1.2.3