From a61f1168c3cad2cb82c8c42afa9ee9875b1bd356 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Sat, 10 Dec 2022 09:33:48 +0100 Subject: [PATCH] Automatically hide unknown @mentions in the Friends plugin, add a setting to change this --- .../class-friends-feed-parser-activitypub.php | 71 +++++++++++++++++++ ...-class-friends-feed-parser-activitypub.php | 58 ++++++++++++++- 2 files changed, 127 insertions(+), 2 deletions(-) diff --git a/integration/class-friends-feed-parser-activitypub.php b/integration/class-friends-feed-parser-activitypub.php index a59546b..7500ca0 100644 --- a/integration/class-friends-feed-parser-activitypub.php +++ b/integration/class-friends-feed-parser-activitypub.php @@ -30,6 +30,10 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser { \add_action( 'friends_feed_parser_activitypub_unfollow', array( $this, 'unfollow_user' ), 10, 2 ); \add_filter( 'friends_rewrite_incoming_url', array( $this, 'friends_rewrite_incoming_url' ), 10, 2 ); + \add_filter( 'friends_edit_friend_table_end', array( $this, 'activitypub_settings' ), 10 ); + \add_filter( 'friends_edit_friend_after_form_submit', array( $this, 'activitypub_save_settings' ), 10 ); + \add_filter( 'friends_modify_feed_item', array( $this, 'modify_incoming_item' ), 9, 3 ); + \add_filter( 'the_content', array( $this, 'the_content' ), 99, 2 ); \add_filter( 'activitypub_extract_mentions', array( $this, 'activitypub_extract_mentions' ), 10, 2 ); } @@ -462,4 +466,71 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser { return $result[0]; } + + public function activitypub_save_settings( \Friends\User $friend ) { + if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'edit-friend-feeds-' . $friend->ID ) ) { + + if ( isset( $_POST['friends_show_replies'] ) && $_POST['friends_show_replies'] ) { + $friend->update_user_option( 'activitypub_friends_show_replies', '1' ); + } else { + $friend->delete_user_option( 'activitypub_friends_show_replies' ); + } + } + } + + public function activitypub_settings( \Friends\User $friend ) { + foreach ( $friend->get_active_feeds() as $feed ) { + if ( 'activitypub' === $feed->parser ) { + return; + } + } + $show_replies = $friend->get_user_option( 'activitypub_friends_show_replies' ); + ?> + + ActivityPub + +
+
+ /> + +
+
+

+ +

+ + + get_parser() ) { + return $item; + } + + if ( ! $friend_user->get_user_option( 'activitypub_friends_show_replies' ) ) { + $plain_text_content = \wp_strip_all_tags( $item->post_content ); + + if ( preg_match( ' /^@(?:[a-zA-Z0-9_.-]+)/i', $plain_text_content, $m ) ) { + $users = $this->get_possible_mentions(); + if ( ! isset( $users[ $m[0] ] ) ) { + $item->_feed_rule_transform = array( + 'post_status' => 'trash', + ); + } + } + } + + return $item; + } } diff --git a/tests/test-class-friends-feed-parser-activitypub.php b/tests/test-class-friends-feed-parser-activitypub.php index 55a4230..b4d5cf7 100644 --- a/tests/test-class-friends-feed-parser-activitypub.php +++ b/tests/test-class-friends-feed-parser-activitypub.php @@ -8,6 +8,7 @@ class Test_Friends_Feed_Parser_ActivityPub extends ActivityPub_TestCase_Cache_HT private $actor; public function test_incoming_post() { + update_user_option( 'activitypub_friends_show_replies', '1', $this->friend_id ); $now = time() - 10; $status_id = 123; @@ -107,6 +108,60 @@ class Test_Friends_Feed_Parser_ActivityPub extends ActivityPub_TestCase_Cache_HT $this->assertEquals( $content, $posts[0]->post_content ); $this->assertEquals( $this->friend_id, $posts[0]->post_author ); $this->assertEquals( $this->friend_name, get_post_meta( $posts[0]->ID, 'author', true ) ); + + delete_user_option( 'activitypub_friends_show_replies', $this->friend_id ); + + } + + public function test_incoming_mention_of_others() { + $now = time() - 10; + $status_id = 123; + + $posts = get_posts( + array( + 'post_type' => \Friends\Friends::CPT, + 'author' => $this->friend_id, + ) + ); + + $post_count = count( $posts ); + + // Let's post a new Note through the REST API. + $date = gmdate( \DATE_W3C, $now++ ); + $id = 'test' . $status_id; + $content = '@abc Test ' . $date . ' ' . wp_rand(); + + $request = new \WP_REST_Request( 'POST', '/activitypub/1.0/users/' . get_current_user_id() . '/inbox' ); + $request->set_param( 'type', 'Create' ); + $request->set_param( 'id', $id ); + $request->set_param( 'actor', $this->actor ); + + $request->set_param( + 'object', + array( + 'type' => 'Note', + 'id' => $id, + 'attributedTo' => $this->actor, + 'content' => $content, + 'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ), + 'published' => $date, + ) + ); + + $response = $this->server->dispatch( $request ); + $this->assertEquals( 202, $response->get_status() ); + + $posts = get_posts( + array( + 'post_type' => \Friends\Friends::CPT, + 'author' => $this->friend_id, + 'post_status' => 'trash', + ) + ); + + $this->assertEquals( $post_count + 1, count( $posts ) ); + $this->assertStringStartsWith( $content, $posts[0]->post_content ); + $this->assertEquals( $this->friend_id, $posts[0]->post_author ); } public function test_incoming_announce() { @@ -159,11 +214,10 @@ class Test_Friends_Feed_Parser_ActivityPub extends ActivityPub_TestCase_Cache_HT $this->assertStringContainsString( 'Dezentrale Netzwerke', $posts[0]->post_content ); $this->assertEquals( $this->friend_id, $posts[0]->post_author ); $this->assertEquals( 'Matthias Pfefferle', get_post_meta( $posts[0]->ID, 'author', true ) ); - } public function test_friend_mentions() { - add_filter( 'friends_cache_possible_mentions', '__return_false' ); + add_filter( 'activitypub_cache_possible_friend_mentions', '__return_false' ); $post = \wp_insert_post( array( 'post_author' => 1,