schedule-video-update.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
  2. import { ScopeNames as VideoScopeNames, VideoModel } from './video'
  3. import { VideoPrivacy } from '../../../shared/models/videos'
  4. import { Op, Transaction } from 'sequelize'
  5. @Table({
  6. tableName: 'scheduleVideoUpdate',
  7. indexes: [
  8. {
  9. fields: [ 'videoId' ],
  10. unique: true
  11. },
  12. {
  13. fields: [ 'updateAt' ]
  14. }
  15. ]
  16. })
  17. export class ScheduleVideoUpdateModel extends Model<ScheduleVideoUpdateModel> {
  18. @AllowNull(false)
  19. @Default(null)
  20. @Column
  21. updateAt: Date
  22. @AllowNull(true)
  23. @Default(null)
  24. @Column
  25. privacy: VideoPrivacy.PUBLIC | VideoPrivacy.UNLISTED
  26. @CreatedAt
  27. createdAt: Date
  28. @UpdatedAt
  29. updatedAt: Date
  30. @ForeignKey(() => VideoModel)
  31. @Column
  32. videoId: number
  33. @BelongsTo(() => VideoModel, {
  34. foreignKey: {
  35. allowNull: false
  36. },
  37. onDelete: 'cascade'
  38. })
  39. Video: VideoModel
  40. static areVideosToUpdate () {
  41. const query = {
  42. logging: false,
  43. attributes: [ 'id' ],
  44. where: {
  45. updateAt: {
  46. [Op.lte]: new Date()
  47. }
  48. }
  49. }
  50. return ScheduleVideoUpdateModel.findOne(query)
  51. .then(res => !!res)
  52. }
  53. static listVideosToUpdate (t: Transaction) {
  54. const query = {
  55. where: {
  56. updateAt: {
  57. [Op.lte]: new Date()
  58. }
  59. },
  60. include: [
  61. {
  62. model: VideoModel.scope(
  63. [
  64. VideoScopeNames.WITH_FILES,
  65. VideoScopeNames.WITH_ACCOUNT_DETAILS,
  66. VideoScopeNames.WITH_BLACKLISTED,
  67. VideoScopeNames.WITH_THUMBNAILS
  68. ]
  69. )
  70. }
  71. ],
  72. transaction: t
  73. }
  74. return ScheduleVideoUpdateModel.findAll(query)
  75. }
  76. static deleteByVideoId (videoId: number, t: Transaction) {
  77. const query = {
  78. where: {
  79. videoId
  80. },
  81. transaction: t
  82. }
  83. return ScheduleVideoUpdateModel.destroy(query)
  84. }
  85. toFormattedJSON () {
  86. return {
  87. updateAt: this.updateAt,
  88. privacy: this.privacy || undefined
  89. }
  90. }
  91. }