aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/user/user-video-history.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/user/user-video-history.ts')
-rw-r--r--server/models/user/user-video-history.ts101
1 files changed, 101 insertions, 0 deletions
diff --git a/server/models/user/user-video-history.ts b/server/models/user/user-video-history.ts
new file mode 100644
index 000000000..e3dc4a062
--- /dev/null
+++ b/server/models/user/user-video-history.ts
@@ -0,0 +1,101 @@
1import { DestroyOptions, Op, Transaction } from 'sequelize'
2import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Model, Table, UpdatedAt } from 'sequelize-typescript'
3import { MUserAccountId, MUserId } from '@server/types/models'
4import { AttributesOnly } from '@shared/core-utils'
5import { VideoModel } from '../video/video'
6import { UserModel } from './user'
7
8@Table({
9 tableName: 'userVideoHistory',
10 indexes: [
11 {
12 fields: [ 'userId', 'videoId' ],
13 unique: true
14 },
15 {
16 fields: [ 'userId' ]
17 },
18 {
19 fields: [ 'videoId' ]
20 }
21 ]
22})
23export class UserVideoHistoryModel extends Model<Partial<AttributesOnly<UserVideoHistoryModel>>> {
24 @CreatedAt
25 createdAt: Date
26
27 @UpdatedAt
28 updatedAt: Date
29
30 @AllowNull(false)
31 @IsInt
32 @Column
33 currentTime: number
34
35 @ForeignKey(() => VideoModel)
36 @Column
37 videoId: number
38
39 @BelongsTo(() => VideoModel, {
40 foreignKey: {
41 allowNull: false
42 },
43 onDelete: 'CASCADE'
44 })
45 Video: VideoModel
46
47 @ForeignKey(() => UserModel)
48 @Column
49 userId: number
50
51 @BelongsTo(() => UserModel, {
52 foreignKey: {
53 allowNull: false
54 },
55 onDelete: 'CASCADE'
56 })
57 User: UserModel
58
59 static listForApi (user: MUserAccountId, start: number, count: number, search?: string) {
60 return VideoModel.listForApi({
61 start,
62 count,
63 search,
64 sort: '-"userVideoHistory"."updatedAt"',
65 nsfw: null, // All
66 includeLocalVideos: true,
67 withFiles: false,
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}