| @@ -2,9 +2,12 @@ | ||
| 2 | 2 | namespace Activitypub\Handler; |
| 3 | 3 | |
| 4 | 4 | use Activitypub\Collection\Users; |
| 5 | 5 | use Activitypub\Collection\Followers; |
| 6 | +use Activitypub\Comment; | |
| 6 | 7 | |
| 8 | +use function Activitypub\object_to_uri; | |
| 9 | + | |
| 7 | 10 | /** |
| 8 | 11 | * Handle Undo requests |
| 9 | 12 | */ |
| 10 | 13 | class Undo { |
| @@ -25,15 +28,21 @@ | ||
| 25 | 28 | * @param int $user_id The ID of the ID of the WordPress User |
| 26 | 29 | */ |
| 27 | 30 | public static function handle_undo( $activity ) { |
| 28 | 31 | 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 ) | |
| 32 | + ! isset( $activity['object']['type'] ) || | |
| 33 | + ! isset( $activity['object']['object'] ) | |
| 33 | 34 | ) { |
| 34 | - $user = Users::get_by_resource( $activity['object']['object'] ); | |
| 35 | + return; | |
| 36 | + } | |
| 35 | 37 | |
| 38 | + $type = $activity['object']['type']; | |
| 39 | + | |
| 40 | + // Handle "Unfollow" requests | |
| 41 | + if ( 'Follow' === $type ) { | |
| 42 | + $user_id = object_to_uri( $activity['object']['object'] ); | |
| 43 | + $user = Users::get_by_resource( $user_id ); | |
| 44 | + | |
| 36 | 45 | if ( ! $user || is_wp_error( $user ) ) { |
| 37 | 46 | // If we can not find a user, |
| 38 | 47 | // we can not initiate a follow process |
| 39 | 48 | return; |
| @@ -39,9 +48,28 @@ | ||
| 39 | 48 | return; |
| 40 | 49 | } |
| 41 | 50 | |
| 42 | 51 | $user_id = $user->get__id(); |
| 52 | + $actor = object_to_uri( $activity['actor'] ); | |
| 43 | 53 | |
| 44 | - Followers::remove_follower( $user_id, $activity['actor'] ); | |
| 54 | + Followers::remove_follower( $user_id, $actor ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + // Handle "Undo" requests for "Like" and "Create" activities | |
| 58 | + if ( in_array( $type, array( 'Like', 'Create', 'Announce' ), true ) ) { | |
| 59 | + if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) { | |
| 60 | + return; | |
| 61 | + } | |
| 62 | + | |
| 63 | + $object_id = object_to_uri( $activity['object'] ); | |
| 64 | + $comment = Comment::object_id_to_comment( esc_url_raw( $object_id ) ); | |
| 65 | + | |
| 66 | + if ( empty( $comment ) ) { | |
| 67 | + return; | |
| 68 | + } | |
| 69 | + | |
| 70 | + $state = wp_trash_comment( $comment ); | |
| 71 | + | |
| 72 | + do_action( 'activitypub_handled_undo', $activity, $user_id, isset( $state ) ? $state : null, null ); | |
| 45 | 73 | } |
| 46 | 74 | } |
| 47 | 75 | } |