]>
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' | |
2760b454 | 7 | import { getServerActor } from '../application/application' |
6e46de09 C |
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 | }) | |
16c016e8 | 24 | export class UserVideoHistoryModel extends Model<Partial<AttributesOnly<UserVideoHistoryModel>>> { |
6e46de09 C |
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 | |
8b9a525a | 59 | |
2760b454 C |
60 | static async listForApi (user: MUserAccountId, start: number, count: number, search?: string) { |
61 | const serverActor = await getServerActor() | |
62 | ||
8b9a525a C |
63 | return VideoModel.listForApi({ |
64 | start, | |
65 | count, | |
d8b34ee5 | 66 | search, |
6b842050 | 67 | sort: '-"userVideoHistory"."updatedAt"', |
8b9a525a | 68 | nsfw: null, // All |
2760b454 C |
69 | displayOnlyForFollower: { |
70 | actorId: serverActor.id, | |
71 | orLocalVideos: true | |
72 | }, | |
8b9a525a C |
73 | user, |
74 | historyOfUser: user | |
75 | }) | |
76 | } | |
77 | ||
453e83ea | 78 | static removeUserHistoryBefore (user: MUserId, beforeDate: string, t: Transaction) { |
8b9a525a C |
79 | const query: DestroyOptions = { |
80 | where: { | |
81 | userId: user.id | |
82 | }, | |
83 | transaction: t | |
84 | } | |
85 | ||
86 | if (beforeDate) { | |
1735c825 | 87 | query.where['updatedAt'] = { |
8b9a525a C |
88 | [Op.lt]: beforeDate |
89 | } | |
90 | } | |
91 | ||
92 | return UserVideoHistoryModel.destroy(query) | |
93 | } | |
8f0bc73d C |
94 | |
95 | static removeOldHistory (beforeDate: string) { | |
96 | const query: DestroyOptions = { | |
97 | where: { | |
98 | updatedAt: { | |
99 | [Op.lt]: beforeDate | |
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | return UserVideoHistoryModel.destroy(query) | |
105 | } | |
6e46de09 | 106 | } |