]>
Commit | Line | Data |
---|---|---|
16c016e8 | 1 | import { DestroyOptions, Op, Transaction } from 'sequelize' |
8b9a525a | 2 | import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Model, Table, UpdatedAt } from 'sequelize-typescript' |
16c016e8 C |
3 | import { MUserAccountId, MUserId } from '@server/types/models' |
4 | import { AttributesOnly } from '@shared/core-utils' | |
6e46de09 C |
5 | import { VideoModel } from '../video/video' |
6 | import { 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 | }) | |
16c016e8 | 23 | export class UserVideoHistoryModel extends Model<Partial<AttributesOnly<UserVideoHistoryModel>>> { |
6e46de09 C |
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 | |
8b9a525a | 58 | |
d8b34ee5 | 59 | static listForApi (user: MUserAccountId, start: number, count: number, search?: string) { |
8b9a525a C |
60 | return VideoModel.listForApi({ |
61 | start, | |
62 | count, | |
d8b34ee5 | 63 | search, |
6b842050 | 64 | sort: '-"userVideoHistory"."updatedAt"', |
8b9a525a C |
65 | nsfw: null, // All |
66 | includeLocalVideos: true, | |
67 | withFiles: false, | |
68 | user, | |
69 | historyOfUser: user | |
70 | }) | |
71 | } | |
72 | ||
453e83ea | 73 | static removeUserHistoryBefore (user: MUserId, beforeDate: string, t: Transaction) { |
8b9a525a C |
74 | const query: DestroyOptions = { |
75 | where: { | |
76 | userId: user.id | |
77 | }, | |
78 | transaction: t | |
79 | } | |
80 | ||
81 | if (beforeDate) { | |
1735c825 | 82 | query.where['updatedAt'] = { |
8b9a525a C |
83 | [Op.lt]: beforeDate |
84 | } | |
85 | } | |
86 | ||
87 | return UserVideoHistoryModel.destroy(query) | |
88 | } | |
8f0bc73d C |
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 | } | |
6e46de09 | 101 | } |