aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-03-19 16:23:02 +0100
committerChocobozzz <me@florianbigard.com>2019-03-19 16:23:02 +0100
commit2ba92871319d7af63472c1380664a9f9eeb1c690 (patch)
treed593b2dfea29a8171b9f6afaaef076321f5edf71 /server/models/account
parentd74d29ad9e35929491cf37223398d2535ab23de0 (diff)
downloadPeerTube-2ba92871319d7af63472c1380664a9f9eeb1c690.tar.gz
PeerTube-2ba92871319d7af63472c1380664a9f9eeb1c690.tar.zst
PeerTube-2ba92871319d7af63472c1380664a9f9eeb1c690.zip
Cleanup invalid rates/comments/shares
Diffstat (limited to 'server/models/account')
-rw-r--r--server/models/account/account-video-rate.ts29
1 files changed, 28 insertions, 1 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 @@
1import { values } from 'lodash' 1import { values } from 'lodash'
2import { Transaction } from 'sequelize' 2import { Transaction, Op } 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 { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions' 4import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions'
5import { VideoRateType } from '../../../shared/models/videos' 5import { 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}