]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/schedule-video-update.ts
Stronger model typings
[github/Chocobozzz/PeerTube.git] / server / models / video / schedule-video-update.ts
index d4e37beb5ce245eaccb95e7a3923baa9257b8a6f..603d556924c7e4f59845963175b4662288758502 100644 (file)
@@ -1,7 +1,7 @@
-import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Sequelize, Table, UpdatedAt } from 'sequelize-typescript'
+import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
 import { ScopeNames as VideoScopeNames, VideoModel } from './video'
 import { VideoPrivacy } from '../../../shared/models/videos'
-import { Transaction } from 'sequelize'
+import { Op, Transaction } from 'sequelize'
 
 @Table({
   tableName: 'scheduleVideoUpdate',
@@ -25,7 +25,7 @@ export class ScheduleVideoUpdateModel extends Model<ScheduleVideoUpdateModel> {
   @AllowNull(true)
   @Default(null)
   @Column
-  privacy: VideoPrivacy
+  privacy: VideoPrivacy.PUBLIC | VideoPrivacy.UNLISTED
 
   @CreatedAt
   createdAt: Date
@@ -45,11 +45,26 @@ export class ScheduleVideoUpdateModel extends Model<ScheduleVideoUpdateModel> {
   })
   Video: VideoModel
 
+  static areVideosToUpdate () {
+    const query = {
+      logging: false,
+      attributes: [ 'id' ],
+      where: {
+        updateAt: {
+          [Op.lte]: new Date()
+        }
+      }
+    }
+
+    return ScheduleVideoUpdateModel.findOne(query)
+      .then(res => !!res)
+  }
+
   static listVideosToUpdate (t: Transaction) {
     const query = {
       where: {
         updateAt: {
-          [Sequelize.Op.lte]: new Date()
+          [Op.lte]: new Date()
         }
       },
       include: [
@@ -57,7 +72,9 @@ export class ScheduleVideoUpdateModel extends Model<ScheduleVideoUpdateModel> {
           model: VideoModel.scope(
             [
               VideoScopeNames.WITH_FILES,
-              VideoScopeNames.WITH_ACCOUNT_DETAILS
+              VideoScopeNames.WITH_ACCOUNT_DETAILS,
+              VideoScopeNames.WITH_BLACKLISTED,
+              VideoScopeNames.WITH_THUMBNAILS
             ]
           )
         }
@@ -68,4 +85,21 @@ 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,
+      privacy: this.privacy || undefined
+    }
+  }
 }