]>
Commit | Line | Data |
---|---|---|
1 | import { Op, Transaction } from 'sequelize' | |
2 | import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' | |
3 | import { MScheduleVideoUpdateFormattable, MScheduleVideoUpdate } from '@server/types/models' | |
4 | import { AttributesOnly } from '@shared/core-utils' | |
5 | import { VideoPrivacy } from '../../../shared/models/videos' | |
6 | import { VideoModel } from './video' | |
7 | ||
8 | @Table({ | |
9 | tableName: 'scheduleVideoUpdate', | |
10 | indexes: [ | |
11 | { | |
12 | fields: [ 'videoId' ], | |
13 | unique: true | |
14 | }, | |
15 | { | |
16 | fields: [ 'updateAt' ] | |
17 | } | |
18 | ] | |
19 | }) | |
20 | export class ScheduleVideoUpdateModel extends Model<Partial<AttributesOnly<ScheduleVideoUpdateModel>>> { | |
21 | ||
22 | @AllowNull(false) | |
23 | @Default(null) | |
24 | @Column | |
25 | updateAt: Date | |
26 | ||
27 | @AllowNull(true) | |
28 | @Default(null) | |
29 | @Column | |
30 | privacy: VideoPrivacy.PUBLIC | VideoPrivacy.UNLISTED | VideoPrivacy.INTERNAL | |
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 | ||
50 | static areVideosToUpdate () { | |
51 | const query = { | |
52 | logging: false, | |
53 | attributes: [ 'id' ], | |
54 | where: { | |
55 | updateAt: { | |
56 | [Op.lte]: new Date() | |
57 | } | |
58 | } | |
59 | } | |
60 | ||
61 | return ScheduleVideoUpdateModel.findOne(query) | |
62 | .then(res => !!res) | |
63 | } | |
64 | ||
65 | static listVideosToUpdate (transaction?: Transaction) { | |
66 | const query = { | |
67 | where: { | |
68 | updateAt: { | |
69 | [Op.lte]: new Date() | |
70 | } | |
71 | }, | |
72 | transaction | |
73 | } | |
74 | ||
75 | return ScheduleVideoUpdateModel.findAll<MScheduleVideoUpdate>(query) | |
76 | } | |
77 | ||
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 | ||
89 | toFormattedJSON (this: MScheduleVideoUpdateFormattable) { | |
90 | return { | |
91 | updateAt: this.updateAt, | |
92 | privacy: this.privacy || undefined | |
93 | } | |
94 | } | |
95 | } |