diff options
Diffstat (limited to 'server/models/account/account-video-rate.ts')
-rw-r--r-- | server/models/account/account-video-rate.ts | 53 |
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 @@ | |||
1 | import { values } from 'lodash' | 1 | import { values } from 'lodash' |
2 | import { FindOptions, Op, Transaction } from 'sequelize' | 2 | import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize' |
3 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' | 3 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
4 | import { VideoRateType } from '../../../shared/models/videos' | ||
5 | import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants' | ||
6 | import { VideoModel } from '../video/video' | ||
7 | import { AccountModel } from './account' | ||
8 | import { ActorModel } from '../activitypub/actor' | ||
9 | import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils' | ||
10 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | ||
11 | import { AccountVideoRate } from '../../../shared' | ||
12 | import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel' | ||
13 | import * as Bluebird from 'bluebird' | ||
14 | import { | 4 | import { |
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' |
10 | import { AccountVideoRate } from '../../../shared' | ||
11 | import { VideoRateType } from '../../../shared/models/videos' | ||
12 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | ||
13 | import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers/constants' | ||
14 | import { ActorModel } from '../activitypub/actor' | ||
15 | import { buildLocalAccountIdsIn, getSort, throwIfNotValid } from '../utils' | ||
16 | import { VideoModel } from '../video/video' | ||
17 | import { ScopeNames as VideoChannelScopeNames, SummaryOptions, VideoChannelModel } from '../video/video-channel' | ||
18 | import { 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 | }) |
46 | export class AccountVideoRateModel extends Model<AccountVideoRateModel> { | 45 | export 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 | ||