]>
Commit | Line | Data |
---|---|---|
1735c825 | 1 | import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' |
2baea0c7 C |
2 | import { ScopeNames as VideoScopeNames, VideoModel } from './video' |
3 | import { VideoPrivacy } from '../../../shared/models/videos' | |
1735c825 | 4 | import { Op, Transaction } from 'sequelize' |
2baea0c7 C |
5 | |
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 | ||
20 | @AllowNull(false) | |
21 | @Default(null) | |
22 | @Column | |
23 | updateAt: Date | |
24 | ||
25 | @AllowNull(true) | |
26 | @Default(null) | |
27 | @Column | |
bbe0f064 | 28 | privacy: VideoPrivacy.PUBLIC | VideoPrivacy.UNLISTED |
2baea0c7 C |
29 | |
30 | @CreatedAt | |
31 | createdAt: Date | |
32 | ||
33 | @UpdatedAt | |
34 | updatedAt: Date | |
35 | ||
36 | @ForeignKey(() => VideoModel) | |
37 | @Column | |
38 | videoId: number | |
39 | ||
40 | @BelongsTo(() => VideoModel, { | |
41 | foreignKey: { | |
42 | allowNull: false | |
43 | }, | |
44 | onDelete: 'cascade' | |
45 | }) | |
46 | Video: VideoModel | |
47 | ||
bbe0f064 C |
48 | static areVideosToUpdate () { |
49 | const query = { | |
50 | logging: false, | |
51 | attributes: [ 'id' ], | |
52 | where: { | |
53 | updateAt: { | |
1735c825 | 54 | [Op.lte]: new Date() |
bbe0f064 C |
55 | } |
56 | } | |
57 | } | |
58 | ||
59 | return ScheduleVideoUpdateModel.findOne(query) | |
60 | .then(res => !!res) | |
61 | } | |
62 | ||
2baea0c7 C |
63 | static listVideosToUpdate (t: Transaction) { |
64 | const query = { | |
65 | where: { | |
66 | updateAt: { | |
1735c825 | 67 | [Op.lte]: new Date() |
2baea0c7 C |
68 | } |
69 | }, | |
70 | include: [ | |
71 | { | |
72 | model: VideoModel.scope( | |
73 | [ | |
74 | VideoScopeNames.WITH_FILES, | |
7ccddd7b | 75 | VideoScopeNames.WITH_ACCOUNT_DETAILS, |
cbe94af8 C |
76 | VideoScopeNames.WITH_BLACKLISTED, |
77 | VideoScopeNames.WITH_THUMBNAILS | |
2baea0c7 C |
78 | ] |
79 | ) | |
80 | } | |
81 | ], | |
82 | transaction: t | |
83 | } | |
84 | ||
85 | return ScheduleVideoUpdateModel.findAll(query) | |
86 | } | |
87 | ||
e94fc297 C |
88 | static deleteByVideoId (videoId: number, t: Transaction) { |
89 | const query = { | |
90 | where: { | |
91 | videoId | |
92 | }, | |
93 | transaction: t | |
94 | } | |
95 | ||
96 | return ScheduleVideoUpdateModel.destroy(query) | |
97 | } | |
98 | ||
bbe0f064 C |
99 | toFormattedJSON () { |
100 | return { | |
101 | updateAt: this.updateAt, | |
102 | privacy: this.privacy || undefined | |
103 | } | |
104 | } | |
2baea0c7 | 105 | } |