]>
Commit | Line | Data |
---|---|---|
1735c825 | 1 | import { Op, Transaction } from 'sequelize' |
16c016e8 | 2 | import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' |
26d6bf65 | 3 | import { MScheduleVideoUpdateFormattable, MScheduleVideoUpdateVideoAll } from '@server/types/models' |
16c016e8 C |
4 | import { AttributesOnly } from '@shared/core-utils' |
5 | import { VideoPrivacy } from '../../../shared/models/videos' | |
6 | import { ScopeNames as VideoScopeNames, 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 | 20 | export 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 | ||
2baea0c7 C |
65 | static listVideosToUpdate (t: Transaction) { |
66 | const query = { | |
67 | where: { | |
68 | updateAt: { | |
1735c825 | 69 | [Op.lte]: new Date() |
2baea0c7 C |
70 | } |
71 | }, | |
72 | include: [ | |
73 | { | |
74 | model: VideoModel.scope( | |
75 | [ | |
d7a25329 C |
76 | VideoScopeNames.WITH_WEBTORRENT_FILES, |
77 | VideoScopeNames.WITH_STREAMING_PLAYLISTS, | |
7ccddd7b | 78 | VideoScopeNames.WITH_ACCOUNT_DETAILS, |
cbe94af8 | 79 | VideoScopeNames.WITH_BLACKLISTED, |
d7a25329 C |
80 | VideoScopeNames.WITH_THUMBNAILS, |
81 | VideoScopeNames.WITH_TAGS | |
2baea0c7 C |
82 | ] |
83 | ) | |
84 | } | |
85 | ], | |
86 | transaction: t | |
87 | } | |
88 | ||
d7a25329 | 89 | return ScheduleVideoUpdateModel.findAll<MScheduleVideoUpdateVideoAll>(query) |
2baea0c7 C |
90 | } |
91 | ||
e94fc297 C |
92 | static deleteByVideoId (videoId: number, t: Transaction) { |
93 | const query = { | |
94 | where: { | |
95 | videoId | |
96 | }, | |
97 | transaction: t | |
98 | } | |
99 | ||
100 | return ScheduleVideoUpdateModel.destroy(query) | |
101 | } | |
102 | ||
1ca9f7c3 | 103 | toFormattedJSON (this: MScheduleVideoUpdateFormattable) { |
bbe0f064 C |
104 | return { |
105 | updateAt: this.updateAt, | |
106 | privacy: this.privacy || undefined | |
107 | } | |
108 | } | |
2baea0c7 | 109 | } |