]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/user/user-video-history.ts
Display all user history
[github/Chocobozzz/PeerTube.git] / server / models / user / user-video-history.ts
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'
8
9 @Table({
10 tableName: 'userVideoHistory',
11 indexes: [
12 {
13 fields: [ 'userId', 'videoId' ],
14 unique: true
15 },
16 {
17 fields: [ 'userId' ]
18 },
19 {
20 fields: [ 'videoId' ]
21 }
22 ]
23 })
24 export class UserVideoHistoryModel extends Model<Partial<AttributesOnly<UserVideoHistoryModel>>> {
25 @CreatedAt
26 createdAt: Date
27
28 @UpdatedAt
29 updatedAt: Date
30
31 @AllowNull(false)
32 @IsInt
33 @Column
34 currentTime: number
35
36 @ForeignKey(() => VideoModel)
37 @Column
38 videoId: number
39
40 @BelongsTo(() => VideoModel, {
41 foreignKey: {
42 allowNull: false
43 },
44 onDelete: 'CASCADE'
45 })
46 Video: VideoModel
47
48 @ForeignKey(() => UserModel)
49 @Column
50 userId: number
51
52 @BelongsTo(() => UserModel, {
53 foreignKey: {
54 allowNull: false
55 },
56 onDelete: 'CASCADE'
57 })
58 User: UserModel
59
60 static listForApi (user: MUserAccountId, start: number, count: number, search?: string) {
61 return VideoModel.listForApi({
62 start,
63 count,
64 search,
65 sort: '-"userVideoHistory"."updatedAt"',
66 nsfw: null, // All
67 displayOnlyForFollower: null,
68 user,
69 historyOfUser: user
70 })
71 }
72
73 static removeUserHistoryBefore (user: MUserId, beforeDate: string, t: Transaction) {
74 const query: DestroyOptions = {
75 where: {
76 userId: user.id
77 },
78 transaction: t
79 }
80
81 if (beforeDate) {
82 query.where['updatedAt'] = {
83 [Op.lt]: beforeDate
84 }
85 }
86
87 return UserVideoHistoryModel.destroy(query)
88 }
89
90 static removeOldHistory (beforeDate: string) {
91 const query: DestroyOptions = {
92 where: {
93 updatedAt: {
94 [Op.lt]: beforeDate
95 }
96 }
97 }
98
99 return UserVideoHistoryModel.destroy(query)
100 }
101 }