2021-05-12 14:09:04 +02:00
|
|
|
import { DestroyOptions, Op, Transaction } from 'sequelize'
|
2024-02-22 10:12:04 +01:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Table, UpdatedAt } from 'sequelize-typescript'
|
2023-07-31 14:34:36 +02: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 10:12:04 +01:00
|
|
|
import { SequelizeModel } from '../shared/sequelize-type.js'
|
2024-02-28 16:22:51 +01:00
|
|
|
import { USER_EXPORT_MAX_ITEMS } from '@server/initializers/constants.js'
|
|
|
|
import { getSort } from '../shared/sort.js'
|
2018-10-05 11:15:06 +02:00
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'userVideoHistory',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'userId', 'videoId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'userId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'videoId' ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2024-02-22 10:12:04 +01:00
|
|
|
export class UserVideoHistoryModel extends SequelizeModel<UserVideoHistoryModel> {
|
2018-10-05 11:15:06 +02: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 14:34:36 +02:00
|
|
|
Video: Awaited<VideoModel>
|
2018-10-05 11:15:06 +02:00
|
|
|
|
|
|
|
@ForeignKey(() => UserModel)
|
|
|
|
@Column
|
|
|
|
userId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => UserModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
2023-07-31 14:34:36 +02:00
|
|
|
User: Awaited<UserModel>
|
2018-12-17 15:52:38 +01:00
|
|
|
|
2023-07-31 14:34:36 +02: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 15:52:38 +01:00
|
|
|
return VideoModel.listForApi({
|
|
|
|
start,
|
|
|
|
count,
|
2021-01-13 09:16:15 +01:00
|
|
|
search,
|
2020-03-09 14:44:44 +01:00
|
|
|
sort: '-"userVideoHistory"."updatedAt"',
|
2018-12-17 15:52:38 +01:00
|
|
|
nsfw: null, // All
|
2022-01-10 16:40:56 +01:00
|
|
|
displayOnlyForFollower: null,
|
2018-12-17 15:52:38 +01:00
|
|
|
user,
|
|
|
|
historyOfUser: user
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-28 16:22:51 +01:00
|
|
|
static async listForExport (user: MUserId) {
|
|
|
|
const rows = await UserVideoHistoryModel.findAll({
|
|
|
|
attributes: [ 'createdAt', 'updatedAt', 'currentTime' ],
|
|
|
|
where: {
|
|
|
|
userId: user.id
|
|
|
|
},
|
|
|
|
limit: USER_EXPORT_MAX_ITEMS,
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
attributes: [ 'url' ],
|
|
|
|
model: VideoModel.unscoped(),
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
order: getSort('updatedAt')
|
|
|
|
})
|
|
|
|
|
|
|
|
return rows.map(r => ({ createdAt: r.createdAt, updatedAt: r.updatedAt, currentTime: r.currentTime, videoUrl: r.Video.url }))
|
|
|
|
}
|
|
|
|
|
2022-01-18 11:23:41 +01:00
|
|
|
static removeUserHistoryElement (user: MUserId, videoId: number) {
|
|
|
|
const query: DestroyOptions = {
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
|
|
|
videoId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return UserVideoHistoryModel.destroy(query)
|
|
|
|
}
|
|
|
|
|
2019-08-15 11:53:26 +02:00
|
|
|
static removeUserHistoryBefore (user: MUserId, beforeDate: string, t: Transaction) {
|
2018-12-17 15:52:38 +01:00
|
|
|
const query: DestroyOptions = {
|
|
|
|
where: {
|
|
|
|
userId: user.id
|
|
|
|
},
|
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
|
|
|
if (beforeDate) {
|
2019-04-18 11:28:17 +02:00
|
|
|
query.where['updatedAt'] = {
|
2018-12-17 15:52:38 +01:00
|
|
|
[Op.lt]: beforeDate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return UserVideoHistoryModel.destroy(query)
|
|
|
|
}
|
2019-04-11 15:38:53 +02:00
|
|
|
|
|
|
|
static removeOldHistory (beforeDate: string) {
|
|
|
|
const query: DestroyOptions = {
|
|
|
|
where: {
|
|
|
|
updatedAt: {
|
|
|
|
[Op.lt]: beforeDate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return UserVideoHistoryModel.destroy(query)
|
|
|
|
}
|
2018-10-05 11:15:06 +02:00
|
|
|
}
|