]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/user/user-video-history.ts
Fix update user password confirm message
[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 removeUserHistoryElement (user: MUserId, videoId: number) {
73 const query: DestroyOptions = {
74 where: {
75 userId: user.id,
76 videoId
77 }
78 }
79
80 return UserVideoHistoryModel.destroy(query)
81 }
82
83 static removeUserHistoryBefore (user: MUserId, beforeDate: string, t: Transaction) {
84 const query: DestroyOptions = {
85 where: {
86 userId: user.id
87 },
88 transaction: t
89 }
90
91 if (beforeDate) {
92 query.where['updatedAt'] = {
93 [Op.lt]: beforeDate
94 }
95 }
96
97 return UserVideoHistoryModel.destroy(query)
98 }
99
100 static removeOldHistory (beforeDate: string) {
101 const query: DestroyOptions = {
102 where: {
103 updatedAt: {
104 [Op.lt]: beforeDate
105 }
106 }
107 }
108
109 return UserVideoHistoryModel.destroy(query)
110 }
111 }