]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-accept.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-accept.ts
index 73c6cb27913d9fc1089d84196667f436dfb17d68..077b01eda878d9a56a5732435c184f903082701f 100644 (file)
@@ -1,14 +1,14 @@
-import { ActivityAccept } from '../../../../shared/models/activitypub/activity'
-import { database as db } from '../../../initializers'
-import { AccountInstance } from '../../../models/account/account-interface'
-import { addFetchOutboxJob } from '../fetch'
+import { ActivityAccept } from '../../../../shared/models/activitypub'
+import { ActorFollowModel } from '../../../models/actor/actor-follow'
+import { APProcessorOptions } from '../../../types/activitypub-processor.model'
+import { MActorDefault, MActorSignature } from '../../../types/models'
+import { addFetchOutboxJob } from '../outbox'
 
-async function processAcceptActivity (activity: ActivityAccept, inboxAccount?: AccountInstance) {
-  if (inboxAccount === undefined) throw new Error('Need to accept on explicit inbox.')
+async function processAcceptActivity (options: APProcessorOptions<ActivityAccept>) {
+  const { byActor: targetActor, inboxActor } = options
+  if (inboxActor === undefined) throw new Error('Need to accept on explicit inbox.')
 
-  const targetAccount = await db.Account.loadByUrl(activity.actor)
-
-  return processAccept(inboxAccount, targetAccount)
+  return processAccept(inboxActor, targetActor)
 }
 
 // ---------------------------------------------------------------------------
@@ -19,11 +19,14 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function processAccept (account: AccountInstance, targetAccount: AccountInstance) {
-  const follow = await db.AccountFollow.loadByAccountAndTarget(account.id, targetAccount.id)
+async function processAccept (actor: MActorDefault, targetActor: MActorSignature) {
+  const follow = await ActorFollowModel.loadByActorAndTarget(actor.id, targetActor.id)
   if (!follow) throw new Error('Cannot find associated follow.')
 
-  follow.set('state', 'accepted')
-  await follow.save()
-  await addFetchOutboxJob(targetAccount, undefined)
+  if (follow.state !== 'accepted') {
+    follow.state = 'accepted'
+    await follow.save()
+
+    await addFetchOutboxJob(targetActor)
+  }
 }