]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/schedule-video-update.ts
Move typescript utils in its own directory
[github/Chocobozzz/PeerTube.git] / server / models / video / schedule-video-update.ts
CommitLineData
1735c825 1import { Op, Transaction } from 'sequelize'
16c016e8 2import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
fd6a74a8 3import { MScheduleVideoUpdateFormattable, MScheduleVideoUpdate } from '@server/types/models'
6b5f72be 4import { AttributesOnly } from '@shared/typescript-utils'
16c016e8 5import { VideoPrivacy } from '../../../shared/models/videos'
fd6a74a8 6import { VideoModel } from './video'
2baea0c7
C
7
8@Table({
9 tableName: 'scheduleVideoUpdate',
10 indexes: [
11 {
12 fields: [ 'videoId' ],
13 unique: true
14 },
15 {
16 fields: [ 'updateAt' ]
17 }
18 ]
19})
16c016e8 20export class ScheduleVideoUpdateModel extends Model<Partial<AttributesOnly<ScheduleVideoUpdateModel>>> {
2baea0c7
C
21
22 @AllowNull(false)
23 @Default(null)
24 @Column
25 updateAt: Date
26
27 @AllowNull(true)
28 @Default(null)
29 @Column
22a73cb8 30 privacy: VideoPrivacy.PUBLIC | VideoPrivacy.UNLISTED | VideoPrivacy.INTERNAL
2baea0c7
C
31
32 @CreatedAt
33 createdAt: Date
34
35 @UpdatedAt
36 updatedAt: Date
37
38 @ForeignKey(() => VideoModel)
39 @Column
40 videoId: number
41
42 @BelongsTo(() => VideoModel, {
43 foreignKey: {
44 allowNull: false
45 },
46 onDelete: 'cascade'
47 })
48 Video: VideoModel
49
bbe0f064
C
50 static areVideosToUpdate () {
51 const query = {
52 logging: false,
53 attributes: [ 'id' ],
54 where: {
55 updateAt: {
1735c825 56 [Op.lte]: new Date()
bbe0f064
C
57 }
58 }
59 }
60
61 return ScheduleVideoUpdateModel.findOne(query)
62 .then(res => !!res)
63 }
64
fd6a74a8 65 static listVideosToUpdate (transaction?: Transaction) {
2baea0c7
C
66 const query = {
67 where: {
68 updateAt: {
1735c825 69 [Op.lte]: new Date()
2baea0c7
C
70 }
71 },
fd6a74a8 72 transaction
2baea0c7
C
73 }
74
fd6a74a8 75 return ScheduleVideoUpdateModel.findAll<MScheduleVideoUpdate>(query)
2baea0c7
C
76 }
77
e94fc297
C
78 static deleteByVideoId (videoId: number, t: Transaction) {
79 const query = {
80 where: {
81 videoId
82 },
83 transaction: t
84 }
85
86 return ScheduleVideoUpdateModel.destroy(query)
87 }
88
1ca9f7c3 89 toFormattedJSON (this: MScheduleVideoUpdateFormattable) {
bbe0f064
C
90 return {
91 updateAt: this.updateAt,
92 privacy: this.privacy || undefined
93 }
94 }
2baea0c7 95}