From 2baea0c77cc765f7cbca9c9a2f4272268892a35c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Jun 2018 18:06:56 +0200 Subject: Add ability for uploaders to schedule video update --- server/models/video/schedule-video-update.ts | 71 ++++++++++++++++++++++++++++ server/models/video/video.ts | 37 +++++++++++++-- 2 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 server/models/video/schedule-video-update.ts (limited to 'server/models/video') diff --git a/server/models/video/schedule-video-update.ts b/server/models/video/schedule-video-update.ts new file mode 100644 index 000000000..d4e37beb5 --- /dev/null +++ b/server/models/video/schedule-video-update.ts @@ -0,0 +1,71 @@ +import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Sequelize, Table, UpdatedAt } from 'sequelize-typescript' +import { ScopeNames as VideoScopeNames, VideoModel } from './video' +import { VideoPrivacy } from '../../../shared/models/videos' +import { Transaction } from 'sequelize' + +@Table({ + tableName: 'scheduleVideoUpdate', + indexes: [ + { + fields: [ 'videoId' ], + unique: true + }, + { + fields: [ 'updateAt' ] + } + ] +}) +export class ScheduleVideoUpdateModel extends Model { + + @AllowNull(false) + @Default(null) + @Column + updateAt: Date + + @AllowNull(true) + @Default(null) + @Column + privacy: VideoPrivacy + + @CreatedAt + createdAt: Date + + @UpdatedAt + updatedAt: Date + + @ForeignKey(() => VideoModel) + @Column + videoId: number + + @BelongsTo(() => VideoModel, { + foreignKey: { + allowNull: false + }, + onDelete: 'cascade' + }) + Video: VideoModel + + static listVideosToUpdate (t: Transaction) { + const query = { + where: { + updateAt: { + [Sequelize.Op.lte]: new Date() + } + }, + include: [ + { + model: VideoModel.scope( + [ + VideoScopeNames.WITH_FILES, + VideoScopeNames.WITH_ACCOUNT_DETAILS + ] + ) + } + ], + transaction: t + } + + return ScheduleVideoUpdateModel.findAll(query) + } + +} diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 59c378efa..440f4d171 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -15,6 +15,7 @@ import { Default, ForeignKey, HasMany, + HasOne, IFindOptions, Is, IsInt, @@ -47,7 +48,8 @@ import { isVideoLanguageValid, isVideoLicenceValid, isVideoNameValid, - isVideoPrivacyValid, isVideoStateValid, + isVideoPrivacyValid, + isVideoStateValid, isVideoSupportValid } from '../../helpers/custom-validators/videos' import { generateImageFromVideoFile, getVideoFileResolution, transcode } from '../../helpers/ffmpeg-utils' @@ -66,7 +68,8 @@ import { VIDEO_EXT_MIMETYPE, VIDEO_LANGUAGES, VIDEO_LICENCES, - VIDEO_PRIVACIES, VIDEO_STATES + VIDEO_PRIVACIES, + VIDEO_STATES } from '../../initializers' import { getVideoCommentsActivityPubUrl, @@ -88,8 +91,9 @@ import { VideoCommentModel } from './video-comment' import { VideoFileModel } from './video-file' import { VideoShareModel } from './video-share' import { VideoTagModel } from './video-tag' +import { ScheduleVideoUpdateModel } from './schedule-video-update' -enum ScopeNames { +export enum ScopeNames { AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', WITH_ACCOUNT_DETAILS = 'WITH_ACCOUNT_DETAILS', WITH_TAGS = 'WITH_TAGS', @@ -495,6 +499,15 @@ export class VideoModel extends Model { }) VideoComments: VideoCommentModel[] + @HasOne(() => ScheduleVideoUpdateModel, { + foreignKey: { + name: 'videoId', + allowNull: false + }, + onDelete: 'cascade' + }) + ScheduleVideoUpdate: ScheduleVideoUpdateModel + @BeforeDestroy static async sendDelete (instance: VideoModel, options) { if (instance.isOwned()) { @@ -673,6 +686,10 @@ export class VideoModel extends Model { required: true } ] + }, + { + model: ScheduleVideoUpdateModel, + required: false } ] } @@ -1006,7 +1023,8 @@ export class VideoModel extends Model { toFormattedJSON (options?: { additionalAttributes: { state: boolean, - waitTranscoding: boolean + waitTranscoding: boolean, + scheduledUpdate: boolean } }): Video { const formattedAccount = this.VideoChannel.Account.toFormattedJSON() @@ -1073,7 +1091,16 @@ export class VideoModel extends Model { } } - if (options.additionalAttributes.waitTranscoding) videoObject.waitTranscoding = this.waitTranscoding + if (options.additionalAttributes.waitTranscoding) { + videoObject.waitTranscoding = this.waitTranscoding + } + + if (options.additionalAttributes.scheduledUpdate && this.ScheduleVideoUpdate) { + videoObject.scheduledUpdate = { + updateAt: this.ScheduleVideoUpdate.updateAt, + privacy: this.ScheduleVideoUpdate.privacy || undefined + } + } } return videoObject -- cgit v1.2.3