]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/schedule-video-update.ts
Stricter models typing
[github/Chocobozzz/PeerTube.git] / server / models / video / schedule-video-update.ts
1 import { Op, Transaction } from 'sequelize'
2 import { AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MScheduleVideoUpdateFormattable, MScheduleVideoUpdateVideoAll } from '@server/types/models'
4 import { AttributesOnly } from '@shared/core-utils'
5 import { VideoPrivacy } from '../../../shared/models/videos'
6 import { ScopeNames as VideoScopeNames, 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 (t: Transaction) {
66 const query = {
67 where: {
68 updateAt: {
69 [Op.lte]: new Date()
70 }
71 },
72 include: [
73 {
74 model: VideoModel.scope(
75 [
76 VideoScopeNames.WITH_WEBTORRENT_FILES,
77 VideoScopeNames.WITH_STREAMING_PLAYLISTS,
78 VideoScopeNames.WITH_ACCOUNT_DETAILS,
79 VideoScopeNames.WITH_BLACKLISTED,
80 VideoScopeNames.WITH_THUMBNAILS,
81 VideoScopeNames.WITH_TAGS
82 ]
83 )
84 }
85 ],
86 transaction: t
87 }
88
89 return ScheduleVideoUpdateModel.findAll<MScheduleVideoUpdateVideoAll>(query)
90 }
91
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
103 toFormattedJSON (this: MScheduleVideoUpdateFormattable) {
104 return {
105 updateAt: this.updateAt,
106 privacy: this.privacy || undefined
107 }
108 }
109 }