]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-undo.ts
Fix user notifications on new follow
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-undo.ts
index ff019cd8cff555a390885006fd02b19cea9329fd..7a0f90adfc3821d6fab4c364269fb986310f7fad 100644 (file)
@@ -10,8 +10,10 @@ import { forwardVideoRelatedActivity } from '../send/utils'
 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
 import { VideoShareModel } from '../../../models/video/video-share'
 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
+import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
 
-async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel) {
+async function processUndoActivity (options: APProcessorOptions<ActivityUndo>) {
+  const { activity, byActor } = options
   const activityToUndo = activity.object
 
   if (activityToUndo.type === 'Like') {
@@ -26,6 +28,10 @@ async function processUndoActivity (activity: ActivityUndo, byActor: ActorModel)
     }
   }
 
+  if (activityToUndo.type === 'Dislike') {
+    return retryTransactionWrapper(processUndoDislike, byActor, activity)
+  }
+
   if (activityToUndo.type === 'Follow') {
     return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
   }
@@ -55,8 +61,8 @@ async function processUndoLike (byActor: ActorModel, activity: ActivityUndo) {
   return sequelizeTypescript.transaction(async t => {
     if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
 
-    const rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
-    if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
+    const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, likeActivity.id, t)
+    if (!rate || rate.type !== 'like') throw new Error(`Unknown like by account ${byActor.Account.id} for video ${video.id}.`)
 
     await rate.destroy({ transaction: t })
     await video.decrement('likes', { transaction: t })
@@ -71,15 +77,17 @@ async function processUndoLike (byActor: ActorModel, activity: ActivityUndo) {
 }
 
 async function processUndoDislike (byActor: ActorModel, activity: ActivityUndo) {
-  const dislike = activity.object.object as DislikeObject
+  const dislike = activity.object.type === 'Dislike'
+    ? activity.object
+    : activity.object.object as DislikeObject
 
   const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
 
   return sequelizeTypescript.transaction(async t => {
     if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
 
-    const rate = await AccountVideoRateModel.load(byActor.Account.id, video.id, t)
-    if (!rate) throw new Error(`Unknown rate by account ${byActor.Account.id} for video ${video.id}.`)
+    const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, dislike.id, t)
+    if (!rate || rate.type !== 'dislike') throw new Error(`Unknown dislike by account ${byActor.Account.id} for video ${video.id}.`)
 
     await rate.destroy({ transaction: t })
     await video.decrement('dislikes', { transaction: t })
@@ -100,7 +108,10 @@ async function processUndoCacheFile (byActor: ActorModel, activity: ActivityUndo
 
   return sequelizeTypescript.transaction(async t => {
     const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
-    if (!cacheFile) throw new Error('Unknown video cache ' + cacheFileObject.id)
+    if (!cacheFile) {
+      logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id)
+      return
+    }
 
     if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')