]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/user-video-history.ts
Cleaner warning of IP address leaking on embedded videos (#2034)
[github/Chocobozzz/PeerTube.git] / server / models / account / user-video-history.ts
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 { Transaction, Op, DestroyOptions } from 'sequelize'
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
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 removeUserHistoryBefore (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 }
86
87 static removeOldHistory (beforeDate: string) {
88 const query: DestroyOptions = {
89 where: {
90 updatedAt: {
91 [Op.lt]: beforeDate
92 }
93 }
94 }
95
96 return UserVideoHistoryModel.destroy(query)
97 }
98 }