2021-05-12 07:09:04 -05:00
|
|
|
import { DestroyOptions, Op, Transaction } from 'sequelize'
|
2024-02-22 03:12:04 -06:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Table, UpdatedAt } from 'sequelize-typescript'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { ResultList } from '@peertube/peertube-models'
|
|
|
|
import { MUserAccountId, MUserId } from '@server/types/models/index.js'
|
|
|
|
import { VideoModel } from '../video/video.js'
|
|
|
|
import { UserModel } from './user.js'
|
2024-02-22 03:12:04 -06:00
|
|
|
import { SequelizeModel } from '../shared/sequelize-type.js'
|
2018-10-05 04:15:06 -05:00
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'userVideoHistory',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'userId', 'videoId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'userId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'videoId' ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2024-02-22 03:12:04 -06:00
|
|
|
export class UserVideoHistoryModel extends SequelizeModel<UserVideoHistoryModel> {
|
2018-10-05 04:15:06 -05:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@IsInt
|
|
|
|
@Column
|
|
|
|
currentTime: number
|
|
|
|
|
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
2023-07-31 07:34:36 -05:00
|
|
|
Video: Awaited<VideoModel>
|
2018-10-05 04:15:06 -05:00
|
|
|
|
|
|
|
@ForeignKey(() => UserModel)
|
|
|
|
@Column
|
|
|
|
userId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => UserModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
2023-07-31 07:34:36 -05:00
|
|
|
User: Awaited<UserModel>
|
2018-12-17 08:52:38 -06:00
|
|
|
|
2023-07-31 07:34:36 -05:00
|
|
|
// FIXME: have to specify the result type to not break peertube typings generation
|
|
|
|
static listForApi (user: MUserAccountId, start: number, count: number, search?: string): Promise<ResultList<VideoModel>> {
|
2018-12-17 08:52:38 -06:00
|
|
|
return VideoModel.listForApi({
|
|
|
|
start,
|
|
|
|
count,
|
2021-01-13 02:16:15 -06:00
|
|
|
search,
|
2020-03-09 08:44:44 -05:00
|
|
|
sort: '-"userVideoHistory"."updatedAt"',
|
2018-12-17 08:52:38 -06:00
|
|
|
nsfw: null, // All
|
2022-01-10 09:40:56 -06:00
|
|
|
displayOnlyForFollower: null,
|
2018-12-17 08:52:38 -06:00
|
|
|
user,
|
|
|
|
historyOfUser: user
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-18 04:23:41 -06:00
|
|
|
static removeUserHistoryElement (user: MUserId, videoId: number) {
|
|
|
|
const query: DestroyOptions = {
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
videoId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return UserVideoHistoryModel.destroy(query)
|
|
|
|
}
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
static removeUserHistoryBefore (user: MUserId, beforeDate: string, t: Transaction) {
|
2018-12-17 08:52:38 -06:00
|
|
|
const query: DestroyOptions = {
|
|
|
|
where: {
|
|
|
|
userId: user.id
|
|
|
|
},
|
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
|
|
|
if (beforeDate) {
|
2019-04-18 04:28:17 -05:00
|
|
|
query.where['updatedAt'] = {
|
2018-12-17 08:52:38 -06:00
|
|
|
[Op.lt]: beforeDate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return UserVideoHistoryModel.destroy(query)
|
|
|
|
}
|
2019-04-11 08:38:53 -05:00
|
|
|
|
|
|
|
static removeOldHistory (beforeDate: string) {
|
|
|
|
const query: DestroyOptions = {
|
|
|
|
where: {
|
|
|
|
updatedAt: {
|
|
|
|
[Op.lt]: beforeDate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return UserVideoHistoryModel.destroy(query)
|
|
|
|
}
|
2018-10-05 04:15:06 -05:00
|
|
|
}
|