From 5c59834a0c622bcb841450afa425acf28b01ebcb Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 5 Jul 2023 15:34:22 +0200 Subject: [PATCH] various fixes mainly regarding `send_follow_response` --- includes/collection/class-followers.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index 5b67a7f..c63a28c 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -7,7 +7,9 @@ use WP_Query; use Activitypub\Http; use Activitypub\Webfinger; use Activitypub\Model\Follower; +use Activitypub\Collection\Users; use Activitypub\Activity\Activity; +use Activitypub\Activity\Base_Object; use function Activitypub\is_tombstone; use function Activitypub\get_remote_metadata_by_actor; @@ -131,7 +133,7 @@ class Followers { '_actor', array( 'type' => 'string', - 'single' => false, + 'single' => true, 'sanitize_callback' => function( $value ) { return esc_sql( $value ); }, @@ -191,7 +193,7 @@ class Followers { public static function add_follower( $user_id, $actor ) { $meta = get_remote_metadata_by_actor( $actor ); - if ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) { + if ( is_tombstone( $meta ) ) { return $meta; } @@ -241,7 +243,7 @@ class Followers { $post_id = $wpdb->get_var( $wpdb->prepare( - "SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id WHERE p.post_type = %s AND pm.meta_key = '_user_id' AND pm.meta_value = %d AND p.guid = %s", + "SELECT DISTINCT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->postmeta pm ON p.ID = pm.post_id WHERE p.post_type = %s AND pm.meta_key = '_user_id' AND pm.meta_value = %d AND p.guid = %s", array( esc_sql( self::POST_TYPE ), esc_sql( $user_id ), @@ -280,17 +282,21 @@ class Followers { unset( $object['@context'] ); } + $user = Users::get_by_id( $user_id ); + // get inbox $inbox = $follower->get_inbox(); // send "Accept" activity - $activity = new Activity( 'Accept' ); - $activity->set_activity_object( $object ); - $activity->set_actor( \get_author_posts_url( $user_id ) ); + $activity = new Activity(); + $activity->set_type( 'Accept' ); + $activity->set_object( $object ); + $activity->set_actor( $user->get_id() ); $activity->set_to( $actor ); - $activity->set_id( \get_author_posts_url( $user_id ) . '#follow-' . \preg_replace( '~^https?://~', '', $actor ) ); + $activity->set_id( $user->get_id() . '#follow-' . \preg_replace( '~^https?://~', '', $actor ) ); + + $activity = $activity->to_json(); - $activity = $activity->to_simple_json(); $response = Http::post( $inbox, $activity, $user_id ); }