]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/user/user-video-history.ts
Upgrade minor dependencies
[github/Chocobozzz/PeerTube.git] / server / models / user / user-video-history.ts
1 import { DestroyOptions, Op, Transaction } from 'sequelize'
2 import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Model, Table, UpdatedAt } from 'sequelize-typescript'
3 import { MUserAccountId, MUserId } from '@server/types/models'
4 import { AttributesOnly } from '@shared/typescript-utils'
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 })
23 export 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 displayOnlyForFollower: null,
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 }