]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-like.ts
Manual approves followers only for the instance
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-like.ts
index 8b97aae556bd985e9b2747f8d953bf239ba83564..62be0de42f30aa82b076f6d41e64539f5effcc1a 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 { MActorSignature } 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: MActorSignature, activity: ActivityLike) {
   const videoUrl = getAPId(activity.object)
 
   const byAccount = byActor.Account
@@ -34,15 +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 })
+    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 ]