1
0
Fork 0
peertube/server/models/video/video-view.ts

61 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-08-27 08:32:44 -04:00
import { literal, Op } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table } from 'sequelize-typescript'
import { AttributesOnly } from '@shared/typescript-utils'
2018-08-29 10:26:25 -04:00
import { VideoModel } from './video'
@Table({
tableName: 'videoView',
2019-04-26 03:16:43 -04:00
updatedAt: false,
2018-08-29 10:26:25 -04:00
indexes: [
{
fields: [ 'videoId' ]
},
{
fields: [ 'startDate' ]
2018-08-29 10:26:25 -04:00
}
]
})
2021-05-12 08:09:04 -04:00
export class VideoViewModel extends Model<Partial<AttributesOnly<VideoViewModel>>> {
2018-08-29 10:26:25 -04:00
@CreatedAt
createdAt: Date
@AllowNull(false)
2021-08-27 08:32:44 -04:00
@Column(DataType.DATE)
2018-08-29 10:26:25 -04:00
startDate: Date
@AllowNull(false)
2021-08-27 08:32:44 -04:00
@Column(DataType.DATE)
2018-08-29 10:26:25 -04:00
endDate: Date
@AllowNull(false)
@Column
views: number
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
foreignKey: {
allowNull: false
},
onDelete: 'CASCADE'
})
Video: VideoModel
2019-04-11 11:33:36 -04:00
static removeOldRemoteViewsHistory (beforeDate: string) {
const query = {
where: {
startDate: {
2021-08-27 08:32:44 -04:00
[Op.lt]: beforeDate
2019-04-11 11:33:36 -04:00
},
videoId: {
2021-08-27 08:32:44 -04:00
[Op.in]: literal('(SELECT "id" FROM "video" WHERE "remote" IS TRUE)')
2019-04-11 11:33:36 -04:00
}
}
}
return VideoViewModel.destroy(query)
}
2018-08-29 10:26:25 -04:00
}