schedule-video-update.ts 2.3 KB

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