| @@ -6,17 +6,14 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Activitypub\Handler; |
| 9 | 9 | |
| 10 | +use Activitypub\Notification; | |
| 10 | 11 | use Activitypub\Activity\Activity; |
| 11 | -use Activitypub\Application; | |
| 12 | 12 | use Activitypub\Collection\Actors; |
| 13 | 13 | use Activitypub\Collection\Followers; |
| 14 | -use Activitypub\Collection\Remote_Actors; | |
| 15 | -use Activitypub\Http; | |
| 16 | 14 | |
| 17 | 15 | use function Activitypub\add_to_outbox; |
| 18 | -use function Activitypub\object_to_uri; | |
| 19 | 16 | |
| 20 | 17 | /** |
| 21 | 18 | * Handle Follow requests. |
| 22 | 19 | */ |
| @@ -24,94 +21,86 @@ | ||
| 24 | 21 | /** |
| 25 | 22 | * Initialize the class, registering WordPress hooks. |
| 26 | 23 | */ |
| 27 | 24 | public static function init() { |
| 28 | - \add_action( 'activitypub_inbox_follow', array( self::class, 'handle_follow' ), 10, 2 ); | |
| 29 | - \add_action( 'activitypub_handled_follow', array( self::class, 'queue_accept' ), 10, 4 ); | |
| 25 | + \add_action( | |
| 26 | + 'activitypub_inbox_follow', | |
| 27 | + array( self::class, 'handle_follow' ) | |
| 28 | + ); | |
| 30 | 29 | |
| 31 | - // The Application actor cannot be followed; explicitly reject such Follows so they don't sit "pending" on the remote instance forever. | |
| 32 | - \add_action( 'activitypub_inbox_shared_follow', array( self::class, 'reject_application_follow' ), 10, 2 ); | |
| 30 | + \add_action( | |
| 31 | + 'activitypub_followers_post_follow', | |
| 32 | + array( self::class, 'queue_accept' ), | |
| 33 | + 10, | |
| 34 | + 4 | |
| 35 | + ); | |
| 33 | 36 | } |
| 34 | 37 | |
| 35 | 38 | /** |
| 36 | 39 | * Handle "Follow" requests. |
| 37 | 40 | * |
| 38 | - * @param array $activity The activity object. | |
| 39 | - * @param int|int[] $user_ids The user ID(s). | |
| 41 | + * @param array $activity The activity object. | |
| 40 | 42 | */ |
| 41 | - public static function handle_follow( $activity, $user_ids ) { | |
| 42 | - // Extract the user ID (follow requests are always for a single user). | |
| 43 | - $user_id = \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids; | |
| 43 | + public static function handle_follow( $activity ) { | |
| 44 | + $user = Actors::get_by_resource( $activity['object'] ); | |
| 44 | 45 | |
| 45 | - // Check if the actor already follows the user. | |
| 46 | - $already_following = false; | |
| 47 | - $remote_actor = Remote_Actors::get_by_uri( $activity['actor'] ); | |
| 48 | - if ( ! \is_wp_error( $remote_actor ) ) { | |
| 49 | - $already_following = Followers::follows( $remote_actor->ID, $user_id ); | |
| 46 | + if ( ! $user || is_wp_error( $user ) ) { | |
| 47 | + // If we can not find a user, we can not initiate a follow process. | |
| 48 | + return; | |
| 50 | 49 | } |
| 51 | 50 | |
| 52 | - // Save follower if not already following. | |
| 53 | - if ( $already_following ) { | |
| 54 | - $success = false; | |
| 55 | - } else { | |
| 56 | - $remote_actor = Followers::add( $user_id, $activity['actor'] ); | |
| 57 | - $success = ! \is_wp_error( $remote_actor ); | |
| 51 | + $user_id = $user->get__id(); | |
| 58 | 52 | |
| 59 | - if ( $success ) { | |
| 60 | - $remote_actor = \get_post( $remote_actor ); | |
| 61 | - } | |
| 62 | - } | |
| 53 | + // Save follower. | |
| 54 | + $follower = Followers::add_follower( | |
| 55 | + $user_id, | |
| 56 | + $activity['actor'] | |
| 57 | + ); | |
| 63 | 58 | |
| 64 | 59 | /** |
| 65 | 60 | * Fires after a new follower has been added. |
| 66 | 61 | * |
| 67 | - * @deprecated 7.5.0 Use "activitypub_handled_follow" instead. | |
| 68 | - * | |
| 69 | - * @param string $actor The URL of the actor (follower) who initiated the follow. | |
| 70 | - * @param array $activity The complete activity data of the follow request. | |
| 71 | - * @param int $user_id The ID of the WordPress user being followed. | |
| 72 | - * @param \WP_Post|\WP_Error $remote_actor The Actor object containing the new follower's data. | |
| 62 | + * @param string $actor The URL of the actor (follower) who initiated the follow. | |
| 63 | + * @param array $activity The complete activity data of the follow request. | |
| 64 | + * @param int $user_id The ID of the WordPress user being followed. | |
| 65 | + * @param \Activitypub\Model\Follower|\WP_Error $follower The Follower object containing the new follower's data. | |
| 73 | 66 | */ |
| 74 | - \do_action_deprecated( 'activitypub_followers_post_follow', array( $activity['actor'], $activity, $user_id, $remote_actor ), '7.5.0', 'activitypub_handled_follow' ); | |
| 67 | + do_action( 'activitypub_followers_post_follow', $activity['actor'], $activity, $user_id, $follower ); | |
| 75 | 68 | |
| 76 | - /** | |
| 77 | - * Fires after a Follow activity has been handled. | |
| 78 | - * | |
| 79 | - * @param array $activity The ActivityPub activity data. | |
| 80 | - * @param int[] $user_ids The local user IDs. | |
| 81 | - * @param bool $success True on success, false otherwise. | |
| 82 | - * @param \WP_Post|\WP_Error $remote_actor The remote actor/follower, or WP_Error if failed. | |
| 83 | - */ | |
| 84 | - \do_action( 'activitypub_handled_follow', $activity, (array) $user_ids, $success, $remote_actor ); | |
| 69 | + // Send notification. | |
| 70 | + $notification = new Notification( | |
| 71 | + 'follow', | |
| 72 | + $activity['actor'], | |
| 73 | + $activity, | |
| 74 | + $user_id | |
| 75 | + ); | |
| 76 | + $notification->send(); | |
| 85 | 77 | } |
| 86 | 78 | |
| 87 | 79 | /** |
| 88 | 80 | * Send Accept response. |
| 89 | 81 | * |
| 90 | - * @param array $activity_object The ActivityPub activity data. | |
| 91 | - * @param int|int[] $user_ids The local user IDs. | |
| 92 | - * @param bool $success True on success, false otherwise. | |
| 93 | - * @param \WP_Post|\WP_Error $remote_actor The remote actor/follower, or WP_Error if failed. | |
| 82 | + * @param string $actor The Actor URL. | |
| 83 | + * @param array $activity_object The Activity object. | |
| 84 | + * @param int $user_id The ID of the WordPress User. | |
| 85 | + * @param \Activitypub\Model\Follower|\WP_Error $follower The Follower object. | |
| 94 | 86 | */ |
| 95 | - public static function queue_accept( $activity_object, $user_ids, $success, $remote_actor ) { | |
| 96 | - if ( \is_wp_error( $remote_actor ) ) { | |
| 87 | + public static function queue_accept( $actor, $activity_object, $user_id, $follower ) { | |
| 88 | + if ( \is_wp_error( $follower ) ) { | |
| 97 | 89 | // Impossible to send a "Reject" because we can not get the Remote-Inbox. |
| 98 | 90 | return; |
| 99 | 91 | } |
| 100 | 92 | |
| 101 | - // Extract the user ID from the array (follow requests are always for a single user). | |
| 102 | - $user_id = \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids; | |
| 103 | - | |
| 104 | - $actor = $activity_object['actor']; | |
| 105 | - | |
| 106 | 93 | // Only send minimal data. |
| 107 | - $activity_object = \array_intersect_key( | |
| 94 | + $activity_object = array_intersect_key( | |
| 108 | 95 | $activity_object, |
| 109 | - array( | |
| 110 | - 'id' => 1, | |
| 111 | - 'type' => 1, | |
| 112 | - 'actor' => 1, | |
| 113 | - 'object' => 1, | |
| 96 | + array_flip( | |
| 97 | + array( | |
| 98 | + 'id', | |
| 99 | + 'type', | |
| 100 | + 'actor', | |
| 101 | + 'object', | |
| 102 | + ) | |
| 114 | 103 | ) |
| 115 | 104 | ); |
| 116 | 105 | |
| 117 | 106 | $activity = new Activity(); |
| @@ -120,63 +109,6 @@ | ||
| 120 | 109 | $activity->set_object( $activity_object ); |
| 121 | 110 | $activity->set_to( array( $actor ) ); |
| 122 | 111 | |
| 123 | 112 | add_to_outbox( $activity, null, $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE ); |
| 124 | - } | |
| 125 | - | |
| 126 | - /** | |
| 127 | - * Reject Follow requests aimed at the Application actor. | |
| 128 | - * | |
| 129 | - * The Application advertises `manuallyApprovesFollowers` but is not followable, | |
| 130 | - * so an explicit Reject is the only way a remote follow request gets resolved. | |
| 131 | - * The Reject is sent directly instead of through the Outbox, which only | |
| 132 | - * dispatches for real actors, and is signed with the Application key. | |
| 133 | - * | |
| 134 | - * @since 9.1.0 | |
| 135 | - * | |
| 136 | - * @param array $activity The Follow activity data. | |
| 137 | - * @param int[] $user_ids The local recipient IDs the inbox resolved. | |
| 138 | - */ | |
| 139 | - public static function reject_application_follow( $activity, $user_ids ) { | |
| 140 | - // A resolved recipient means the Follow targets a real actor, not the Application. | |
| 141 | - if ( ! empty( $user_ids ) ) { | |
| 142 | - return; | |
| 143 | - } | |
| 144 | - | |
| 145 | - if ( empty( $activity['object'] ) || ! Application::is_application_resource( object_to_uri( $activity['object'] ) ) ) { | |
| 146 | - return; | |
| 147 | - } | |
| 148 | - | |
| 149 | - $actor = object_to_uri( $activity['actor'] ); | |
| 150 | - $remote_actor = Remote_Actors::fetch_by_uri( $actor ); | |
| 151 | - | |
| 152 | - if ( \is_wp_error( $remote_actor ) ) { | |
| 153 | - return; | |
| 154 | - } | |
| 155 | - | |
| 156 | - $inbox = \get_post_meta( $remote_actor->ID, '_activitypub_inbox', true ); | |
| 157 | - | |
| 158 | - if ( ! $inbox ) { | |
| 159 | - return; | |
| 160 | - } | |
| 161 | - | |
| 162 | - // Only send minimal data. | |
| 163 | - $origin_activity = \array_intersect_key( | |
| 164 | - $activity, | |
| 165 | - array( | |
| 166 | - 'id' => 1, | |
| 167 | - 'type' => 1, | |
| 168 | - 'actor' => 1, | |
| 169 | - 'object' => 1, | |
| 170 | - ) | |
| 171 | - ); | |
| 172 | - | |
| 173 | - $reject = new Activity(); | |
| 174 | - $reject->set_type( 'Reject' ); | |
| 175 | - $reject->set_id( Application::get_id() . '#reject-' . \md5( \wp_json_encode( $origin_activity ) ) ); | |
| 176 | - $reject->set_actor( Application::get_id() ); | |
| 177 | - $reject->set_object( $origin_activity ); | |
| 178 | - $reject->set_to( array( $actor ) ); | |
| 179 | - | |
| 180 | - Http::post( $inbox, $reject->to_json(), null ); | |
| 181 | 113 | } |
| 182 | 114 | } |