| @@ -1,151 +1,119 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Follow handler file. | |
| 4 | - * | |
| 5 | - * @package Activitypub | |
| 6 | - */ | |
| 7 | - | |
| 8 | 2 | namespace Activitypub\Handler; |
| 9 | 3 | |
| 4 | +use Activitypub\Http; | |
| 5 | +use Activitypub\Notification; | |
| 10 | 6 | use Activitypub\Activity\Activity; |
| 11 | -use Activitypub\Collection\Actors; | |
| 7 | +use Activitypub\Collection\Users; | |
| 12 | 8 | use Activitypub\Collection\Followers; |
| 13 | -use Activitypub\Collection\Remote_Actors; | |
| 14 | 9 | |
| 15 | -use function Activitypub\add_to_outbox; | |
| 16 | - | |
| 17 | 10 | /** |
| 18 | - * Handle Follow requests. | |
| 11 | + * Handle Follow requests | |
| 19 | 12 | */ |
| 20 | 13 | class Follow { |
| 21 | 14 | /** |
| 22 | - * Initialize the class, registering WordPress hooks. | |
| 15 | + * Initialize the class, registering WordPress hooks | |
| 23 | 16 | */ |
| 24 | 17 | public static function init() { |
| 25 | - \add_action( 'activitypub_inbox_follow', array( self::class, 'handle_follow' ), 10, 2 ); | |
| 26 | - \add_action( 'activitypub_handled_follow', array( self::class, 'queue_accept' ), 10, 4 ); | |
| 18 | + \add_action( | |
| 19 | + 'activitypub_inbox_follow', | |
| 20 | + array( self::class, 'handle_follow' ) | |
| 21 | + ); | |
| 22 | + | |
| 23 | + \add_action( | |
| 24 | + 'activitypub_followers_post_follow', | |
| 25 | + array( self::class, 'send_follow_response' ), | |
| 26 | + 10, | |
| 27 | + 4 | |
| 28 | + ); | |
| 27 | 29 | } |
| 28 | 30 | |
| 29 | 31 | /** |
| 30 | - * Handle "Follow" requests. | |
| 32 | + * Handle "Follow" requests | |
| 31 | 33 | * |
| 32 | - * @param array $activity The activity object. | |
| 33 | - * @param int|int[] $user_ids The user ID(s). | |
| 34 | + * @param array $activity The activity object | |
| 35 | + * @param int $user_id The user ID | |
| 34 | 36 | */ |
| 35 | - public static function handle_follow( $activity, $user_ids ) { | |
| 36 | - // Extract the user ID (follow requests are always for a single user). | |
| 37 | - $user_id = \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids; | |
| 37 | + public static function handle_follow( $activity ) { | |
| 38 | + $user = Users::get_by_resource( $activity['object'] ); | |
| 38 | 39 | |
| 39 | - if ( Actors::APPLICATION_USER_ID === $user_id ) { | |
| 40 | - self::queue_reject( $activity, $user_id ); | |
| 40 | + if ( ! $user || is_wp_error( $user ) ) { | |
| 41 | + // If we can not find a user, | |
| 42 | + // we can not initiate a follow process | |
| 41 | 43 | return; |
| 42 | 44 | } |
| 43 | 45 | |
| 44 | - // Check if the actor already follows the user. | |
| 45 | - $already_following = false; | |
| 46 | - $remote_actor = Remote_Actors::get_by_uri( $activity['actor'] ); | |
| 47 | - if ( ! \is_wp_error( $remote_actor ) ) { | |
| 48 | - $already_following = Followers::follows( $remote_actor->ID, $user_id ); | |
| 49 | - } | |
| 46 | + $user_id = $user->get__id(); | |
| 50 | 47 | |
| 51 | - // Save follower if not already following. | |
| 52 | - if ( $already_following ) { | |
| 53 | - $success = false; | |
| 54 | - } else { | |
| 55 | - $remote_actor = Followers::add( $user_id, $activity['actor'] ); | |
| 56 | - $success = ! \is_wp_error( $remote_actor ); | |
| 48 | + // save follower | |
| 49 | + $follower = Followers::add_follower( | |
| 50 | + $user_id, | |
| 51 | + $activity['actor'] | |
| 52 | + ); | |
| 57 | 53 | |
| 58 | - if ( $success ) { | |
| 59 | - $remote_actor = \get_post( $remote_actor ); | |
| 60 | - } | |
| 61 | - } | |
| 54 | + do_action( | |
| 55 | + 'activitypub_followers_post_follow', | |
| 56 | + $activity['actor'], | |
| 57 | + $activity, | |
| 58 | + $user_id, | |
| 59 | + $follower | |
| 60 | + ); | |
| 62 | 61 | |
| 63 | - /** | |
| 64 | - * Fires after a new follower has been added. | |
| 65 | - * | |
| 66 | - * @deprecated 7.5.0 Use "activitypub_handled_follow" instead. | |
| 67 | - * | |
| 68 | - * @param string $actor The URL of the actor (follower) who initiated the follow. | |
| 69 | - * @param array $activity The complete activity data of the follow request. | |
| 70 | - * @param int $user_id The ID of the WordPress user being followed. | |
| 71 | - * @param \WP_Post|\WP_Error $remote_actor The Actor object containing the new follower's data. | |
| 72 | - */ | |
| 73 | - \do_action_deprecated( 'activitypub_followers_post_follow', array( $activity['actor'], $activity, $user_id, $remote_actor ), '7.5.0', 'activitypub_handled_follow' ); | |
| 74 | - | |
| 75 | - /** | |
| 76 | - * Fires after a Follow activity has been handled. | |
| 77 | - * | |
| 78 | - * @param array $activity The ActivityPub activity data. | |
| 79 | - * @param int[] $user_ids The local user IDs. | |
| 80 | - * @param bool $success True on success, false otherwise. | |
| 81 | - * @param \WP_Post|\WP_Error $remote_actor The remote actor/follower, or WP_Error if failed. | |
| 82 | - */ | |
| 83 | - \do_action( 'activitypub_handled_follow', $activity, (array) $user_ids, $success, $remote_actor ); | |
| 62 | + // send notification | |
| 63 | + $notification = new Notification( | |
| 64 | + 'follow', | |
| 65 | + $activity['actor'], | |
| 66 | + $activity, | |
| 67 | + $user_id | |
| 68 | + ); | |
| 69 | + $notification->send(); | |
| 84 | 70 | } |
| 85 | 71 | |
| 86 | 72 | /** |
| 87 | - * Send Accept response. | |
| 73 | + * Send Accept response | |
| 88 | 74 | * |
| 89 | - * @param array $activity_object The ActivityPub activity data. | |
| 90 | - * @param int|int[] $user_ids The local user IDs. | |
| 91 | - * @param bool $success True on success, false otherwise. | |
| 92 | - * @param \WP_Post|\WP_Error $remote_actor The remote actor/follower, or WP_Error if failed. | |
| 75 | + * @param string $actor The Actor URL | |
| 76 | + * @param array $object The Activity object | |
| 77 | + * @param int $user_id The ID of the WordPress User | |
| 78 | + * @param Activitypub\Model\Follower $follower The Follower object | |
| 79 | + * | |
| 80 | + * @return void | |
| 93 | 81 | */ |
| 94 | - public static function queue_accept( $activity_object, $user_ids, $success, $remote_actor ) { | |
| 95 | - if ( \is_wp_error( $remote_actor ) ) { | |
| 96 | - // Impossible to send a "Reject" because we can not get the Remote-Inbox. | |
| 82 | + public static function send_follow_response( $actor, $object, $user_id, $follower ) { | |
| 83 | + if ( \is_wp_error( $follower ) ) { | |
| 84 | + // it is not even possible to send a "Reject" because | |
| 85 | + // we can not get the Remote-Inbox | |
| 97 | 86 | return; |
| 98 | 87 | } |
| 99 | 88 | |
| 100 | - // Extract the user ID from the array (follow requests are always for a single user). | |
| 101 | - $user_id = \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids; | |
| 89 | + // only send minimal data | |
| 90 | + $object = array_intersect_key( | |
| 91 | + $object, | |
| 92 | + array_flip( | |
| 93 | + array( | |
| 94 | + 'id', | |
| 95 | + 'type', | |
| 96 | + 'actor', | |
| 97 | + 'object', | |
| 98 | + ) | |
| 99 | + ) | |
| 100 | + ); | |
| 102 | 101 | |
| 103 | - $actor = $activity_object['actor']; | |
| 102 | + $user = Users::get_by_id( $user_id ); | |
| 104 | 103 | |
| 105 | - // Only send minimal data. | |
| 106 | - $activity_object = array_intersect_key( | |
| 107 | - $activity_object, | |
| 108 | - array( | |
| 109 | - 'id' => 1, | |
| 110 | - 'type' => 1, | |
| 111 | - 'actor' => 1, | |
| 112 | - 'object' => 1, | |
| 113 | - ) | |
| 114 | - ); | |
| 104 | + // get inbox | |
| 105 | + $inbox = $follower->get_shared_inbox(); | |
| 115 | 106 | |
| 107 | + // send "Accept" activity | |
| 116 | 108 | $activity = new Activity(); |
| 117 | 109 | $activity->set_type( 'Accept' ); |
| 118 | - $activity->set_actor( Actors::get_by_id( $user_id )->get_id() ); | |
| 119 | - $activity->set_object( $activity_object ); | |
| 120 | - $activity->set_to( array( $actor ) ); | |
| 110 | + $activity->set_object( $object ); | |
| 111 | + $activity->set_actor( $user->get_id() ); | |
| 112 | + $activity->set_to( $actor ); | |
| 113 | + $activity->set_id( $user->get_id() . '#follow-' . \preg_replace( '~^https?://~', '', $actor ) . '-' . \time() ); | |
| 121 | 114 | |
| 122 | - add_to_outbox( $activity, null, $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE ); | |
| 123 | - } | |
| 115 | + $activity = $activity->to_json(); | |
| 124 | 116 | |
| 125 | - /** | |
| 126 | - * Send Reject response. | |
| 127 | - * | |
| 128 | - * @param array $activity The Activity array. | |
| 129 | - * @param int $user_id The ID of the WordPress User. | |
| 130 | - */ | |
| 131 | - public static function queue_reject( $activity, $user_id ) { | |
| 132 | - // Only send minimal data. | |
| 133 | - $origin_activity = array_intersect_key( | |
| 134 | - $activity, | |
| 135 | - array( | |
| 136 | - 'id' => 1, | |
| 137 | - 'type' => 1, | |
| 138 | - 'actor' => 1, | |
| 139 | - 'object' => 1, | |
| 140 | - ) | |
| 141 | - ); | |
| 142 | - | |
| 143 | - $activity = new Activity(); | |
| 144 | - $activity->set_type( 'Reject' ); | |
| 145 | - $activity->set_actor( Actors::get_by_id( $user_id )->get_id() ); | |
| 146 | - $activity->set_object( $origin_activity ); | |
| 147 | - $activity->set_to( array( $origin_activity['actor'] ) ); | |
| 148 | - | |
| 149 | - add_to_outbox( $activity, null, $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE ); | |
| 117 | + Http::post( $inbox, $activity, $user_id ); | |
| 150 | 118 | } |
| 151 | 119 | } |