From 1ee21d54dc83d383ae56b799a31905da37d50f82 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Thu, 20 Sep 2018 19:10:15 -0400 Subject: [PATCH] Implement follow for inbox --- inc/activities/follow.php | 40 ++++++++++++++++++++++++++++++++++++--- inc/inbox.php | 8 +++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/inc/activities/follow.php b/inc/activities/follow.php index f4b0cab..42a08ee 100644 --- a/inc/activities/follow.php +++ b/inc/activities/follow.php @@ -4,22 +4,56 @@ namespace activities\follow; require_once plugin_dir_path( __FILE__ ) . '/../following.php'; require_once plugin_dir_path( __FILE__ ) . '/../actors.php'; require_once plugin_dir_path( __FILE__ ) . '/../objects.php'; +require_once plugin_dir_path( __FILE__ ) . '/../outbox.php'; -function handle_outbox( $actor, $activity ) { +function handle_outbox( $actor_slug, $activity ) { if ( !array_key_exists( 'object', $activity ) ) { return new \WP_Error( 'invalid_activity', - __( 'Expected an object', 'activitypub' ), + __( 'Expected an object', 'pterotype' ), array( 'status' => 400 ) ); } $object = $activity['object']; $object_row = \objects\upsert_object( $object ); - $actor_id = \actors\get_actor_id( $actor ); + $actor_id = \actors\get_actor_id( $actor_slug ); $res = \following\request_follow( $actor_id, $object_row->id ); if ( is_wp_error( $res ) ) { return $res; } return $activity; } + +function handle_inbox( $actor_slug, $activity ) { + // For now, always Accept follow requests + // in the future, implement a UI to either accept or reject + // (or automatically accept if the user chooses to enable that setting) + $accept = make_accept( $actor_slug, $activity ); + if ( is_wp_error( $accept ) ) { + return $accept; + } + $res = \outbox\handle_activity( $actor_slug, $accept ); + if ( is_wp_error( $res ) ) { + return $res; + } + return $activity; +} + +function make_accept( $actor_slug, $follow ) { + if ( !array_key_exists( 'actor', $follow ) ) { + return new \WP_Error( + 'invalid_activity', + __( 'Activity must have an actor', 'pterotype' ), + array( 'status' => 400 ) + ); + } + $accept = array( + '@context' => 'https://www.w3.org/ns/activitystreams', + 'type' => 'Accept', + 'actor' => \actors\get_actor_by_slug( $actor_slug ), + 'object' => $follow, + 'to' => $follow['actor'], + ); + return $accept; +} ?> diff --git a/inc/inbox.php b/inc/inbox.php index 69e9308..0b70a40 100644 --- a/inc/inbox.php +++ b/inc/inbox.php @@ -13,6 +13,7 @@ require_once plugin_dir_path( __FILE__ ) . '/activities.php'; require_once plugin_dir_path( __FILE__ ) . '/activities/create.php'; require_once plugin_dir_path( __FILE__ ) . '/activities/update.php'; require_once plugin_dir_path( __FILE__ ) . '/activities/delete.php'; +require_once plugin_dir_path( __FILE__ ) . '/activities/follow.php'; function handle_activity( $actor_slug, $activity ) { if ( !array_key_exists( 'type', $activity ) ) { @@ -25,15 +26,16 @@ function handle_activity( $actor_slug, $activity ) { forward_activity( $activity ); switch ( $activity['type'] ) { case 'Create': - $activity = \create\handle_inbox( $actor_slug, $activity ); + $activity = \activities\create\handle_inbox( $actor_slug, $activity ); break; case 'Update': - $activity = \update\handle_inbox( $actor_slug, $activity ); + $activity = \activities\update\handle_inbox( $actor_slug, $activity ); break; case 'Delete': - $activity = \delete\handle_inbox( $actor_slug, $activity ); + $activity = \activities\delete\handle_inbox( $actor_slug, $activity ); break; case 'Follow': + $activity = \activities\follow\handle_inbox( $actor_slug, $activity ); break; case 'Accept': break;