diff options
author | Chocobozzz <me@florianbigard.com> | 2021-05-11 11:15:29 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-05-11 11:32:31 +0200 |
commit | 7d9ba5c08999c6482f0bc5e0c09c6f55b7724090 (patch) | |
tree | 5f4ea1f06dcb781c9e61062522b53a28a401111f /server/models/user/user-video-history.ts | |
parent | c07902b9083ab5756436cd020bed5bdfa51028bf (diff) | |
download | PeerTube-7d9ba5c08999c6482f0bc5e0c09c6f55b7724090.tar.gz PeerTube-7d9ba5c08999c6482f0bc5e0c09c6f55b7724090.tar.zst PeerTube-7d9ba5c08999c6482f0bc5e0c09c6f55b7724090.zip |
Cleanup models directory organization
Diffstat (limited to 'server/models/user/user-video-history.ts')
-rw-r--r-- | server/models/user/user-video-history.ts | 100 |
1 files changed, 100 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..6be1d65ea --- /dev/null +++ b/server/models/user/user-video-history.ts | |||
@@ -0,0 +1,100 @@ | |||
1 | import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Model, Table, UpdatedAt } from 'sequelize-typescript' | ||
2 | import { VideoModel } from '../video/video' | ||
3 | import { UserModel } from './user' | ||
4 | import { DestroyOptions, Op, Transaction } from 'sequelize' | ||
5 | import { MUserAccountId, MUserId } from '@server/types/models' | ||
6 | |||
7 | @Table({ | ||
8 | tableName: 'userVideoHistory', | ||
9 | indexes: [ | ||
10 | { | ||
11 | fields: [ 'userId', 'videoId' ], | ||
12 | unique: true | ||
13 | }, | ||
14 | { | ||
15 | fields: [ 'userId' ] | ||
16 | }, | ||
17 | { | ||
18 | fields: [ 'videoId' ] | ||
19 | } | ||
20 | ] | ||
21 | }) | ||
22 | export class UserVideoHistoryModel extends Model { | ||
23 | @CreatedAt | ||
24 | createdAt: Date | ||
25 | |||
26 | @UpdatedAt | ||
27 | updatedAt: Date | ||
28 | |||
29 | @AllowNull(false) | ||
30 | @IsInt | ||
31 | @Column | ||
32 | currentTime: number | ||
33 | |||
34 | @ForeignKey(() => VideoModel) | ||
35 | @Column | ||
36 | videoId: number | ||
37 | |||
38 | @BelongsTo(() => VideoModel, { | ||
39 | foreignKey: { | ||
40 | allowNull: false | ||
41 | }, | ||
42 | onDelete: 'CASCADE' | ||
43 | }) | ||
44 | Video: VideoModel | ||
45 | |||
46 | @ForeignKey(() => UserModel) | ||
47 | @Column | ||
48 | userId: number | ||
49 | |||
50 | @BelongsTo(() => UserModel, { | ||
51 | foreignKey: { | ||
52 | allowNull: false | ||
53 | }, | ||
54 | onDelete: 'CASCADE' | ||
55 | }) | ||
56 | User: UserModel | ||
57 | |||
58 | static listForApi (user: MUserAccountId, start: number, count: number, search?: string) { | ||
59 | return VideoModel.listForApi({ | ||
60 | start, | ||
61 | count, | ||
62 | search, | ||
63 | sort: '-"userVideoHistory"."updatedAt"', | ||
64 | nsfw: null, // All | ||
65 | includeLocalVideos: true, | ||
66 | withFiles: false, | ||
67 | user, | ||
68 | historyOfUser: user | ||
69 | }) | ||
70 | } | ||
71 | |||
72 | static removeUserHistoryBefore (user: MUserId, beforeDate: string, t: Transaction) { | ||
73 | const query: DestroyOptions = { | ||
74 | where: { | ||
75 | userId: user.id | ||
76 | }, | ||
77 | transaction: t | ||
78 | } | ||
79 | |||
80 | if (beforeDate) { | ||
81 | query.where['updatedAt'] = { | ||
82 | [Op.lt]: beforeDate | ||
83 | } | ||
84 | } | ||
85 | |||
86 | return UserVideoHistoryModel.destroy(query) | ||
87 | } | ||
88 | |||
89 | static removeOldHistory (beforeDate: string) { | ||
90 | const query: DestroyOptions = { | ||
91 | where: { | ||
92 | updatedAt: { | ||
93 | [Op.lt]: beforeDate | ||
94 | } | ||
95 | } | ||
96 | } | ||
97 | |||
98 | return UserVideoHistoryModel.destroy(query) | ||
99 | } | ||
100 | } | ||