]>
Commit | Line | Data |
---|---|---|
8b9a525a | 1 | import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Model, Table, UpdatedAt } from 'sequelize-typescript' |
6e46de09 C |
2 | import { VideoModel } from '../video/video' |
3 | import { UserModel } from './user' | |
8b9a525a | 4 | import { Transaction, Op, DestroyOptions } from 'sequelize' |
6e46de09 C |
5 | |
6 | @Table({ | |
7 | tableName: 'userVideoHistory', | |
8 | indexes: [ | |
9 | { | |
10 | fields: [ 'userId', 'videoId' ], | |
11 | unique: true | |
12 | }, | |
13 | { | |
14 | fields: [ 'userId' ] | |
15 | }, | |
16 | { | |
17 | fields: [ 'videoId' ] | |
18 | } | |
19 | ] | |
20 | }) | |
21 | export class UserVideoHistoryModel extends Model<UserVideoHistoryModel> { | |
22 | @CreatedAt | |
23 | createdAt: Date | |
24 | ||
25 | @UpdatedAt | |
26 | updatedAt: Date | |
27 | ||
28 | @AllowNull(false) | |
29 | @IsInt | |
30 | @Column | |
31 | currentTime: number | |
32 | ||
33 | @ForeignKey(() => VideoModel) | |
34 | @Column | |
35 | videoId: number | |
36 | ||
37 | @BelongsTo(() => VideoModel, { | |
38 | foreignKey: { | |
39 | allowNull: false | |
40 | }, | |
41 | onDelete: 'CASCADE' | |
42 | }) | |
43 | Video: VideoModel | |
44 | ||
45 | @ForeignKey(() => UserModel) | |
46 | @Column | |
47 | userId: number | |
48 | ||
49 | @BelongsTo(() => UserModel, { | |
50 | foreignKey: { | |
51 | allowNull: false | |
52 | }, | |
53 | onDelete: 'CASCADE' | |
54 | }) | |
55 | User: UserModel | |
8b9a525a C |
56 | |
57 | static listForApi (user: UserModel, start: number, count: number) { | |
58 | return VideoModel.listForApi({ | |
59 | start, | |
60 | count, | |
61 | sort: '-UserVideoHistories.updatedAt', | |
62 | nsfw: null, // All | |
63 | includeLocalVideos: true, | |
64 | withFiles: false, | |
65 | user, | |
66 | historyOfUser: user | |
67 | }) | |
68 | } | |
69 | ||
70 | static removeHistoryBefore (user: UserModel, beforeDate: string, t: Transaction) { | |
71 | const query: DestroyOptions = { | |
72 | where: { | |
73 | userId: user.id | |
74 | }, | |
75 | transaction: t | |
76 | } | |
77 | ||
78 | if (beforeDate) { | |
79 | query.where.updatedAt = { | |
80 | [Op.lt]: beforeDate | |
81 | } | |
82 | } | |
83 | ||
84 | return UserVideoHistoryModel.destroy(query) | |
85 | } | |
6e46de09 | 86 | } |