]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account-video-rate.ts
Don't call watching endpoint if history is disabled
[github/Chocobozzz/PeerTube.git] / server / models / account / account-video-rate.ts
CommitLineData
65fcc311 1import { values } from 'lodash'
3fd3ab2d 2import { Transaction } from 'sequelize'
5c6d985f 3import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
3fd3ab2d
C
4import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions'
5import { VideoRateType } from '../../../shared/models/videos'
5c6d985f 6import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers'
3fd3ab2d
C
7import { VideoModel } from '../video/video'
8import { AccountModel } from './account'
8fffe21a 9import { ActorModel } from '../activitypub/actor'
5c6d985f
C
10import { throwIfNotValid } from '../utils'
11import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
d38b8281 12
3fd3ab2d
C
13/*
14 Account rates per video.
15*/
16@Table({
17 tableName: 'accountVideoRate',
18 indexes: [
d38b8281 19 {
3fd3ab2d
C
20 fields: [ 'videoId', 'accountId' ],
21 unique: true
8cd72bd3
C
22 },
23 {
24 fields: [ 'videoId' ]
25 },
26 {
27 fields: [ 'accountId' ]
28 },
29 {
30 fields: [ 'videoId', 'type' ]
5c6d985f
C
31 },
32 {
33 fields: [ 'url' ],
34 unique: true
d38b8281 35 }
3fd3ab2d
C
36 ]
37})
38export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
d38b8281 39
3fd3ab2d
C
40 @AllowNull(false)
41 @Column(DataType.ENUM(values(VIDEO_RATE_TYPES)))
42 type: VideoRateType
e02643f3 43
5c6d985f
C
44 @AllowNull(false)
45 @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
46 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
47 url: string
48
3fd3ab2d
C
49 @CreatedAt
50 createdAt: Date
e02643f3 51
3fd3ab2d
C
52 @UpdatedAt
53 updatedAt: Date
d38b8281 54
3fd3ab2d
C
55 @ForeignKey(() => VideoModel)
56 @Column
57 videoId: number
d38b8281 58
3fd3ab2d 59 @BelongsTo(() => VideoModel, {
d38b8281 60 foreignKey: {
d38b8281
C
61 allowNull: false
62 },
63 onDelete: 'CASCADE'
64 })
3fd3ab2d 65 Video: VideoModel
d38b8281 66
3fd3ab2d
C
67 @ForeignKey(() => AccountModel)
68 @Column
69 accountId: number
70
71 @BelongsTo(() => AccountModel, {
d38b8281 72 foreignKey: {
d38b8281
C
73 allowNull: false
74 },
75 onDelete: 'CASCADE'
76 })
3fd3ab2d 77 Account: AccountModel
d38b8281 78
5c6d985f 79 static load (accountId: number, videoId: number, transaction?: Transaction) {
3fd3ab2d
C
80 const options: IFindOptions<AccountVideoRateModel> = {
81 where: {
82 accountId,
83 videoId
84 }
d38b8281 85 }
3fd3ab2d 86 if (transaction) options.transaction = transaction
d38b8281 87
3fd3ab2d
C
88 return AccountVideoRateModel.findOne(options)
89 }
8fffe21a 90
5c6d985f
C
91 static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) {
92 const options: IFindOptions<AccountVideoRateModel> = {
93 where: {
94 videoId,
95 type: rateType
96 },
97 include: [
98 {
99 model: AccountModel.unscoped(),
100 required: true,
101 include: [
102 {
103 attributes: [ 'id', 'url', 'preferredUsername' ],
104 model: ActorModel.unscoped(),
105 required: true,
106 where: {
107 preferredUsername: accountName
108 }
109 }
110 ]
111 },
112 {
113 model: VideoModel.unscoped(),
114 required: true
115 }
116 ]
117 }
118 if (transaction) options.transaction = transaction
119
120 return AccountVideoRateModel.findOne(options)
121 }
122
123 static loadByUrl (url: string, transaction: Transaction) {
124 const options: IFindOptions<AccountVideoRateModel> = {
125 where: {
126 url
127 }
128 }
129 if (transaction) options.transaction = transaction
130
131 return AccountVideoRateModel.findOne(options)
132 }
133
8fffe21a
C
134 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
135 const query = {
9a4a9b6c
C
136 offset: start,
137 limit: count,
8fffe21a
C
138 where: {
139 videoId,
140 type: rateType
141 },
142 transaction: t,
143 include: [
144 {
145 attributes: [ 'actorId' ],
146 model: AccountModel.unscoped(),
147 required: true,
148 include: [
149 {
150 attributes: [ 'url' ],
151 model: ActorModel.unscoped(),
152 required: true
153 }
154 ]
155 }
156 ]
157 }
158
159 return AccountVideoRateModel.findAndCountAll(query)
160 }
d38b8281 161}