]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account-video-rate.ts
Fix offset/limit on some SQL queries :facepalm:
[github/Chocobozzz/PeerTube.git] / server / models / account / account-video-rate.ts
CommitLineData
65fcc311 1import { values } from 'lodash'
3fd3ab2d
C
2import { Transaction } from 'sequelize'
3import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
4import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions'
5import { VideoRateType } from '../../../shared/models/videos'
74889a71 6import { VIDEO_RATE_TYPES } from '../../initializers'
3fd3ab2d
C
7import { VideoModel } from '../video/video'
8import { AccountModel } from './account'
8fffe21a 9import { ActorModel } from '../activitypub/actor'
d38b8281 10
3fd3ab2d
C
11/*
12 Account rates per video.
13*/
14@Table({
15 tableName: 'accountVideoRate',
16 indexes: [
d38b8281 17 {
3fd3ab2d
C
18 fields: [ 'videoId', 'accountId' ],
19 unique: true
d38b8281 20 }
3fd3ab2d
C
21 ]
22})
23export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
d38b8281 24
3fd3ab2d
C
25 @AllowNull(false)
26 @Column(DataType.ENUM(values(VIDEO_RATE_TYPES)))
27 type: VideoRateType
e02643f3 28
3fd3ab2d
C
29 @CreatedAt
30 createdAt: Date
e02643f3 31
3fd3ab2d
C
32 @UpdatedAt
33 updatedAt: Date
d38b8281 34
3fd3ab2d
C
35 @ForeignKey(() => VideoModel)
36 @Column
37 videoId: number
d38b8281 38
3fd3ab2d 39 @BelongsTo(() => VideoModel, {
d38b8281 40 foreignKey: {
d38b8281
C
41 allowNull: false
42 },
43 onDelete: 'CASCADE'
44 })
3fd3ab2d 45 Video: VideoModel
d38b8281 46
3fd3ab2d
C
47 @ForeignKey(() => AccountModel)
48 @Column
49 accountId: number
50
51 @BelongsTo(() => AccountModel, {
d38b8281 52 foreignKey: {
d38b8281
C
53 allowNull: false
54 },
55 onDelete: 'CASCADE'
56 })
3fd3ab2d 57 Account: AccountModel
d38b8281 58
3fd3ab2d
C
59 static load (accountId: number, videoId: number, transaction: Transaction) {
60 const options: IFindOptions<AccountVideoRateModel> = {
61 where: {
62 accountId,
63 videoId
64 }
d38b8281 65 }
3fd3ab2d 66 if (transaction) options.transaction = transaction
d38b8281 67
3fd3ab2d
C
68 return AccountVideoRateModel.findOne(options)
69 }
8fffe21a
C
70
71 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
72 const query = {
9a4a9b6c
C
73 offset: start,
74 limit: count,
8fffe21a
C
75 where: {
76 videoId,
77 type: rateType
78 },
79 transaction: t,
80 include: [
81 {
82 attributes: [ 'actorId' ],
83 model: AccountModel.unscoped(),
84 required: true,
85 include: [
86 {
87 attributes: [ 'url' ],
88 model: ActorModel.unscoped(),
89 required: true
90 }
91 ]
92 }
93 ]
94 }
95
96 return AccountVideoRateModel.findAndCountAll(query)
97 }
d38b8281 98}