| @@ -1,47 +1,85 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Undo handler file. | |
| 4 | + * | |
| 5 | + * @package Activitypub | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace Activitypub\Handler; |
| 3 | 9 | |
| 4 | -use Activitypub\Collection\Users; | |
| 5 | -use Activitypub\Collection\Followers; | |
| 10 | +use Activitypub\Collection\Inbox as Inbox_Collection; | |
| 6 | 11 | |
| 12 | +use function Activitypub\object_to_uri; | |
| 13 | + | |
| 7 | 14 | /** |
| 8 | - * Handle Undo requests | |
| 15 | + * Handle Undo requests. | |
| 9 | 16 | */ |
| 10 | 17 | class Undo { |
| 11 | 18 | /** |
| 12 | - * Initialize the class, registering WordPress hooks | |
| 19 | + * Initialize the class, registering WordPress hooks. | |
| 13 | 20 | */ |
| 14 | 21 | public static function init() { |
| 15 | - \add_action( | |
| 16 | - 'activitypub_inbox_undo', | |
| 17 | - array( self::class, 'handle_undo' ) | |
| 18 | - ); | |
| 22 | + \add_action( 'activitypub_inbox_undo', array( self::class, 'handle_undo' ), 10, 2 ); | |
| 23 | + \add_action( 'activitypub_validate_object', array( self::class, 'validate_object' ), 10, 3 ); | |
| 19 | 24 | } |
| 20 | 25 | |
| 21 | 26 | /** |
| 22 | - * Handle "Unfollow" requests | |
| 27 | + * Handle "Unfollow" requests. | |
| 23 | 28 | * |
| 24 | - * @param array $activity The JSON "Undo" Activity | |
| 25 | - * @param int $user_id The ID of the ID of the WordPress User | |
| 29 | + * @param array $activity The JSON "Undo" Activity. | |
| 30 | + * @param int|int[]|null $user_ids The user ID(s). | |
| 26 | 31 | */ |
| 27 | - public static function handle_undo( $activity ) { | |
| 28 | - if ( | |
| 29 | - isset( $activity['object']['type'] ) && | |
| 30 | - 'Follow' === $activity['object']['type'] && | |
| 31 | - isset( $activity['object']['object'] ) && | |
| 32 | - filter_var( $activity['object']['object'], FILTER_VALIDATE_URL ) | |
| 33 | - ) { | |
| 34 | - $user = Users::get_by_resource( $activity['object']['object'] ); | |
| 32 | + public static function handle_undo( $activity, $user_ids ) { | |
| 33 | + $success = false; | |
| 34 | + $result = Inbox_Collection::undo( object_to_uri( $activity['object'] ) ); | |
| 35 | 35 | |
| 36 | - if ( ! $user || is_wp_error( $user ) ) { | |
| 37 | - // If we can not find a user, | |
| 38 | - // we can not initiate a follow process | |
| 39 | - return; | |
| 40 | - } | |
| 36 | + if ( $result && ! \is_wp_error( $result ) ) { | |
| 37 | + $success = true; | |
| 38 | + } | |
| 41 | 39 | |
| 42 | - $user_id = $user->get__id(); | |
| 40 | + /** | |
| 41 | + * Fires after an ActivityPub Undo activity has been handled. | |
| 42 | + * | |
| 43 | + * @param array $activity The ActivityPub activity data. | |
| 44 | + * @param int[] $user_ids The local user IDs. | |
| 45 | + * @param bool $success True on success, false on failure. | |
| 46 | + * @param \WP_Comment|string $result The target, based on the activity that is being undone. | |
| 47 | + */ | |
| 48 | + \do_action( 'activitypub_handled_undo', $activity, (array) $user_ids, $success, $result ); | |
| 49 | + } | |
| 43 | 50 | |
| 44 | - Followers::remove_follower( $user_id, $activity['actor'] ); | |
| 51 | + /** | |
| 52 | + * Validate the object. | |
| 53 | + * | |
| 54 | + * @param bool $valid The validation state. | |
| 55 | + * @param string $param The object parameter. | |
| 56 | + * @param \WP_REST_Request $request The request object. | |
| 57 | + * | |
| 58 | + * @return bool The validation state: true if valid, false if not. | |
| 59 | + */ | |
| 60 | + public static function validate_object( $valid, $param, $request ) { | |
| 61 | + $activity = $request->get_json_params(); | |
| 62 | + | |
| 63 | + if ( empty( $activity['type'] ) ) { | |
| 64 | + return false; | |
| 45 | 65 | } |
| 66 | + | |
| 67 | + if ( 'Undo' !== $activity['type'] ) { | |
| 68 | + return $valid; | |
| 69 | + } | |
| 70 | + | |
| 71 | + if ( ! isset( $activity['actor'], $activity['object'] ) ) { | |
| 72 | + return false; | |
| 73 | + } | |
| 74 | + | |
| 75 | + if ( ! \is_array( $activity['object'] ) && ! \is_string( $activity['object'] ) ) { | |
| 76 | + return false; | |
| 77 | + } | |
| 78 | + | |
| 79 | + if ( \is_array( $activity['object'] ) && ! isset( $activity['object']['id'] ) ) { | |
| 80 | + return false; | |
| 81 | + } | |
| 82 | + | |
| 83 | + return $valid; | |
| 46 | 84 | } |
| 47 | 85 | } |