]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-like.ts
Stronger actor association typing in AP functions
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-like.ts
index 5b2ab4b6614a4e25b0666586672f689706b10a48..cf559af721c4c65b816ce60acbf8f913f1ecf863 100644 (file)
@@ -2,13 +2,15 @@ import { ActivityLike } from '../../../../shared/models/activitypub'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
 import { sequelizeTypescript } from '../../../initializers'
 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
-import { ActorModel } from '../../../models/activitypub/actor'
 import { forwardVideoRelatedActivity } from '../send/utils'
 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
 import { getVideoLikeActivityPubUrl } from '../url'
 import { getAPId } from '../../../helpers/activitypub'
+import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
+import { SignatureActorModel } from '../../../typings/models'
 
-async function processLikeActivity (activity: ActivityLike, byActor: ActorModel) {
+async function processLikeActivity (options: APProcessorOptions<ActivityLike>) {
+  const { activity, byActor } = options
   return retryTransactionWrapper(processLikeVideo, byActor, activity)
 }
 
@@ -20,7 +22,7 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) {
+async function processLikeVideo (byActor: SignatureActorModel, activity: ActivityLike) {
   const videoUrl = getAPId(activity.object)
 
   const byAccount = byActor.Account
@@ -34,19 +36,20 @@ async function processLikeVideo (byActor: ActorModel, activity: ActivityLike) {
     const existingRate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byAccount.id, video.id, url)
     if (existingRate && existingRate.type === 'like') return
 
-    await AccountVideoRateModel.create({
-      type: 'like' as 'like',
-      videoId: video.id,
-      accountId: byAccount.id,
-      url
-    }, { transaction: t })
-
-    await video.increment('likes', { transaction: t })
-
     if (existingRate && existingRate.type === 'dislike') {
       await video.decrement('dislikes', { transaction: t })
     }
 
+    await video.increment('likes', { transaction: t })
+
+    const rate = existingRate || new AccountVideoRateModel()
+    rate.type = 'like'
+    rate.videoId = video.id
+    rate.accountId = byAccount.id
+    rate.url = url
+
+    await rate.save({ transaction: t })
+
     if (video.isOwned()) {
       // Don't resend the activity to the sender
       const exceptions = [ byActor ]