updated plugin ActivityPub version 8.3.0
This commit is contained in:
@ -7,10 +7,12 @@
|
||||
|
||||
namespace Activitypub\Handler;
|
||||
|
||||
use Activitypub\Collection\Followers;
|
||||
use Activitypub\Collection\Interactions;
|
||||
use Activitypub\Collection\Remote_Actors;
|
||||
use Activitypub\Collection\Remote_Posts;
|
||||
use Activitypub\Http;
|
||||
|
||||
use function Activitypub\get_remote_metadata_by_actor;
|
||||
use function Activitypub\is_activity_reply;
|
||||
|
||||
/**
|
||||
* Handle Update requests.
|
||||
@ -20,19 +22,18 @@ class Update {
|
||||
* Initialize the class, registering WordPress hooks.
|
||||
*/
|
||||
public static function init() {
|
||||
\add_action(
|
||||
'activitypub_inbox_update',
|
||||
array( self::class, 'handle_update' )
|
||||
);
|
||||
\add_action( 'activitypub_handled_inbox_update', array( self::class, 'handle_update' ), 10, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle "Update" requests.
|
||||
*
|
||||
* @param array $activity The Activity object.
|
||||
* @param array $activity The Activity object.
|
||||
* @param int[] $user_ids The user IDs. Always null for Update activities.
|
||||
* @param \Activitypub\Activity\Activity $activity_object The activity object. Default null.
|
||||
*/
|
||||
public static function handle_update( $activity ) {
|
||||
$object_type = isset( $activity['object']['type'] ) ? $activity['object']['type'] : '';
|
||||
public static function handle_update( $activity, $user_ids, $activity_object ) {
|
||||
$object_type = $activity['object']['type'] ?? '';
|
||||
|
||||
switch ( $object_type ) {
|
||||
/*
|
||||
@ -45,7 +46,7 @@ class Update {
|
||||
case 'Organization':
|
||||
case 'Service':
|
||||
case 'Application':
|
||||
self::update_actor( $activity );
|
||||
self::update_actor( $activity, $user_ids );
|
||||
break;
|
||||
|
||||
/*
|
||||
@ -60,7 +61,7 @@ class Update {
|
||||
case 'Video':
|
||||
case 'Event':
|
||||
case 'Document':
|
||||
self::update_interaction( $activity );
|
||||
self::update_object( $activity, $user_ids, $activity_object );
|
||||
break;
|
||||
|
||||
/*
|
||||
@ -74,52 +75,94 @@ class Update {
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an Interaction.
|
||||
* Update an Object.
|
||||
*
|
||||
* @param array $activity The Activity object.
|
||||
* @param array $activity The Activity object.
|
||||
* @param int[]|null $user_ids The user IDs. Always null for Update activities.
|
||||
* @param \Activitypub\Activity\Activity $activity_object The activity object. Default null.
|
||||
*/
|
||||
public static function update_interaction( $activity ) {
|
||||
$commentdata = Interactions::update_comment( $activity );
|
||||
$reaction = null;
|
||||
public static function update_object( $activity, $user_ids, $activity_object ) {
|
||||
$result = new \WP_Error( 'activitypub_update_failed', 'Update failed' );
|
||||
$updated = true;
|
||||
|
||||
if ( ! empty( $commentdata['comment_ID'] ) ) {
|
||||
$state = 1;
|
||||
$reaction = \get_comment( $commentdata['comment_ID'] );
|
||||
} else {
|
||||
$state = $commentdata;
|
||||
// Check for private and/or direct messages.
|
||||
if ( is_activity_reply( $activity ) ) {
|
||||
$comment_data = Interactions::update_comment( $activity );
|
||||
|
||||
if ( false === $comment_data ) {
|
||||
$updated = false;
|
||||
} elseif ( ! empty( $comment_data['comment_ID'] ) ) {
|
||||
$result = \get_comment( $comment_data['comment_ID'] );
|
||||
}
|
||||
} elseif ( \get_option( 'activitypub_create_posts', false ) ) {
|
||||
$result = Remote_Posts::update( $activity, $user_ids );
|
||||
|
||||
if ( \is_wp_error( $result ) && 'activitypub_post_not_found' === $result->get_error_code() ) {
|
||||
$updated = false;
|
||||
}
|
||||
}
|
||||
|
||||
// There is no object to update, try to trigger create instead.
|
||||
if ( ! $updated ) {
|
||||
return Create::handle_create( $activity, $user_ids, $activity_object );
|
||||
}
|
||||
|
||||
$success = ( $result && ! \is_wp_error( $result ) );
|
||||
|
||||
/**
|
||||
* Fires after an Update activity has been handled.
|
||||
* Fires after an ActivityPub Update activity has been handled.
|
||||
*
|
||||
* @param array $activity The complete Update activity data.
|
||||
* @param null $user Always null for Update activities.
|
||||
* @param int|array $state 1 if comment was updated successfully, error data otherwise.
|
||||
* @param \WP_Comment|null $reaction The updated comment object if successful, null otherwise.
|
||||
* @param array $activity The ActivityPub activity data.
|
||||
* @param int[]|null $user_ids The local user IDs.
|
||||
* @param bool $success True on success, false otherwise.
|
||||
* @param \WP_Comment|\WP_Post|\WP_Error $result The updated post, comment, or error.
|
||||
*/
|
||||
\do_action( 'activitypub_handled_update', $activity, null, $state, $reaction );
|
||||
\do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $success, $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an Actor.
|
||||
*
|
||||
* @param array $activity The Activity object.
|
||||
* @param array $activity The Activity object.
|
||||
* @param int[]|null $user_ids The user IDs. Always null for Update activities.
|
||||
*/
|
||||
public static function update_actor( $activity ) {
|
||||
// Update cache.
|
||||
$actor = get_remote_metadata_by_actor( $activity['actor'], false );
|
||||
public static function update_actor( $activity, $user_ids ) {
|
||||
/*
|
||||
* Prefer the actor data embedded in the activity object, as it contains
|
||||
* the fresh data sent by the remote server.
|
||||
*/
|
||||
$actor = $activity['object'] ?? null;
|
||||
|
||||
if ( ! $actor || \is_wp_error( $actor ) || ! isset( $actor['id'] ) ) {
|
||||
return;
|
||||
/*
|
||||
* The object may be a string IRI instead of an embedded object,
|
||||
* in which case we need to fetch the actor data remotely.
|
||||
* We use Http::get_remote_object() directly instead of
|
||||
* get_remote_metadata_by_actor() because the latter returns the
|
||||
* stale locally cached copy via fetch_by_uri().
|
||||
*/
|
||||
if ( ! \is_array( $actor ) || ! isset( $actor['id'] ) ) {
|
||||
$object = Http::get_remote_object( $activity['actor'], false );
|
||||
|
||||
if ( ! \is_wp_error( $object ) && \is_array( $object ) ) {
|
||||
$actor = $object;
|
||||
}
|
||||
}
|
||||
|
||||
$follower = Followers::get_follower_by_actor( $actor['id'] );
|
||||
|
||||
if ( ! $follower ) {
|
||||
return;
|
||||
if ( \is_array( $actor ) && isset( $actor['id'] ) ) {
|
||||
$state = Remote_Actors::upsert( $actor );
|
||||
} else {
|
||||
$state = new \WP_Error( 'activitypub_update_failed', 'Update failed: missing or invalid actor object in Update activity' );
|
||||
$actor = array();
|
||||
}
|
||||
|
||||
$follower->from_array( $actor );
|
||||
$follower->upsert();
|
||||
/**
|
||||
* Fires after an ActivityPub Update activity has been handled.
|
||||
*
|
||||
* @param array $activity The ActivityPub activity data.
|
||||
* @param int[] $user_ids The local user IDs.
|
||||
* @param int|\WP_Error $state Actor post ID on success, WP_Error on failure.
|
||||
* @param array $actor Remote actor meta data.
|
||||
*/
|
||||
\do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $state, $actor );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user