]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-dislike.ts
Fix like/dislike federation
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-dislike.ts
index bfd69e07a2dd4223e5782a511506f542571ecb39..f06269f8b13f68f41cb14f2392d99e846879569c 100644 (file)
@@ -29,20 +29,26 @@ async function processDislike (activity: ActivityCreate | ActivityDislike, byAct
   const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislikeObject })
 
   return sequelizeTypescript.transaction(async t => {
-    const rate = {
-      type: 'dislike' as 'dislike',
-      videoId: video.id,
-      accountId: byAccount.id
+    const url = getVideoDislikeActivityPubUrl(byActor, video)
+
+    const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, url)
+    if (existingRate && existingRate.type === 'dislike') return
+
+    await video.increment('dislikes', { transaction: t })
+
+    if (existingRate && existingRate.type === 'like') {
+      await video.decrement('likes', { transaction: t })
     }
 
-    const [ , created ] = await AccountVideoRateModel.findOrCreate({
-      where: rate,
-      defaults: Object.assign({}, rate, { url: getVideoDislikeActivityPubUrl(byActor, video) }),
-      transaction: t
-    })
-    if (created === true) await video.increment('dislikes', { transaction: t })
+    const rate = existingRate || new AccountVideoRateModel()
+    rate.type = 'dislike'
+    rate.videoId = video.id
+    rate.accountId = byAccount.id
+    rate.url = url
+
+    await rate.save({ transaction: t })
 
-    if (video.isOwned() && created === true) {
+    if (video.isOwned()) {
       // Don't resend the activity to the sender
       const exceptions = [ byActor ]