From f1f3c3165d8b35e56b48c7b1d73366fc51dff0f3 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Wed, 28 Dec 2022 14:44:45 +0100 Subject: [PATCH 1/7] support threaded comments from ActivityPub --- includes/rest/class-inbox.php | 53 ++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 5c21f43..8891853 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -431,19 +431,47 @@ 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'] ); - - if ( ! isset( $object['object']['inReplyTo'] ) ) { - return; - } - // check if Activity is public or not if ( ! self::is_activity_public( $object ) ) { // @todo maybe send email - return; + return false; } - $comment_post_id = \url_to_postid( $object['object']['inReplyTo'] ); + $meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] ); + + $comment_post_id = 0; + // TODO: search for an existing comment with the source url and abandon if it's already there + + if ( isset( $object['object']['context'] ) ) { + $comment_post_id = \url_to_postid( $object['object']['context'] ); + } + + if ( ! $comment_post_id && isset( $object['object']['inReplyTo'] ) ) { + $comment_post_id = \url_to_postid( $object['object']['inReplyTo'] ); + } + + $comment_parent_id = 0; + $comment_meta = array( + 'source_id' => \esc_url_raw( $object['object']['id'] ), + 'source_url' => \esc_url_raw( $object['object']['url'] ), + 'avatar_url' => \esc_url_raw( $meta['icon']['url'] ), + 'protocol' => 'activitypub', + ); + + if ( isset( $object['object']['inReplyTo'] ) ) { + $comment_meta['parent_url'] = $object['object']['inReplyTo']; + $comment_query = new \WP_Comment_Query( array( 'meta_key' => 'source_id', 'meta_value' => $object['object']['inReplyTo'] ) ); + if ( $comment_query->comments ) { + foreach ( $comment_query->comments as $comment ) { + if ( ! $comment_parent_id ) { + $comment_parent_id = $comment->comment_ID; + } + if ( ! $comment_post_id ) { + $comment_post_id = $comment->comment_post_ID; + } + } + } + } // save only replys and reactions if ( ! $comment_post_id ) { @@ -457,12 +485,8 @@ class Inbox { 'comment_content' => \wp_filter_kses( $object['object']['content'] ), 'comment_type' => '', 'comment_author_email' => '', - 'comment_parent' => 0, - 'comment_meta' => array( - 'source_url' => \esc_url_raw( $object['object']['url'] ), - 'avatar_url' => \esc_url_raw( $meta['icon']['url'] ), - 'protocol' => 'activitypub', - ), + 'comment_parent' => $comment_parent_id, + 'comment_meta' => $comment_meta, ); // disable flood control @@ -472,6 +496,7 @@ class Inbox { \add_filter( 'pre_option_require_name_email', '__return_false' ); $state = \wp_new_comment( $commentdata, true ); + // TODO: search for comments with that source url as their parent url and update their parent \remove_filter( 'pre_option_require_name_email', '__return_false' ); From 6a906e5fe2f1441bdf05782314d2ee3eadda252d Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Tue, 3 Jan 2023 21:42:43 +0100 Subject: [PATCH 2/7] refactor support for threaded comments from ActivityPub --- includes/functions.php | 37 +++++++++++++++ includes/rest/class-inbox.php | 86 +++++++++++++++++++---------------- 2 files changed, 84 insertions(+), 39 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 1abb9d1..3e79e5e 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -311,3 +311,40 @@ function url_to_authorid( $url ) { return 0; } + +/** + * Examine a comment ID and look up an existing comment it represents. + * + * @param string $id ActivityPub object ID (usually a URL) to check. + * + * @return WP_Comment, or undef if no comment could be found. + */ +function object_id_to_comment( $id ) { + $comment_query = new \WP_Comment_Query( array( 'meta_key' => 'source_id', 'meta_value' => $id ) ); + if ( !$comment_query->comments ) { + return; + } + if ( count( $comment_query->comments ) > 1 ){ + \error_log( "More than one comment under {$id}" ); + return; + } + return $comment_query->comments[0]; +} + +/** + * Examine an activity object and find the post that the specified URL field refers to. + * + * @param string $field_name The name of the URL field in the object to query. + * + * @return int Post ID, or null on failure. + */ +function object_to_post_id_by_field_name( $object, $field_name ) { + if ( ! isset( $object['object'][$field_name] ) ) { + return; + } + $result = \url_to_postid( $object['object'][$field_name] ); + \error_log( "@@@ found result for " . $field_name . ": " . $result ); + if ( $result > 0 ) { + return $result; + } +} diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 8891853..38d1d50 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -425,12 +425,13 @@ class Inbox { } /** - * Handles "Create" requests + * Converts a new ActivityPub object to comment data suitable for creating a comment * - * @param array $object The activity-object - * @param int $user_id The id of the local blog-user + * @param array $object The activity-object. + * + * @return array Comment data suitable for creating a comment. */ - public static function handle_create( $object, $user_id ) { + private static function convert_object_to_comment_data( $object ) { // check if Activity is public or not if ( ! self::is_activity_public( $object ) ) { // @todo maybe send email @@ -439,55 +440,62 @@ class Inbox { $meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] ); - $comment_post_id = 0; - // TODO: search for an existing comment with the source url and abandon if it's already there + // Objects must have IDs + if ( ! isset( $object['object']['id'] ) ) { + \error_log( "Comment provided without ID" ); + return; + } + $id = $object['object']['id']; - if ( isset( $object['object']['context'] ) ) { - $comment_post_id = \url_to_postid( $object['object']['context'] ); + // Only handle replies + if ( ! isset( $object['object']['inReplyTo'] ) ) { + return; + } + $in_reply_to = $object['object']['inReplyTo']; + + // Comment already exists + if ( \Activitypub\object_id_to_comment( $id ) ) { + return; } - if ( ! $comment_post_id && isset( $object['object']['inReplyTo'] ) ) { - $comment_post_id = \url_to_postid( $object['object']['inReplyTo'] ); - } + $parent_comment = \Activitypub\object_id_to_comment( $in_reply_to ); - $comment_parent_id = 0; - $comment_meta = array( - 'source_id' => \esc_url_raw( $object['object']['id'] ), - 'source_url' => \esc_url_raw( $object['object']['url'] ), - 'avatar_url' => \esc_url_raw( $meta['icon']['url'] ), - 'protocol' => 'activitypub', - ); - - if ( isset( $object['object']['inReplyTo'] ) ) { - $comment_meta['parent_url'] = $object['object']['inReplyTo']; - $comment_query = new \WP_Comment_Query( array( 'meta_key' => 'source_id', 'meta_value' => $object['object']['inReplyTo'] ) ); - if ( $comment_query->comments ) { - foreach ( $comment_query->comments as $comment ) { - if ( ! $comment_parent_id ) { - $comment_parent_id = $comment->comment_ID; - } - if ( ! $comment_post_id ) { - $comment_post_id = $comment->comment_post_ID; - } - } - } - } - - // save only replys and reactions + // save only replies and reactions + $comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'context' ) ?? + \Activitypub\object_to_post_id_by_field_name( $object, 'inReplyTo' ) ?? + ( $parent_comment ? $parent_comment->comment_post_ID : 0 ); if ( ! $comment_post_id ) { - return false; + return; } - $commentdata = array( + return array( 'comment_post_ID' => $comment_post_id, 'comment_author' => \esc_attr( $meta['name'] ), 'comment_author_url' => \esc_url_raw( $object['actor'] ), 'comment_content' => \wp_filter_kses( $object['object']['content'] ), 'comment_type' => '', 'comment_author_email' => '', - 'comment_parent' => $comment_parent_id, - 'comment_meta' => $comment_meta, + 'comment_parent' => $parent_comment ? $parent_comment->comment_ID : 0, + 'comment_meta' => array( + 'source_id' => \esc_url_raw( $id ), + 'source_url' => \esc_url_raw( $object['object']['url'] ), + 'avatar_url' => \esc_url_raw( $meta['icon']['url'] ), + 'protocol' => 'activitypub', + ), ); + } + + /** + * Handles "Create" requests + * + * @param array $object The activity-object + * @param int $user_id The id of the local blog-user + */ + public static function handle_create( $object, $user_id ) { + $commentdata = self::convert_object_to_comment_data( $object ); + if ( !$commentdata ) { + return false; + } // disable flood control \remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 ); From 8db9be5c2ea12aaac6faf98ddaf3a08b932e2a84 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Tue, 24 Jan 2023 11:10:01 +0800 Subject: [PATCH 3/7] remove debugging log line --- includes/functions.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/functions.php b/includes/functions.php index 3e79e5e..caae289 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -343,7 +343,6 @@ function object_to_post_id_by_field_name( $object, $field_name ) { return; } $result = \url_to_postid( $object['object'][$field_name] ); - \error_log( "@@@ found result for " . $field_name . ": " . $result ); if ( $result > 0 ) { return $result; } From 7fa58cf26c38b4052b502598b8d2a2fdaf4793cf Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Tue, 24 Jan 2023 13:23:23 +0800 Subject: [PATCH 4/7] add first unit tests for class inbox --- includes/rest/class-inbox.php | 6 ++- tests/test-class-inbox.php | 69 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/test-class-inbox.php diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 38d1d50..8dc8ae4 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -431,7 +431,11 @@ class Inbox { * * @return array Comment data suitable for creating a comment. */ - private static function convert_object_to_comment_data( $object ) { + public static function convert_object_to_comment_data( $object ) { + if ( ! isset( $object['object'] ) ) { + return false; + } + // check if Activity is public or not if ( ! self::is_activity_public( $object ) ) { // @todo maybe send email diff --git a/tests/test-class-inbox.php b/tests/test-class-inbox.php new file mode 100644 index 0000000..4b13e1d --- /dev/null +++ b/tests/test-class-inbox.php @@ -0,0 +1,69 @@ +user_url = $authordata->user_url; + + $post = \wp_insert_post( + array( + 'post_author' => 1, + 'post_content' => 'test', + ) + ); + $this->post_permalink = \get_permalink( $post ); + + \add_filter( 'pre_get_remote_metadata_by_actor', array( '\Test_Inbox', 'get_remote_metadata_by_actor' ), 10, 2); + } + + public function get_remote_metadata_by_actor( $value, $actor ) { + return array( + "name" => "Example User", + "icon" => array( + "url" => "https://example.com/icon", + ), + ); + } + + public function test_convert_object_to_comment_data_basic() { + $inbox = new \Activitypub\Rest\Inbox(); + $object = array( + "actor" => $this->user_url, + "to" => [ $this->user_url ], + "cc" => [ "https://www.w3.org/ns/activitystreams#Public" ], + "object" => array( + "id" => "123", + "url" => "https://example.com/example", + "inReplyTo" => $this->post_permalink, + "content" => "example", + ), + ); + $converted = $inbox->convert_object_to_comment_data($object); + + $this->assertGreaterThan(1, $converted["comment_post_ID"]); + $this->assertEquals($converted["comment_author"], "Example User"); + $this->assertEquals($converted["comment_author_url"], "http://example.org"); + $this->assertEquals($converted["comment_content"], "example"); + $this->assertEquals($converted["comment_type"], ""); + $this->assertEquals($converted["comment_author_email"], ""); + $this->assertEquals($converted["comment_parent"], 0); + $this->assertArrayHasKey("comment_meta", $converted); + $this->assertEquals($converted["comment_meta"]["source_id"], "http://123"); + $this->assertEquals($converted["comment_meta"]["source_url"], "https://example.com/example"); + $this->assertEquals($converted["comment_meta"]["avatar_url"], "https://example.com/icon"); + $this->assertEquals($converted["comment_meta"]["protocol"], "activitypub"); + } + + public function test_convert_object_to_comment_data_non_public_rejected() { + $inbox = new \Activitypub\Rest\Inbox(); + $object = array( + "to" => ["https://example.com/profile/test"], + "cc" => [], + ); + $converted = $inbox->convert_object_to_comment_data($object); + $this->assertFalse($converted); + } +} From 85ca37aa1d991a4f5be68fe35de96d9c9110bd96 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Tue, 24 Jan 2023 13:32:29 +0800 Subject: [PATCH 5/7] fix code smells --- includes/functions.php | 15 ++++++++++----- includes/rest/class-inbox.php | 20 ++++++++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index caae289..8872e84 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -320,11 +320,16 @@ function url_to_authorid( $url ) { * @return WP_Comment, or undef if no comment could be found. */ function object_id_to_comment( $id ) { - $comment_query = new \WP_Comment_Query( array( 'meta_key' => 'source_id', 'meta_value' => $id ) ); - if ( !$comment_query->comments ) { + $comment_query = new \WP_Comment_Query( + array( + 'meta_key' => 'source_id', + 'meta_value' => $id, + ) + ); + if ( ! $comment_query->comments ) { return; } - if ( count( $comment_query->comments ) > 1 ){ + if ( count( $comment_query->comments ) > 1 ) { \error_log( "More than one comment under {$id}" ); return; } @@ -339,10 +344,10 @@ function object_id_to_comment( $id ) { * @return int Post ID, or null on failure. */ function object_to_post_id_by_field_name( $object, $field_name ) { - if ( ! isset( $object['object'][$field_name] ) ) { + if ( ! isset( $object['object'][ $field_name ] ) ) { return; } - $result = \url_to_postid( $object['object'][$field_name] ); + $result = \url_to_postid( $object['object'][ $field_name ] ); if ( $result > 0 ) { return $result; } diff --git a/includes/rest/class-inbox.php b/includes/rest/class-inbox.php index 8dc8ae4..5c3cd17 100644 --- a/includes/rest/class-inbox.php +++ b/includes/rest/class-inbox.php @@ -432,10 +432,10 @@ class Inbox { * @return array Comment data suitable for creating a comment. */ public static function convert_object_to_comment_data( $object ) { - if ( ! isset( $object['object'] ) ) { + if ( ! isset( $object['object'] ) ) { return false; - } - + } + // check if Activity is public or not if ( ! self::is_activity_public( $object ) ) { // @todo maybe send email @@ -446,7 +446,7 @@ class Inbox { // Objects must have IDs if ( ! isset( $object['object']['id'] ) ) { - \error_log( "Comment provided without ID" ); + \error_log( 'Comment provided without ID' ); return; } $id = $object['object']['id']; @@ -465,9 +465,13 @@ class Inbox { $parent_comment = \Activitypub\object_id_to_comment( $in_reply_to ); // save only replies and reactions - $comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'context' ) ?? - \Activitypub\object_to_post_id_by_field_name( $object, 'inReplyTo' ) ?? - ( $parent_comment ? $parent_comment->comment_post_ID : 0 ); + $comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'context' ); + if ( ! $comment_post_id ) { + $comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'inReplyTo' ); + } + if ( ! $comment_post_id ) { + $comment_post_id = $parent_comment->comment_post_ID; + } if ( ! $comment_post_id ) { return; } @@ -497,7 +501,7 @@ class Inbox { */ public static function handle_create( $object, $user_id ) { $commentdata = self::convert_object_to_comment_data( $object ); - if ( !$commentdata ) { + if ( ! $commentdata ) { return false; } From a9a5b112b0f10b535b7eb3386b1ef616ba98fab2 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Tue, 24 Jan 2023 13:34:15 +0800 Subject: [PATCH 6/7] make filter function static --- tests/test-class-inbox.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-class-inbox.php b/tests/test-class-inbox.php index 4b13e1d..f5b29b1 100644 --- a/tests/test-class-inbox.php +++ b/tests/test-class-inbox.php @@ -19,7 +19,7 @@ class Test_Inbox extends WP_UnitTestCase { \add_filter( 'pre_get_remote_metadata_by_actor', array( '\Test_Inbox', 'get_remote_metadata_by_actor' ), 10, 2); } - public function get_remote_metadata_by_actor( $value, $actor ) { + public static function get_remote_metadata_by_actor( $value, $actor ) { return array( "name" => "Example User", "icon" => array( From 22e0ddc134cd57e0cfaa195b89fb52cd830dfab7 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Tue, 24 Jan 2023 13:55:00 +0800 Subject: [PATCH 7/7] attempt to resolve backwards compatibility issues --- tests/test-class-inbox.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test-class-inbox.php b/tests/test-class-inbox.php index f5b29b1..38f978e 100644 --- a/tests/test-class-inbox.php +++ b/tests/test-class-inbox.php @@ -1,10 +1,11 @@ user_url = $authordata->user_url;