diff options
author | Chocobozzz <me@florianbigard.com> | 2019-03-19 16:23:02 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-03-19 16:23:02 +0100 |
commit | 2ba92871319d7af63472c1380664a9f9eeb1c690 (patch) | |
tree | d593b2dfea29a8171b9f6afaaef076321f5edf71 /server/models | |
parent | d74d29ad9e35929491cf37223398d2535ab23de0 (diff) | |
download | PeerTube-2ba92871319d7af63472c1380664a9f9eeb1c690.tar.gz PeerTube-2ba92871319d7af63472c1380664a9f9eeb1c690.tar.zst PeerTube-2ba92871319d7af63472c1380664a9f9eeb1c690.zip |
Cleanup invalid rates/comments/shares
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/account/account-video-rate.ts | 29 | ||||
-rw-r--r-- | server/models/video/video-comment.ts | 14 | ||||
-rw-r--r-- | server/models/video/video-share.ts | 14 | ||||
-rw-r--r-- | server/models/video/video.ts | 2 |
4 files changed, 57 insertions, 2 deletions
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 @@ | |||
1 | import { values } from 'lodash' | 1 | import { values } from 'lodash' |
2 | import { Transaction } from 'sequelize' | 2 | import { Transaction, Op } 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 { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions' | 4 | import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions' |
5 | import { VideoRateType } from '../../../shared/models/videos' | 5 | import { VideoRateType } from '../../../shared/models/videos' |
@@ -158,4 +158,31 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> { | |||
158 | 158 | ||
159 | return AccountVideoRateModel.findAndCountAll(query) | 159 | return AccountVideoRateModel.findAndCountAll(query) |
160 | } | 160 | } |
161 | |||
162 | static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) { | ||
163 | return AccountVideoRateModel.sequelize.transaction(async t => { | ||
164 | const query = { | ||
165 | where: { | ||
166 | updatedAt: { | ||
167 | [Op.lt]: beforeUpdatedAt | ||
168 | }, | ||
169 | videoId, | ||
170 | type | ||
171 | }, | ||
172 | transaction: t | ||
173 | } | ||
174 | |||
175 | const deleted = await AccountVideoRateModel.destroy(query) | ||
176 | |||
177 | const options = { | ||
178 | transaction: t, | ||
179 | where: { | ||
180 | id: videoId | ||
181 | } | ||
182 | } | ||
183 | |||
184 | if (type === 'like') await VideoModel.increment({ likes: -deleted }, options) | ||
185 | else if (type === 'dislike') await VideoModel.increment({ dislikes: -deleted }, options) | ||
186 | }) | ||
187 | } | ||
161 | } | 188 | } |
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 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | import { Op } from 'sequelize' | ||
2 | import { | 3 | import { |
3 | AllowNull, | 4 | AllowNull, |
4 | BeforeDestroy, | 5 | BeforeDestroy, |
@@ -453,6 +454,19 @@ export class VideoCommentModel extends Model<VideoCommentModel> { | |||
453 | } | 454 | } |
454 | } | 455 | } |
455 | 456 | ||
457 | static cleanOldCommentsOf (videoId: number, beforeUpdatedAt: Date) { | ||
458 | const query = { | ||
459 | where: { | ||
460 | updatedAt: { | ||
461 | [Op.lt]: beforeUpdatedAt | ||
462 | }, | ||
463 | videoId | ||
464 | } | ||
465 | } | ||
466 | |||
467 | return VideoCommentModel.destroy(query) | ||
468 | } | ||
469 | |||
456 | getCommentStaticPath () { | 470 | getCommentStaticPath () { |
457 | return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId() | 471 | return this.Video.getWatchStaticPath() + ';threadId=' + this.getThreadId() |
458 | } | 472 | } |
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 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | import { Op } from 'sequelize' | ||
2 | import * as Bluebird from 'bluebird' | 3 | import * as Bluebird from 'bluebird' |
3 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' | 4 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' |
4 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | 5 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
@@ -200,4 +201,17 @@ export class VideoShareModel extends Model<VideoShareModel> { | |||
200 | 201 | ||
201 | return VideoShareModel.findAndCountAll(query) | 202 | return VideoShareModel.findAndCountAll(query) |
202 | } | 203 | } |
204 | |||
205 | static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) { | ||
206 | const query = { | ||
207 | where: { | ||
208 | updatedAt: { | ||
209 | [Op.lt]: beforeUpdatedAt | ||
210 | }, | ||
211 | videoId | ||
212 | } | ||
213 | } | ||
214 | |||
215 | return VideoShareModel.destroy(query) | ||
216 | } | ||
203 | } | 217 | } |
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<VideoModel> { | |||
1547 | attributes: query.attributes, | 1547 | attributes: query.attributes, |
1548 | order: [ // Keep original order | 1548 | order: [ // Keep original order |
1549 | Sequelize.literal( | 1549 | Sequelize.literal( |
1550 | ids.map(id => `"VideoModel".id = ${id}`).join(', ') | 1550 | ids.map(id => `"VideoModel".id = ${id} DESC`).join(', ') |
1551 | ) | 1551 | ) |
1552 | ] | 1552 | ] |
1553 | } | 1553 | } |