updated plugin ActivityPub version 8.3.0
This commit is contained in:
@ -7,8 +7,9 @@
|
||||
|
||||
namespace Activitypub\Handler;
|
||||
|
||||
use Activitypub\Http;
|
||||
use Activitypub\Collection\Followers;
|
||||
use Activitypub\Collection\Remote_Actors;
|
||||
use Activitypub\Http;
|
||||
|
||||
use function Activitypub\object_to_uri;
|
||||
|
||||
@ -24,98 +25,79 @@ class Move {
|
||||
* Initialize the class, registering WordPress hooks.
|
||||
*/
|
||||
public static function init() {
|
||||
\add_action( 'activitypub_inbox_move', array( self::class, 'handle_move' ) );
|
||||
\add_filter( 'activitypub_get_outbox_activity', array( self::class, 'outbox_activity' ) );
|
||||
\add_action( 'activitypub_inbox_move', array( self::class, 'handle_move' ), 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle Move requests.
|
||||
*
|
||||
* @param array $activity The JSON "Move" Activity.
|
||||
* @param array $activity The JSON "Move" Activity.
|
||||
* @param int|int[] $user_ids The user ID(s).
|
||||
*/
|
||||
public static function handle_move( $activity ) {
|
||||
$target = self::extract_target( $activity );
|
||||
$origin = self::extract_origin( $activity );
|
||||
public static function handle_move( $activity, $user_ids ) {
|
||||
$target_uri = self::extract_target( $activity );
|
||||
$origin_uri = self::extract_origin( $activity );
|
||||
|
||||
if ( ! $target || ! $origin ) {
|
||||
if ( ! $target_uri || ! $origin_uri ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$target_object = Http::get_remote_object( $target );
|
||||
$origin_object = Http::get_remote_object( $origin );
|
||||
$target_json = Http::get_remote_object( $target_uri );
|
||||
$origin_json = Http::get_remote_object( $origin_uri );
|
||||
|
||||
$verified = self::verify_move( $target_object, $origin_object );
|
||||
$verified = self::verify_move( $target_json, $origin_json );
|
||||
|
||||
if ( ! $verified ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$target_follower = Followers::get_follower_by_actor( $target );
|
||||
$origin_follower = Followers::get_follower_by_actor( $origin );
|
||||
|
||||
/*
|
||||
* If the new target is followed, but the origin is not,
|
||||
* everything is fine, so we can return.
|
||||
*/
|
||||
if ( $target_follower && ! $origin_follower ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* If the new target is not followed, but the origin is,
|
||||
* update the origin follower to the new target.
|
||||
*/
|
||||
if ( ! $target_follower && $origin_follower ) {
|
||||
$origin_follower->from_array( $target_object );
|
||||
$origin_follower->set_id( $target );
|
||||
$origin_id = $origin_follower->upsert();
|
||||
$target_object = Remote_Actors::get_by_uri( $target_uri );
|
||||
$origin_object = Remote_Actors::get_by_uri( $origin_uri );
|
||||
$result = null;
|
||||
$success = false;
|
||||
|
||||
// If the origin is followed but the target is not, update the origin to point to the target.
|
||||
if ( \is_wp_error( $target_object ) && ! \is_wp_error( $origin_object ) ) {
|
||||
global $wpdb;
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$wpdb->update(
|
||||
$wpdb->posts,
|
||||
array( 'guid' => sanitize_url( $target ) ),
|
||||
array( 'ID' => sanitize_key( $origin_id ) )
|
||||
array( 'guid' => sanitize_url( $target_uri ) ),
|
||||
array( 'ID' => sanitize_key( $origin_object->ID ) )
|
||||
);
|
||||
|
||||
// Clear the cache.
|
||||
wp_cache_delete( $origin_id, 'posts' );
|
||||
return;
|
||||
\wp_cache_delete( $origin_object->ID, 'posts' );
|
||||
|
||||
$success = true;
|
||||
$result = Remote_Actors::upsert( $target_json );
|
||||
}
|
||||
|
||||
/*
|
||||
* If the new target is followed, and the origin is followed,
|
||||
* move users and delete the origin follower.
|
||||
*/
|
||||
if ( $target_follower && $origin_follower ) {
|
||||
$origin_users = \get_post_meta( $origin_follower->get__id(), '_activitypub_user_id', false );
|
||||
$target_users = \get_post_meta( $target_follower->get__id(), '_activitypub_user_id', false );
|
||||
// If both the target and origin are followed, merge them.
|
||||
if ( ! \is_wp_error( $target_object ) && ! \is_wp_error( $origin_object ) ) {
|
||||
$origin_users = \get_post_meta( $origin_object->ID, Followers::FOLLOWER_META_KEY, false );
|
||||
$target_users = \get_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, false );
|
||||
|
||||
// Get all user ids from $origin_users that are not in $target_users.
|
||||
$users = \array_diff( $origin_users, $target_users );
|
||||
|
||||
foreach ( $users as $user_id ) {
|
||||
\add_post_meta( $target_follower->get__id(), '_activitypub_user_id', $user_id );
|
||||
foreach ( $users as $follower_user_id ) {
|
||||
\add_post_meta( $target_object->ID, Followers::FOLLOWER_META_KEY, $follower_user_id );
|
||||
}
|
||||
|
||||
$origin_follower->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the object and origin to the correct format.
|
||||
*
|
||||
* @param \Activitypub\Activity\Activity $activity The Activity object.
|
||||
* @return \Activitypub\Activity\Activity The filtered Activity object.
|
||||
*/
|
||||
public static function outbox_activity( $activity ) {
|
||||
if ( 'Move' === $activity->get_type() ) {
|
||||
$activity->set_object( object_to_uri( $activity->get_object() ) );
|
||||
$activity->set_origin( $activity->get_actor() );
|
||||
$activity->set_target( $activity->get_object() );
|
||||
$success = true;
|
||||
$result = \wp_delete_post( $origin_object->ID );
|
||||
}
|
||||
|
||||
return $activity;
|
||||
/**
|
||||
* Fires after an ActivityPub Move activity has been handled.
|
||||
*
|
||||
* @param array $activity The ActivityPub activity data.
|
||||
* @param int[] $user_ids The local user IDs.
|
||||
* @param bool $success True on success, false otherwise.
|
||||
* @param mixed $result The result of the operation (e.g., post ID, WP_Error, or status).
|
||||
*/
|
||||
\do_action( 'activitypub_handled_move', $activity, (array) $user_ids, $success, $result );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -188,13 +170,23 @@ class Move {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if the target has an alsoKnownAs property.
|
||||
if ( empty( $target_object['also_known_as'] ) ) {
|
||||
// Normalize alsoKnownAs to an array (some JSON-LD payloads may use a string).
|
||||
$also_known_as = (array) ( $target_object['alsoKnownAs'] ?? array() );
|
||||
if ( empty( $also_known_as ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if the origin is in the alsoKnownAs property of the target.
|
||||
if ( ! in_array( $origin_object['id'], $target_object['also_known_as'], true ) ) {
|
||||
// Collect all possible origin identifiers (id, url, webfinger).
|
||||
$origin_ids = array_filter(
|
||||
array(
|
||||
$origin_object['id'] ?? null,
|
||||
$origin_object['url'] ?? null,
|
||||
$origin_object['webfinger'] ?? null,
|
||||
)
|
||||
);
|
||||
|
||||
// Check if any origin identifier is in the alsoKnownAs property of the target.
|
||||
if ( ! array_intersect( $origin_ids, $also_known_as ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user