1 import { DestroyOptions, Op, Transaction } from 'sequelize'
2 import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MUserAccountId, MUserId } from '@server/types/models'
4 import { AttributesOnly } from '@shared/typescript-utils'
5 import { VideoModel } from '../video/video'
6 import { UserModel } from './user'
7 import { getServerActor } from '../application/application'
10 tableName: 'userVideoHistory',
13 fields: [ 'userId', 'videoId' ],
24 export class UserVideoHistoryModel extends Model<Partial<AttributesOnly<UserVideoHistoryModel>>> {
36 @ForeignKey(() => VideoModel)
40 @BelongsTo(() => VideoModel, {
48 @ForeignKey(() => UserModel)
52 @BelongsTo(() => UserModel, {
60 static async listForApi (user: MUserAccountId, start: number, count: number, search?: string) {
61 const serverActor = await getServerActor()
63 return VideoModel.listForApi({
67 sort: '-"userVideoHistory"."updatedAt"',
69 displayOnlyForFollower: {
70 actorId: serverActor.id,
78 static removeUserHistoryBefore (user: MUserId, beforeDate: string, t: Transaction) {
79 const query: DestroyOptions = {
87 query.where['updatedAt'] = {
92 return UserVideoHistoryModel.destroy(query)
95 static removeOldHistory (beforeDate: string) {
96 const query: DestroyOptions = {
104 return UserVideoHistoryModel.destroy(query)