aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account/account-video-rate.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/account/account-video-rate.ts')
-rw-r--r--server/models/account/account-video-rate.ts53
1 files changed, 29 insertions, 24 deletions
diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts
index 6955f45ee..d9c529491 100644
--- a/server/models/account/account-video-rate.ts
+++ b/server/models/account/account-video-rate.ts
@@ -1,22 +1,21 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import { FindOptions, Op, Transaction } from 'sequelize' 2import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize'
3import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' 3import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
4import { VideoRateType } from '../../../shared/models/videos'
5import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants'
6import { VideoModel } from '../video/video'
7import { AccountModel } from './account'
8import { ActorModel } from '../activitypub/actor'
9import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils'
10import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
11import { AccountVideoRate } from '../../../shared'
12import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
13import * as Bluebird from 'bluebird'
14import { 4import {
15 MAccountVideoRate, 5 MAccountVideoRate,
16 MAccountVideoRateAccountUrl, 6 MAccountVideoRateAccountUrl,
17 MAccountVideoRateAccountVideo, 7 MAccountVideoRateAccountVideo,
18 MAccountVideoRateFormattable 8 MAccountVideoRateFormattable
19} from '@server/types/models/video/video-rate' 9} from '@server/types/models/video/video-rate'
10import { AccountVideoRate } from '../../../shared'
11import { VideoRateType } from '../../../shared/models/videos'
12import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
13import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants'
14import { ActorModel } from '../activitypub/actor'
15import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils'
16import { VideoModel } from '../video/video'
17import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel'
18import { AccountModel } from './account'
20 19
21/* 20/*
22 Account rates per video. 21 Account rates per video.
@@ -43,7 +42,7 @@ import {
43 } 42 }
44 ] 43 ]
45}) 44})
46export class AccountVideoRateModel extends Model<AccountVideoRateModel> { 45export class AccountVideoRateModel extends Model {
47 46
48 @AllowNull(false) 47 @AllowNull(false)
49 @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES))) 48 @Column(DataType.ENUM(...values(VIDEO_RATE_TYPES)))
@@ -84,7 +83,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
84 }) 83 })
85 Account: AccountModel 84 Account: AccountModel
86 85
87 static load (accountId: number, videoId: number, transaction?: Transaction): Bluebird<MAccountVideoRate> { 86 static load (accountId: number, videoId: number, transaction?: Transaction): Promise<MAccountVideoRate> {
88 const options: FindOptions = { 87 const options: FindOptions = {
89 where: { 88 where: {
90 accountId, 89 accountId,
@@ -96,7 +95,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
96 return AccountVideoRateModel.findOne(options) 95 return AccountVideoRateModel.findOne(options)
97 } 96 }
98 97
99 static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Bluebird<MAccountVideoRate> { 98 static loadByAccountAndVideoOrUrl (accountId: number, videoId: number, url: string, t?: Transaction): Promise<MAccountVideoRate> {
100 const options: FindOptions = { 99 const options: FindOptions = {
101 where: { 100 where: {
102 [Op.or]: [ 101 [Op.or]: [
@@ -152,7 +151,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
152 accountName: string, 151 accountName: string,
153 videoId: number | string, 152 videoId: number | string,
154 t?: Transaction 153 t?: Transaction
155 ): Bluebird<MAccountVideoRateAccountVideo> { 154 ): Promise<MAccountVideoRateAccountVideo> {
156 const options: FindOptions = { 155 const options: FindOptions = {
157 where: { 156 where: {
158 videoId, 157 videoId,
@@ -240,17 +239,23 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> {
240 transaction: t 239 transaction: t
241 } 240 }
242 241
243 const deleted = await AccountVideoRateModel.destroy(query) 242 await AccountVideoRateModel.destroy(query)
244 243
245 const options = { 244 const field = type === 'like'
246 transaction: t, 245 ? 'likes'
247 where: { 246 : 'dislikes'
248 id: videoId 247
249 } 248 const rawQuery = `UPDATE "video" SET "${field}" = ` +
250 } 249 '(' +
250 'SELECT COUNT(id) FROM "accountVideoRate" WHERE "accountVideoRate"."videoId" = "video"."id" AND type = :rateType' +
251 ') ' +
252 'WHERE "video"."id" = :videoId'
251 253
252 if (type === 'like') await VideoModel.increment({ likes: -deleted }, options) 254 return AccountVideoRateModel.sequelize.query(rawQuery, {
253 else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options) 255 transaction: t,
256 replacements: { videoId, rateType: type },
257 type: QueryTypes.UPDATE
258 })
254 }) 259 })
255 } 260 }
256 261