]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account-video-rate.ts
Cleanup model types
[github/Chocobozzz/PeerTube.git] / server / models / account / account-video-rate.ts
CommitLineData
65fcc311 1import { values } from 'lodash'
1735c825 2import { FindOptions, Op, Transaction } from 'sequelize'
5c6d985f 3import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
3fd3ab2d 4import { VideoRateType } from '../../../shared/models/videos'
74dc3bca 5import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants'
3fd3ab2d
C
6import { VideoModel } from '../video/video'
7import { AccountModel } from './account'
8fffe21a 8import { ActorModel } from '../activitypub/actor'
6b9c966f 9import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils'
5c6d985f 10import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
c100a614 11import { AccountVideoRate } from '../../../shared'
bfbd9128 12import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
453e83ea
C
13import * as Bluebird from 'bluebird'
14import { MAccountVideoRate, MAccountVideoRateAccountUrl, MAccountVideoRateAccountVideo } from '@server/typings/models/video/video-rate'
d38b8281 15
3fd3ab2d
C
16/*
17 Account rates per video.
18*/
19@Table({
20 tableName: 'accountVideoRate',
21 indexes: [
d38b8281 22 {
3fd3ab2d
C
23 fields: [ 'videoId', 'accountId' ],
24 unique: true
8cd72bd3
C
25 },
26 {
27 fields: [ 'videoId' ]
28 },
29 {
30 fields: [ 'accountId' ]
31 },
32 {
33 fields: [ 'videoId', 'type' ]
5c6d985f
C
34 },
35 {
36 fields: [ 'url' ],
37 unique: true
d38b8281 38 }
3fd3ab2d
C
39 ]
40})
41export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
d38b8281 42
3fd3ab2d 43 @AllowNull(false)
1735c825 44 @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES)))
3fd3ab2d 45 type: VideoRateType
e02643f3 46
5c6d985f
C
47 @AllowNull(false)
48 @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
49 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max))
50 url: string
51
3fd3ab2d
C
52 @CreatedAt
53 createdAt: Date
e02643f3 54
3fd3ab2d
C
55 @UpdatedAt
56 updatedAt: Date
d38b8281 57
3fd3ab2d
C
58 @ForeignKey(() => VideoModel)
59 @Column
60 videoId: number
d38b8281 61
3fd3ab2d 62 @BelongsTo(() => VideoModel, {
d38b8281 63 foreignKey: {
d38b8281
C
64 allowNull: false
65 },
66 onDelete: 'CASCADE'
67 })
3fd3ab2d 68 Video: VideoModel
d38b8281 69
3fd3ab2d
C
70 @ForeignKey(() => AccountModel)
71 @Column
72 accountId: number
73
74 @BelongsTo(() => AccountModel, {
d38b8281 75 foreignKey: {
d38b8281
C
76 allowNull: false
77 },
78 onDelete: 'CASCADE'
79 })
3fd3ab2d 80 Account: AccountModel
d38b8281 81
453e83ea 82 static load (accountId: number, videoId: number, transaction?: Transaction): Bluebird<MAccountVideoRate> {
1735c825 83 const options: FindOptions = {
3fd3ab2d
C
84 where: {
85 accountId,
86 videoId
87 }
d38b8281 88 }
3fd3ab2d 89 if (transaction) options.transaction = transaction
d38b8281 90
3fd3ab2d
C
91 return AccountVideoRateModel.findOne(options)
92 }
8fffe21a 93
453e83ea 94 static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Bluebird<MAccountVideoRate> {
970ceac0
C
95 const options: FindOptions = {
96 where: {
97 [ Op.or]: [
98 {
99 accountId,
100 videoId
101 },
102 {
103 url
104 }
105 ]
106 }
107 }
453e83ea 108 if (t) options.transaction = t
970ceac0
C
109
110 return AccountVideoRateModel.findOne(options)
111 }
112
c100a614
YB
113 static listByAccountForApi (options: {
114 start: number,
115 count: number,
116 sort: string,
117 type?: string,
118 accountId: number
119 }) {
1735c825 120 const query: FindOptions = {
c100a614
YB
121 offset: options.start,
122 limit: options.count,
123 order: getSort(options.sort),
124 where: {
125 accountId: options.accountId
126 },
127 include: [
128 {
129 model: VideoModel,
130 required: true,
131 include: [
132 {
bfbd9128 133 model: VideoChannelModel.scope({ method: [VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }),
c100a614
YB
134 required: true
135 }
136 ]
137 }
138 ]
139 }
140 if (options.type) query.where['type'] = options.type
141
142 return AccountVideoRateModel.findAndCountAll(query)
143 }
144
453e83ea
C
145 static loadLocalAndPopulateVideo (
146 rateType: VideoRateType,
147 accountName: string,
148 videoId: number,
149 t?: Transaction
150 ): Bluebird<MAccountVideoRateAccountVideo> {
1735c825 151 const options: FindOptions = {
5c6d985f
C
152 where: {
153 videoId,
154 type: rateType
155 },
156 include: [
157 {
158 model: AccountModel.unscoped(),
159 required: true,
160 include: [
161 {
453e83ea 162 attributes: [ 'id', 'url', 'followersUrl', 'preferredUsername' ],
5c6d985f
C
163 model: ActorModel.unscoped(),
164 required: true,
165 where: {
166 preferredUsername: accountName
167 }
168 }
169 ]
170 },
171 {
172 model: VideoModel.unscoped(),
173 required: true
174 }
175 ]
176 }
453e83ea 177 if (t) options.transaction = t
5c6d985f
C
178
179 return AccountVideoRateModel.findOne(options)
180 }
181
182 static loadByUrl (url: string, transaction: Transaction) {
1735c825 183 const options: FindOptions = {
5c6d985f
C
184 where: {
185 url
186 }
187 }
188 if (transaction) options.transaction = transaction
189
190 return AccountVideoRateModel.findOne(options)
191 }
192
8fffe21a
C
193 static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) {
194 const query = {
9a4a9b6c
C
195 offset: start,
196 limit: count,
8fffe21a
C
197 where: {
198 videoId,
199 type: rateType
200 },
201 transaction: t,
202 include: [
203 {
204 attributes: [ 'actorId' ],
205 model: AccountModel.unscoped(),
206 required: true,
207 include: [
208 {
209 attributes: [ 'url' ],
210 model: ActorModel.unscoped(),
211 required: true
212 }
213 ]
214 }
215 ]
216 }
217
453e83ea 218 return AccountVideoRateModel.findAndCountAll<MAccountVideoRateAccountUrl>(query)
8fffe21a 219 }
2ba92871
C
220
221 static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) {
222 return AccountVideoRateModel.sequelize.transaction(async t => {
223 const query = {
224 where: {
225 updatedAt: {
226 [Op.lt]: beforeUpdatedAt
227 },
228 videoId,
6b9c966f
C
229 type,
230 accountId: {
231 [Op.notIn]: buildLocalAccountIdsIn()
970ceac0 232 }
6b9c966f 233 },
2ba92871
C
234 transaction: t
235 }
236
237 const deleted = await AccountVideoRateModel.destroy(query)
238
239 const options = {
240 transaction: t,
241 where: {
242 id: videoId
243 }
244 }
245
246 if (type === 'like') await VideoModel.increment({ likes: -deleted }, options)
247 else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options)
248 })
249 }
c100a614
YB
250
251 toFormattedJSON (): AccountVideoRate {
252 return {
253 video: this.Video.toFormattedJSON(),
254 rating: this.type
255 }
256 }
d38b8281 257}