2021-08-27 07:32:44 -05:00
|
|
|
import { literal, Op } from 'sequelize'
|
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table } from 'sequelize-typescript'
|
2021-12-16 11:04:16 -06:00
|
|
|
import { AttributesOnly } from '@shared/typescript-utils'
|
2022-03-24 07:36:47 -05:00
|
|
|
import { VideoModel } from '../video/video'
|
2018-08-29 09:26:25 -05:00
|
|
|
|
2022-04-05 08:15:09 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Aggregate views of all videos federated with our instance
|
|
|
|
* Mainly used by the trending/hot algorithms
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-08-29 09:26:25 -05:00
|
|
|
@Table({
|
|
|
|
tableName: 'videoView',
|
2019-04-26 02:16:43 -05:00
|
|
|
updatedAt: false,
|
2018-08-29 09:26:25 -05:00
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'videoId' ]
|
2018-09-04 08:25:02 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'startDate' ]
|
2018-08-29 09:26:25 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2021-05-12 07:09:04 -05:00
|
|
|
export class VideoViewModel extends Model<Partial<AttributesOnly<VideoViewModel>>> {
|
2018-08-29 09:26:25 -05:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
2021-08-27 07:32:44 -05:00
|
|
|
@Column(DataType.DATE)
|
2018-08-29 09:26:25 -05:00
|
|
|
startDate: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
2021-08-27 07:32:44 -05:00
|
|
|
@Column(DataType.DATE)
|
2018-08-29 09:26:25 -05: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 10:33:36 -05:00
|
|
|
static removeOldRemoteViewsHistory (beforeDate: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
startDate: {
|
2021-08-27 07:32:44 -05:00
|
|
|
[Op.lt]: beforeDate
|
2019-04-11 10:33:36 -05:00
|
|
|
},
|
|
|
|
videoId: {
|
2021-08-27 07:32:44 -05:00
|
|
|
[Op.in]: literal('(SELECT "id" FROM "video" WHERE "remote" IS TRUE)')
|
2019-04-11 10:33:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoViewModel.destroy(query)
|
|
|
|
}
|
2018-08-29 09:26:25 -05:00
|
|
|
}
|