From c797109fd71001ea777075cd4180d1cbc89927cf Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Tue, 5 Jan 2021 21:56:38 +0100 Subject: [PATCH] consistent API response, GET inbox and filterable follower list --- includes/class-activitypub.php | 4 +- includes/rest/class-followers.php | 5 ++ includes/rest/class-following.php | 9 ++- includes/rest/class-inbox.php | 69 +++++++++++++++++-- includes/rest/class-outbox.php | 4 +- .../{json-author.php => author-json.php} | 0 templates/{json-post.php => post-json.php} | 0 7 files changed, 80 insertions(+), 11 deletions(-) rename templates/{json-author.php => author-json.php} (100%) rename templates/{json-post.php => post-json.php} (100%) diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 6c7c0c8..d26e213 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -39,9 +39,9 @@ class Activitypub { } if ( \is_author() ) { - $json_template = \dirname( __FILE__ ) . '/../templates/json-author.php'; + $json_template = \dirname( __FILE__ ) . '/../templates/author-json.php'; } elseif ( \is_singular() ) { - $json_template = \dirname( __FILE__ ) . '/../templates/json-post.php'; + $json_template = \dirname( __FILE__ ) . '/../templates/post-json.php'; } global $wp_query; diff --git a/includes/rest/class-followers.php b/includes/rest/class-followers.php index cde30a0..f246edd 100644 --- a/includes/rest/class-followers.php +++ b/includes/rest/class-followers.php @@ -61,6 +61,11 @@ class Followers { $json->{'@context'} = \Activitypub\get_context(); + $json->id = \home_url( \add_query_arg( null, null ) ); + $json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' ); + $json->actor = \get_author_posts_url( $user_id ); + $json->type = 'OrderedCollectionPage'; + $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/followers" ); // phpcs:ignore $json->totalItems = \Activitypub\count_followers( $user_id ); // phpcs:ignore $json->orderedItems = \Activitypub\Peer\Followers::get_followers( $user_id ); // phpcs:ignore diff --git a/includes/rest/class-following.php b/includes/rest/class-following.php index 4809a9a..8e37597 100644 --- a/includes/rest/class-following.php +++ b/includes/rest/class-following.php @@ -61,14 +61,17 @@ class Following { $json->{'@context'} = \Activitypub\get_context(); + $json->id = \home_url( \add_query_arg( null, null ) ); + $json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' ); + $json->actor = \get_author_posts_url( $user_id ); + $json->type = 'OrderedCollectionPage'; + $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/following" ); // phpcs:ignore $json->totalItems = 0; // phpcs:ignore - $json->orderedItems = array(); // phpcs:ignore + $json->orderedItems = apply_filters( 'activitypub_following', array(), $user ); // phpcs:ignore $json->first = $json->partOf; // phpcs:ignore - $json->first = \get_rest_url( null, "/activitypub/1.0/users/$user_id/following" ); - $response = new \WP_REST_Response( $json, 200 ); $response->header( 'Content-Type', 'application/activity+json' ); diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 61e6dbe..8fef0ff 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -30,7 +30,7 @@ class Inbox { 'activitypub/1.0', '/inbox', array( array( 'methods' => \WP_REST_Server::EDITABLE, - 'callback' => array( '\Activitypub\Rest\Inbox', 'shared_inbox' ), + 'callback' => array( '\Activitypub\Rest\Inbox', 'shared_inbox_post' ), 'args' => self::shared_inbox_request_parameters(), 'permission_callback' => '__return_true', ), @@ -41,10 +41,15 @@ class Inbox { 'activitypub/1.0', '/users/(?P\d+)/inbox', array( array( 'methods' => \WP_REST_Server::EDITABLE, - 'callback' => array( '\Activitypub\Rest\Inbox', 'user_inbox' ), + 'callback' => array( '\Activitypub\Rest\Inbox', 'user_inbox_post' ), 'args' => self::user_inbox_request_parameters(), 'permission_callback' => '__return_true', ), + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array( '\Activitypub\Rest\Inbox', 'user_inbox_get' ), + 'permission_callback' => '__return_true', + ), ) ); } @@ -82,10 +87,66 @@ class Inbox { * Renders the user-inbox * * @param WP_REST_Request $request + * @return WP_REST_Response + */ + public static function user_inbox_get( $request ) { + $user_id = $request->get_param( 'user_id' ); + $author = \get_user_by( 'ID', $user_id ); + + if ( ! $author ) { + return new \WP_Error( 'rest_invalid_param', \__( 'User not found', 'activitypub' ), array( + 'status' => 404, + 'params' => array( + 'user_id' => \__( 'User not found', 'activitypub' ), + ), + ) ); + } + + $page = $request->get_param( 'page', 0 ); + + /* + * Action triggerd prior to the ActivityPub profile being created and sent to the client + */ + \do_action( 'activitypub_inbox_pre' ); + + $json = new \stdClass(); + + $json->{'@context'} = \Activitypub\get_context(); + $json->id = \home_url( \add_query_arg( null, null ) ); + $json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' ); + $json->actor = \get_author_posts_url( $user_id ); + $json->type = 'OrderedCollectionPage'; + $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/inbox" ); // phpcs:ignore + + $json->totalItems = 0; // phpcs:ignore + + $json->orderedItems = array(); // phpcs:ignore + + $json->first = $json->partOf; // phpcs:ignore + + // filter output + $json = \apply_filters( 'activitypub_inbox_array', $json ); + + /* + * Action triggerd after the ActivityPub profile has been created and sent to the client + */ + \do_action( 'activitypub_inbox_post' ); + + $response = new \WP_REST_Response( $json, 200 ); + + $response->header( 'Content-Type', 'application/activity+json' ); + + return $response; + } + + /** + * Handles user-inbox requests + * + * @param WP_REST_Request $request * * @return WP_REST_Response */ - public static function user_inbox( $request ) { + public static function user_inbox_post( $request ) { $user_id = $request->get_param( 'user_id' ); $data = $request->get_params(); @@ -104,7 +165,7 @@ class Inbox { * * @return WP_REST_Response */ - public static function shared_inbox( $request ) { + public static function shared_inbox_post( $request ) { $data = $request->get_params(); $type = \strtoloer( $request->get_param( 'type' ) ); diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index d4c0c0a..54ca9b1 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -24,7 +24,7 @@ class Outbox { 'activitypub/1.0', '/users/(?P\d+)/outbox', array( array( 'methods' => \WP_REST_Server::READABLE, - 'callback' => array( '\Activitypub\Rest\Outbox', 'user_outbox' ), + 'callback' => array( '\Activitypub\Rest\Outbox', 'user_outbox_get' ), 'args' => self::request_parameters(), 'permission_callback' => '__return_true', ), @@ -38,7 +38,7 @@ class Outbox { * @param WP_REST_Request $request * @return WP_REST_Response */ - public static function user_outbox( $request ) { + public static function user_outbox_get( $request ) { $user_id = $request->get_param( 'user_id' ); $author = \get_user_by( 'ID', $user_id ); diff --git a/templates/json-author.php b/templates/author-json.php similarity index 100% rename from templates/json-author.php rename to templates/author-json.php diff --git a/templates/json-post.php b/templates/post-json.php similarity index 100% rename from templates/json-post.php rename to templates/post-json.php