]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/user-video-history.ts
Merge branch 'release/3.1.0' into develop
[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 { 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 }