From 2ba92871319d7af63472c1380664a9f9eeb1c690 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 19 Mar 2019 16:23:02 +0100 Subject: Cleanup invalid rates/comments/shares --- server/models/account/account-video-rate.ts | 29 ++++++++++++++++++++++++++++- server/models/video/video-comment.ts | 14 ++++++++++++++ server/models/video/video-share.ts | 14 ++++++++++++++ server/models/video/video.ts | 2 +- 4 files changed, 57 insertions(+), 2 deletions(-) (limited to 'server/models') diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index 18762f0c5..e5d39582b 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts @@ -1,5 +1,5 @@ import { values } from 'lodash' -import { Transaction } from 'sequelize' +import { Transaction, Op } from 'sequelize' import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions' import { VideoRateType } from '../../../shared/models/videos' @@ -158,4 +158,31 @@ export class AccountVideoRateModel extends Model { return AccountVideoRateModel.findAndCountAll(query) } + + static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) { + return AccountVideoRateModel.sequelize.transaction(async t => { + const query = { + where: { + updatedAt: { + [Op.lt]: beforeUpdatedAt + }, + videoId, + type + }, + transaction: t + } + + const deleted = await AccountVideoRateModel.destroy(query) + + const options = { + transaction: t, + where: { + id: videoId + } + } + + if (type === 'like') await VideoModel.increment({ likes: -deleted }, options) + else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options) + }) + } } diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index 1163f9a0e..e733138c1 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -1,4 +1,5 @@ import * as Sequelize from 'sequelize' +import { Op } from 'sequelize' import { AllowNull, BeforeDestroy, @@ -453,6 +454,19 @@ export class VideoCommentModel extends Model { } } + static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) { + const query = { + where: { + updatedAt: { + [Op.lt]: beforeUpdatedAt + }, + videoId + } + } + + return VideoCommentModel.destroy(query) + } + getCommentStaticPath () { return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId() } diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts index 7df0ed18d..fb52b35d9 100644 --- a/server/models/video/video-share.ts +++ b/server/models/video/video-share.ts @@ -1,4 +1,5 @@ import * as Sequelize from 'sequelize' +import { Op } from 'sequelize' import * as Bluebird from 'bluebird' import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' @@ -200,4 +201,17 @@ export class VideoShareModel extends Model { return VideoShareModel.findAndCountAll(query) } + + static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) { + const query = { + where: { + updatedAt: { + [Op.lt]: beforeUpdatedAt + }, + videoId + } + } + + return VideoShareModel.destroy(query) + } } diff --git a/server/models/video/video.ts b/server/models/video/video.ts index fb037c21a..b0d92b674 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1547,7 +1547,7 @@ export class VideoModel extends Model { attributes: query.attributes, order: [ // Keep original order Sequelize.literal( - ids.map(id => `"VideoModel".id = ${id}`).join(', ') + ids.map(id => `"VideoModel".id = ${id} DESC`).join(', ') ) ] } -- cgit v1.2.3