Add reject processing for activitypub
authorChocobozzz <me@florianbigard.com>
Thu, 11 Jan 2018 16:37:49 +0000 (17:37 +0100)
committerChocobozzz <me@florianbigard.com>
Thu, 11 Jan 2018 16:45:57 +0000 (17:45 +0100)
server/helpers/custom-validators/activitypub/activity.ts
server/helpers/custom-validators/activitypub/actor.ts
server/lib/activitypub/process/process-reject.ts [new file with mode: 0644]
server/lib/activitypub/process/process.ts
shared/models/activitypub/activity.ts

index 577cf4b52f07bba4dbac2bfb4d8743114ae81c4a..632f1422317c19ccc6c0aecb758e0a29cb037755 100644 (file)
@@ -1,6 +1,9 @@
 import * as validator from 'validator'
 import { Activity, ActivityType } from '../../../../shared/models/activitypub'
-import { isActorAcceptActivityValid, isActorDeleteActivityValid, isActorFollowActivityValid, isActorUpdateActivityValid } from './actor'
+import {
+  isActorAcceptActivityValid, isActorDeleteActivityValid, isActorFollowActivityValid, isActorRejectActivityValid,
+  isActorUpdateActivityValid
+} from './actor'
 import { isAnnounceActivityValid } from './announce'
 import { isActivityPubUrlValid } from './misc'
 import { isDislikeActivityValid, isLikeActivityValid } from './rate'
@@ -33,6 +36,7 @@ const activityCheckers: { [ P in ActivityType ]: (activity: Activity) => boolean
   Delete: checkDeleteActivity,
   Follow: checkFollowActivity,
   Accept: checkAcceptActivity,
+  Reject: checkRejectActivity,
   Announce: checkAnnounceActivity,
   Undo: checkUndoActivity,
   Like: checkLikeActivity
@@ -82,6 +86,10 @@ function checkAcceptActivity (activity: any) {
   return isActorAcceptActivityValid(activity)
 }
 
+function checkRejectActivity (activity: any) {
+  return isActorRejectActivityValid(activity)
+}
+
 function checkAnnounceActivity (activity: any) {
   return isAnnounceActivityValid(activity)
 }
index e1a4b5b8f8a40d880b281ebaa1757e2708aeea72..df0edc30e5bde124a49f9900674f2123fe8fd0da 100644 (file)
@@ -78,6 +78,10 @@ function isActorAcceptActivityValid (activity: any) {
   return isBaseActivityValid(activity, 'Accept')
 }
 
+function isActorRejectActivityValid (activity: any) {
+  return isBaseActivityValid(activity, 'Reject')
+}
+
 function isActorUpdateActivityValid (activity: any) {
   return isBaseActivityValid(activity, 'Update') &&
     isActorObjectValid(activity.object)
@@ -97,6 +101,7 @@ export {
   isActorFollowersCountValid,
   isActorFollowActivityValid,
   isActorAcceptActivityValid,
+  isActorRejectActivityValid,
   isActorDeleteActivityValid,
   isActorUpdateActivityValid
 }
diff --git a/server/lib/activitypub/process/process-reject.ts b/server/lib/activitypub/process/process-reject.ts
new file mode 100644 (file)
index 0000000..b2de28d
--- /dev/null
@@ -0,0 +1,32 @@
+import { ActivityReject } from '../../../../shared/models/activitypub/activity'
+import { sequelizeTypescript } from '../../../initializers'
+import { ActorModel } from '../../../models/activitypub/actor'
+import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
+
+async function processRejectActivity (activity: ActivityReject, inboxActor?: ActorModel) {
+  if (inboxActor === undefined) throw new Error('Need to reject on explicit inbox.')
+
+  const targetActor = await ActorModel.loadByUrl(activity.actor)
+
+  return processReject(inboxActor, targetActor)
+}
+
+// ---------------------------------------------------------------------------
+
+export {
+  processRejectActivity
+}
+
+// ---------------------------------------------------------------------------
+
+async function processReject (actor: ActorModel, targetActor: ActorModel) {
+  return sequelizeTypescript.transaction(async t => {
+    const actorFollow = await ActorFollowModel.loadByActorAndTarget(actor.id, targetActor.id, t)
+
+    if (!actorFollow) throw new Error(`'Unknown actor follow ${actor.id} -> ${targetActor.id}.`)
+
+    await actorFollow.destroy({ transaction: t })
+
+    return undefined
+  })
+}
index 62d310f2101d08c0dc4abb0387161fd44673e8da..810c28e2624f46ee6ab39575870c743db8c3a69e 100644 (file)
@@ -7,6 +7,7 @@ import { processCreateActivity } from './process-create'
 import { processDeleteActivity } from './process-delete'
 import { processFollowActivity } from './process-follow'
 import { processLikeActivity } from './process-like'
+import { processRejectActivity } from './process-reject'
 import { processUndoActivity } from './process-undo'
 import { processUpdateActivity } from './process-update'
 
@@ -16,6 +17,7 @@ const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxActor?
   Delete: processDeleteActivity,
   Follow: processFollowActivity,
   Accept: processAcceptActivity,
+  Reject: processRejectActivity,
   Announce: processAnnounceActivity,
   Undo: processUndoActivity,
   Like: processLikeActivity
index 8cede49a0b8ff8bfc882df17eaa2d67f53b0ccc7..aa25db4467065a21003912ae02b8cecb90eaf7d1 100644 (file)
@@ -8,9 +8,9 @@ import { ViewObject } from './objects/view-object'
 
 export type Activity = ActivityCreate | ActivityUpdate |
   ActivityDelete | ActivityFollow | ActivityAccept | ActivityAnnounce |
-  ActivityUndo | ActivityLike
+  ActivityUndo | ActivityLike | ActivityReject
 
-export type ActivityType = 'Create' | 'Update' | 'Delete' | 'Follow' | 'Accept' | 'Announce' | 'Undo' | 'Like'
+export type ActivityType = 'Create' | 'Update' | 'Delete' | 'Follow' | 'Accept' | 'Announce' | 'Undo' | 'Like' | 'Reject'
 
 export interface ActivityAudience {
   to: string[]
@@ -52,6 +52,11 @@ export interface ActivityAccept extends BaseActivity {
   object: ActivityFollow
 }
 
+export interface ActivityReject extends BaseActivity {
+  type: 'Reject'
+  object: ActivityFollow
+}
+
 export interface ActivityAnnounce extends BaseActivity {
   type: 'Announce'
   object: ActivityCreate | string