]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/user/user-video-history.ts
Add video file size info in admin videos list
[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/core-utils'
5 import { VideoModel } from '../video/video'
6 import { UserModel } from './user'
7 import { getServerActor } from '../application/application'
8
9 @Table({
10 tableName: 'userVideoHistory',
11 indexes: [
12 {
13 fields: [ 'userId', 'videoId' ],
14 unique: true
15 },
16 {
17 fields: [ 'userId' ]
18 },
19 {
20 fields: [ 'videoId' ]
21 }
22 ]
23 })
24 export class UserVideoHistoryModel extends Model<Partial<AttributesOnly<UserVideoHistoryModel>>> {
25 @CreatedAt
26 createdAt: Date
27
28 @UpdatedAt
29 updatedAt: Date
30
31 @AllowNull(false)
32 @IsInt
33 @Column
34 currentTime: number
35
36 @ForeignKey(() => VideoModel)
37 @Column
38 videoId: number
39
40 @BelongsTo(() => VideoModel, {
41 foreignKey: {
42 allowNull: false
43 },
44 onDelete: 'CASCADE'
45 })
46 Video: VideoModel
47
48 @ForeignKey(() => UserModel)
49 @Column
50 userId: number
51
52 @BelongsTo(() => UserModel, {
53 foreignKey: {
54 allowNull: false
55 },
56 onDelete: 'CASCADE'
57 })
58 User: UserModel
59
60 static async listForApi (user: MUserAccountId, start: number, count: number, search?: string) {
61 const serverActor = await getServerActor()
62
63 return VideoModel.listForApi({
64 start,
65 count,
66 search,
67 sort: '-"userVideoHistory"."updatedAt"',
68 nsfw: null, // All
69 displayOnlyForFollower: {
70 actorId: serverActor.id,
71 orLocalVideos: true
72 },
73 user,
74 historyOfUser: user
75 })
76 }
77
78 static removeUserHistoryBefore (user: MUserId, beforeDate: string, t: Transaction) {
79 const query: DestroyOptions = {
80 where: {
81 userId: user.id
82 },
83 transaction: t
84 }
85
86 if (beforeDate) {
87 query.where['updatedAt'] = {
88 [Op.lt]: beforeDate
89 }
90 }
91
92 return UserVideoHistoryModel.destroy(query)
93 }
94
95 static removeOldHistory (beforeDate: string) {
96 const query: DestroyOptions = {
97 where: {
98 updatedAt: {
99 [Op.lt]: beforeDate
100 }
101 }
102 }
103
104 return UserVideoHistoryModel.destroy(query)
105 }
106 }