From a147d21fdabbb1655b6833567e2a34cc5784f020 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 17 May 2023 10:25:00 +0200 Subject: [PATCH 1/6] Update activitypub.php NodeInfo is only initialized when blog is public --- activitypub.php | 1 - 1 file changed, 1 deletion(-) diff --git a/activitypub.php b/activitypub.php index 3a77944..7448e46 100644 --- a/activitypub.php +++ b/activitypub.php @@ -41,7 +41,6 @@ function init() { Rest\Inbox::init(); Rest\Followers::init(); Rest\Following::init(); - Rest\Nodeinfo::init(); Rest\Webfinger::init(); Admin::init(); From ec3f8454c13a75b6e0f5f38e13e317c0b594e98a Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 17 May 2023 10:25:31 +0200 Subject: [PATCH 2/6] Update activitypub.php --- activitypub.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activitypub.php b/activitypub.php index 7448e46..5b19dce 100644 --- a/activitypub.php +++ b/activitypub.php @@ -128,7 +128,7 @@ function plugin_settings_link( $actions ) { ) ); -register_uninstall_hook( +\register_uninstall_hook( __FILE__, array( __NAMESPACE__ . '\Activitypub', From 6a0fc43a05e56308965854126be745d17aaf109e Mon Sep 17 00:00:00 2001 From: Matt Wiebe Date: Thu, 18 May 2023 19:30:08 -0500 Subject: [PATCH 3/6] Set `ACTIVITYPUB_REST_NAMESPACE` outside of `init` Needed to prevent activation errors. --- activitypub.php | 4 ++-- includes/rest/class-outbox.php | 3 +++ includes/rest/class-webfinger.php | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/activitypub.php b/activitypub.php index 5b19dce..3f582ea 100644 --- a/activitypub.php +++ b/activitypub.php @@ -15,6 +15,8 @@ namespace Activitypub; +\defined( 'ACTIVITYPUB_REST_NAMESPACE' ) || \define( 'ACTIVITYPUB_REST_NAMESPACE', 'activitypub/1.0' ); + /** * Initialize plugin */ @@ -25,8 +27,6 @@ function init() { \defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|(?<=

)|(?<=
)|^)#([A-Za-z0-9_]+)(?:(?=\s|[[:punct:]]|$))' ); \defined( 'ACTIVITYPUB_USERNAME_REGEXP' ) || \define( 'ACTIVITYPUB_USERNAME_REGEXP', '(?:([A-Za-z0-9_-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))' ); \defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "[ap_title]\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" ); - \defined( 'ACTIVITYPUB_REST_NAMESPACE' ) || \define( 'ACTIVITYPUB_REST_NAMESPACE', 'activitypub/1.0' ); - \define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) ); diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index abffbe9..087c0ef 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -80,6 +80,8 @@ class Outbox { // phpcs:ignore $json->totalItems = 0; + // We can query this more directly based on the supplied post types. + // And cache these counts and invalidate them on publish_post. foreach ( $post_types as $post_type ) { $count_posts = \wp_count_posts( $post_type ); $json->totalItems += \intval( $count_posts->publish ); // phpcs:ignore @@ -142,6 +144,7 @@ class Outbox { 'required' => true, 'type' => 'integer', 'validate_callback' => function( $param, $request, $key ) { + // this is probably ok on multisite still? return user_can( $param, 'publish_posts' ); }, ); diff --git a/includes/rest/class-webfinger.php b/includes/rest/class-webfinger.php index f75a3f7..e7c00fa 100644 --- a/includes/rest/class-webfinger.php +++ b/includes/rest/class-webfinger.php @@ -62,6 +62,7 @@ class Webfinger { } $user = \get_user_by( 'login', \esc_sql( $resource_identifier ) ); + /// YIKES NOPE NOPE NOT ON DOTCOM! if ( ! $user || ! \user_can( $user, 'publish_posts' ) ) { return new WP_Error( 'activitypub_user_not_found', \__( 'User not found', 'activitypub' ), array( 'status' => 404 ) ); From d7d6ebbc1fd231ee4d470a2e23d3ad49eaa681e6 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 19 May 2023 11:43:54 +0200 Subject: [PATCH 4/6] remove comments @mattwiebe maybe you can add them as issues --- includes/rest/class-outbox.php | 1 - includes/rest/class-webfinger.php | 1 - 2 files changed, 2 deletions(-) diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index 087c0ef..9fff8c7 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -144,7 +144,6 @@ class Outbox { 'required' => true, 'type' => 'integer', 'validate_callback' => function( $param, $request, $key ) { - // this is probably ok on multisite still? return user_can( $param, 'publish_posts' ); }, ); diff --git a/includes/rest/class-webfinger.php b/includes/rest/class-webfinger.php index e7c00fa..f75a3f7 100644 --- a/includes/rest/class-webfinger.php +++ b/includes/rest/class-webfinger.php @@ -62,7 +62,6 @@ class Webfinger { } $user = \get_user_by( 'login', \esc_sql( $resource_identifier ) ); - /// YIKES NOPE NOPE NOT ON DOTCOM! if ( ! $user || ! \user_can( $user, 'publish_posts' ) ) { return new WP_Error( 'activitypub_user_not_found', \__( 'User not found', 'activitypub' ), array( 'status' => 404 ) ); From 70c3b3fd517c3dbb7064e746e792b004d920182e Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 19 May 2023 11:45:12 +0200 Subject: [PATCH 5/6] remove comments --- includes/rest/class-outbox.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index 9fff8c7..abffbe9 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -80,8 +80,6 @@ class Outbox { // phpcs:ignore $json->totalItems = 0; - // We can query this more directly based on the supplied post types. - // And cache these counts and invalidate them on publish_post. foreach ( $post_types as $post_type ) { $count_posts = \wp_count_posts( $post_type ); $json->totalItems += \intval( $count_posts->publish ); // phpcs:ignore From dd486e552ff40e2bdbd9153ba04afd9d93a1782d Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 19 May 2023 12:00:11 +0200 Subject: [PATCH 6/6] some code cleanups --- includes/rest/class-inbox.php | 28 +++++++++++++++++----------- includes/rest/class-outbox.php | 21 ++++++++++++++------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index b4765d3..2345769 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -1,9 +1,15 @@ \WP_REST_Server::EDITABLE, + 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( self::class, 'shared_inbox_post' ), 'args' => self::shared_inbox_post_parameters(), 'permission_callback' => '__return_true', @@ -45,13 +51,13 @@ class Inbox { '/users/(?P\d+)/inbox', array( array( - 'methods' => \WP_REST_Server::EDITABLE, + 'methods' => WP_REST_Server::EDITABLE, 'callback' => array( self::class, 'user_inbox_post' ), 'args' => self::user_inbox_post_parameters(), 'permission_callback' => '__return_true', ), array( - 'methods' => \WP_REST_Server::READABLE, + 'methods' => WP_REST_Server::READABLE, 'callback' => array( self::class, 'user_inbox_get' ), 'args' => self::user_inbox_get_parameters(), 'permission_callback' => '__return_true', @@ -106,7 +112,7 @@ class Inbox { $json = new \stdClass(); - $json->{'@context'} = \Activitypub\get_context(); + $json->{'@context'} = get_context(); $json->id = \home_url( \add_query_arg( null, null ) ); $json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' ); $json->type = 'OrderedCollectionPage'; @@ -126,7 +132,7 @@ class Inbox { */ \do_action( 'activitypub_inbox_post' ); - $response = new \WP_REST_Response( $json, 200 ); + $response = new WP_REST_Response( $json, 200 ); $response->header( 'Content-Type', 'application/activity+json' ); @@ -150,7 +156,7 @@ class Inbox { \do_action( 'activitypub_inbox', $data, $user_id, $type ); \do_action( "activitypub_inbox_{$type}", $data, $user_id ); - return new \WP_REST_Response( array(), 202 ); + return new WP_REST_Response( array(), 202 ); } /** @@ -166,7 +172,7 @@ class Inbox { $users = self::extract_recipients( $data ); if ( ! $users ) { - return new \WP_Error( + return new WP_Error( 'rest_invalid_param', \__( 'No recipients found', 'activitypub' ), array( @@ -189,7 +195,7 @@ class Inbox { \do_action( "activitypub_inbox_{$type}", $data, $user->ID ); } - return new \WP_REST_Response( array(), 202 ); + return new WP_REST_Response( array(), 202 ); } /** @@ -350,7 +356,7 @@ class Inbox { * @param int $user_id The id of the local blog-user */ public static function handle_reaction( $object, $user_id ) { - $meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] ); + $meta = get_remote_metadata_by_actor( $object['actor'] ); $comment_post_id = \url_to_postid( $object['object'] ); @@ -395,7 +401,7 @@ class Inbox { * @param int $user_id The id of the local blog-user */ public static function handle_create( $object, $user_id ) { - $meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] ); + $meta = get_remote_metadata_by_actor( $object['actor'] ); if ( ! isset( $object['object']['inReplyTo'] ) ) { return; @@ -502,7 +508,7 @@ class Inbox { $users = array(); foreach ( $recipients as $recipient ) { - $user_id = \Activitypub\url_to_authorid( $recipient ); + $user_id = url_to_authorid( $recipient ); $user = get_user_by( 'id', $user_id ); diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index abffbe9..d6b0757 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -1,6 +1,13 @@ \d+)/outbox', array( array( - 'methods' => \WP_REST_Server::READABLE, + 'methods' => WP_REST_Server::READABLE, 'callback' => array( self::class, 'user_outbox_get' ), 'args' => self::request_parameters(), 'permission_callback' => '__return_true', @@ -48,7 +55,7 @@ class Outbox { $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ); if ( ! $author ) { - return new \WP_Error( + return new WP_Error( 'rest_invalid_param', \__( 'User not found', 'activitypub' ), array( @@ -67,9 +74,9 @@ class Outbox { */ \do_action( 'activitypub_outbox_pre' ); - $json = new \stdClass(); + $json = new stdClass(); - $json->{'@context'} = \Activitypub\get_context(); + $json->{'@context'} = 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 ); @@ -103,8 +110,8 @@ class Outbox { ); foreach ( $posts as $post ) { - $activitypub_post = new \Activitypub\Model\Post( $post ); - $activitypub_activity = new \Activitypub\Model\Activity( 'Create', false ); + $activitypub_post = new Post( $post ); + $activitypub_activity = new Activity( 'Create', false ); $activitypub_activity->from_post( $activitypub_post ); $json->orderedItems[] = $activitypub_activity->to_array(); // phpcs:ignore @@ -119,7 +126,7 @@ class Outbox { */ \do_action( 'activitypub_outbox_post' ); - $response = new \WP_REST_Response( $json, 200 ); + $response = new WP_REST_Response( $json, 200 ); $response->header( 'Content-Type', 'application/activity+json' );