updated plugin ActivityPub version 3.3.3

This commit is contained in:
2024-10-09 12:44:17 +00:00
committed by Gitium
parent fb4b27bbc6
commit c54fa007bd
106 changed files with 7070 additions and 2918 deletions

View File

@ -1,16 +1,25 @@
<?php
/**
* Announce handler file.
*
* @package Activitypub
*/
namespace Activitypub\Handler;
use Activitypub\Http;
use Activitypub\Comment;
use Activitypub\Collection\Interactions;
use function Activitypub\object_to_uri;
use function Activitypub\is_activity_public;
/**
* Handle Create requests
* Handle Create requests.
*/
class Announce {
/**
* Initialize the class, registering WordPress hooks
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
@ -22,35 +31,27 @@ class Announce {
}
/**
* Handles "Announce" requests
* Handles "Announce" requests.
*
* @param array $array The activity-object
* @param int $user_id The id of the local blog-user
* @param Activitypub\Activity $activity The activity object
*
* @return void
* @param array $announcement The activity-object.
* @param int $user_id The id of the local blog-user.
* @param \Activitypub\Activity\Activity $activity The activity object.
*/
public static function handle_announce( $array, $user_id, $activity = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
return;
}
if ( ! isset( $array['object'] ) ) {
return;
}
// check if Activity is public or not
if ( ! is_activity_public( $array ) ) {
public static function handle_announce( $announcement, $user_id, $activity = null ) {
// Check if Activity is public or not.
if ( ! is_activity_public( $announcement ) ) {
// @todo maybe send email
return;
}
// @todo save the `Announce`-Activity itself
if ( ! ACTIVITYPUB_DISABLE_REACTIONS ) {
self::maybe_save_announce( $announcement, $user_id );
}
if ( is_string( $array['object'] ) ) {
$object = Http::get_remote_object( $array['object'] );
if ( is_string( $announcement['object'] ) ) {
$object = Http::get_remote_object( $announcement['object'] );
} else {
$object = $array['object'];
$object = $announcement['object'];
}
if ( ! $object || is_wp_error( $object ) ) {
@ -63,7 +64,58 @@ class Announce {
$type = \strtolower( $object['type'] );
/**
* Fires after an Announce has been received.
*
* @param array $object The object.
* @param int $user_id The id of the local blog-user.
* @param array $activity The activity object.
*/
\do_action( 'activitypub_inbox', $object, $user_id, $type, $activity );
/**
* Fires after an Announce of a specific type has been received.
*
* @param array $object The object.
* @param int $user_id The id of the local blog-user.
* @param array $activity The activity object.
*/
\do_action( "activitypub_inbox_{$type}", $object, $user_id, $activity );
}
/**
* Try to save the Announce.
*
* @param array $activity The activity-object.
* @param int $user_id The id of the local blog-user.
*/
public static function maybe_save_announce( $activity, $user_id ) {
$url = object_to_uri( $activity['object'] );
if ( empty( $url ) ) {
return;
}
$exists = Comment::object_id_to_comment( esc_url_raw( $url ) );
if ( $exists ) {
return;
}
$state = Interactions::add_reaction( $activity );
$reaction = null;
if ( $state && ! is_wp_error( $state ) ) {
$reaction = get_comment( $state );
}
/**
* Fires after an Announce has been saved.
*
* @param array $activity The activity-object.
* @param int $user_id The id of the local blog-user.
* @param mixed $state The state of the reaction.
* @param mixed $reaction The reaction.
*/
do_action( 'activitypub_handled_announce', $activity, $user_id, $state, $reaction );
}
}

View File

@ -1,18 +1,23 @@
<?php
/**
* Create handler file.
*
* @package Activitypub
*/
namespace Activitypub\Handler;
use WP_Error;
use Activitypub\Collection\Interactions;
use function Activitypub\is_activity_public;
use function Activitypub\object_id_to_comment;
/**
* Handle Create requests
* Handle Create requests.
*/
class Create {
/**
* Initialize the class, registering WordPress hooks
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
@ -21,50 +26,93 @@ class Create {
10,
3
);
\add_filter(
'activitypub_validate_object',
array( self::class, 'validate_object' ),
10,
3
);
}
/**
* Handles "Create" requests
* Handles "Create" requests.
*
* @param array $array The activity-object
* @param int $user_id The id of the local blog-user
* @param Activitypub\Activity $object The activity object
*
* @return void
* @param array $activity The activity-object.
* @param int $user_id The id of the local blog-user.
* @param \Activitypub\Activity\Activity $activity_object Optional. The activity object. Default null.
*/
public static function handle_create( $array, $user_id, $object = null ) {
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
public static function handle_create( $activity, $user_id, $activity_object = null ) {
// Check if Activity is public or not.
if ( ! is_activity_public( $activity ) ) {
// @todo maybe send email.
return;
}
if (
! isset( $array['object'] ) ||
! isset( $array['object']['id'] )
) {
return;
}
$check_dupe = object_id_to_comment( $activity['object']['id'] );
// check if Activity is public or not
if ( ! is_activity_public( $array ) ) {
// @todo maybe send email
return;
}
$check_dupe = object_id_to_comment( $array['object']['id'] );
// if comment exists, call update action
// If comment exists, call update action.
if ( $check_dupe ) {
\do_action( 'activitypub_inbox_update', $array, $user_id, $object );
/**
* Fires when a Create activity is received for an existing comment.
*
* @param array $activity The activity-object.
* @param int $user_id The id of the local blog-user.
* @param \WP_Comment|\WP_Error $check_dupe The comment object or WP_Error.
* @param \Activitypub\Activity\Activity $activity_object The activity object.
*/
\do_action( 'activitypub_inbox_update', $activity, $user_id, $activity_object );
return;
}
$state = Interactions::add_comment( $array );
$state = Interactions::add_comment( $activity );
$reaction = null;
if ( $state && ! \is_wp_error( $state ) ) {
$reaction = \get_comment( $state );
}
\do_action( 'activitypub_handled_create', $array, $user_id, $state, $reaction );
/**
* Fires after a Create activity has been handled.
*
* @param array $activity The activity-object.
* @param int $user_id The id of the local blog-user.
* @param \WP_Comment|\WP_Error $state The comment object or WP_Error.
* @param \WP_Comment|\WP_Error|null $reaction The reaction object or WP_Error.
*/
\do_action( 'activitypub_handled_create', $activity, $user_id, $state, $reaction );
}
/**
* Validate the object.
*
* @param bool $valid The validation state.
* @param string $param The object parameter.
* @param \WP_REST_Request $request The request object.
*
* @return bool The validation state: true if valid, false if not.
*/
public static function validate_object( $valid, $param, $request ) {
$json_params = $request->get_json_params();
if (
'Create' !== $json_params['type'] ||
is_wp_error( $request )
) {
return $valid;
}
$object = $json_params['object'];
$required = array(
'id',
'inReplyTo',
'content',
);
if ( array_intersect( $required, array_keys( $object ) ) !== $required ) {
return false;
}
return $valid;
}
}

View File

@ -1,7 +1,12 @@
<?php
/**
* Delete handler file.
*
* @package Activitypub
*/
namespace Activitypub\Handler;
use WP_Error;
use WP_REST_Request;
use Activitypub\Http;
use Activitypub\Collection\Followers;
@ -12,7 +17,7 @@ use Activitypub\Collection\Interactions;
*/
class Delete {
/**
* Initialize the class, registering WordPress hooks
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
@ -20,7 +25,7 @@ class Delete {
array( self::class, 'handle_delete' )
);
// defer signature verification for `Delete` requests.
// Defer signature verification for `Delete` requests.
\add_filter(
'activitypub_defer_signature_verification',
array( self::class, 'defer_signature_verification' ),
@ -28,7 +33,7 @@ class Delete {
2
);
// side effect
// Side effect.
\add_action(
'activitypub_delete_actor_interactions',
array( self::class, 'delete_interactions' )
@ -39,14 +44,16 @@ class Delete {
* Handles "Delete" requests.
*
* @param array $activity The delete activity.
* @param int $user_id The ID of the user performing the delete activity.
*/
public static function handle_delete( $activity ) {
$object_type = isset( $activity['object']['type'] ) ? $activity['object']['type'] : '';
switch ( $object_type ) {
// Actor Types
// @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
/*
* Actor Types.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
*/
case 'Person':
case 'Group':
case 'Organization':
@ -54,8 +61,12 @@ class Delete {
case 'Application':
self::maybe_delete_follower( $activity );
break;
// Object and Link Types
// @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
/*
* Object and Link Types.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
*/
case 'Note':
case 'Article':
case 'Image':
@ -65,26 +76,34 @@ class Delete {
case 'Document':
self::maybe_delete_interaction( $activity );
break;
// Tombstone Type
// @see: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-tombstone
/*
* Tombstone Type.
*
* @see: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-tombstone
*/
case 'Tombstone':
self::maybe_delete_interaction( $activity );
break;
// Minimal Activity
// @see https://www.w3.org/TR/activitystreams-core/#example-1
/*
* Minimal Activity.
*
* @see https://www.w3.org/TR/activitystreams-core/#example-1
*/
default:
// ignore non Minimal Activities.
// Ignore non Minimal Activities.
if ( ! is_string( $activity['object'] ) ) {
return;
}
// check if Object is an Actor.
// Check if Object is an Actor.
if ( $activity['actor'] === $activity['object'] ) {
self::maybe_delete_follower( $activity );
} else { // assume a interaction otherwise.
} else { // Assume an interaction otherwise.
self::maybe_delete_interaction( $activity );
}
// maybe handle Delete Activity for other Object Types.
// Maybe handle Delete Activity for other Object Types.
break;
}
}
@ -97,7 +116,7 @@ class Delete {
public static function maybe_delete_follower( $activity ) {
$follower = Followers::get_follower_by_actor( $activity['actor'] );
// verify if Actor is deleted.
// Verify that Actor is deleted.
if ( $follower && Http::is_tombstone( $activity['actor'] ) ) {
$follower->delete();
self::maybe_delete_interactions( $activity );
@ -110,7 +129,7 @@ class Delete {
* @param array $activity The delete activity.
*/
public static function maybe_delete_interactions( $activity ) {
// verify if Actor is deleted.
// Verify that Actor is deleted.
if ( Http::is_tombstone( $activity['actor'] ) ) {
\wp_schedule_single_event(
\time(),
@ -123,7 +142,7 @@ class Delete {
/**
* Delete comments from an Actor.
*
* @param array $comments The comments to delete.
* @param array $actor The actor whose comments to delete.
*/
public static function delete_interactions( $actor ) {
$comments = Interactions::get_interactions_by_actor( $actor );
@ -139,8 +158,6 @@ class Delete {
* Delete a Reaction if URL is a Tombstone.
*
* @param array $activity The delete activity.
*
* @return void
*/
public static function maybe_delete_interaction( $activity ) {
if ( is_array( $activity['object'] ) ) {

View File

@ -1,4 +1,10 @@
<?php
/**
* Follow handler file.
*
* @package Activitypub
*/
namespace Activitypub\Handler;
use Activitypub\Http;
@ -8,11 +14,11 @@ use Activitypub\Collection\Users;
use Activitypub\Collection\Followers;
/**
* Handle Follow requests
* Handle Follow requests.
*/
class Follow {
/**
* Initialize the class, registering WordPress hooks
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
@ -29,23 +35,21 @@ class Follow {
}
/**
* Handle "Follow" requests
* Handle "Follow" requests.
*
* @param array $activity The activity object
* @param int $user_id The user ID
* @param array $activity The activity object.
*/
public static function handle_follow( $activity ) {
$user = Users::get_by_resource( $activity['object'] );
if ( ! $user || is_wp_error( $user ) ) {
// If we can not find a user,
// we can not initiate a follow process
// If we can not find a user, we can not initiate a follow process.
return;
}
$user_id = $user->get__id();
// save follower
// Save follower.
$follower = Followers::add_follower(
$user_id,
$activity['actor']
@ -59,7 +63,7 @@ class Follow {
$follower
);
// send notification
// Send notification.
$notification = new Notification(
'follow',
$activity['actor'],
@ -70,25 +74,22 @@ class Follow {
}
/**
* Send Accept response
* Send Accept response.
*
* @param string $actor The Actor URL
* @param array $object The Activity object
* @param int $user_id The ID of the WordPress User
* @param Activitypub\Model\Follower $follower The Follower object
*
* @return void
* @param string $actor The Actor URL.
* @param array $activity_object The Activity object.
* @param int $user_id The ID of the WordPress User.
* @param \Activitypub\Model\Follower $follower The Follower object.
*/
public static function send_follow_response( $actor, $object, $user_id, $follower ) {
public static function send_follow_response( $actor, $activity_object, $user_id, $follower ) {
if ( \is_wp_error( $follower ) ) {
// it is not even possible to send a "Reject" because
// we can not get the Remote-Inbox
// Impossible to send a "Reject" because we can not get the Remote-Inbox.
return;
}
// only send minimal data
$object = array_intersect_key(
$object,
// Only send minimal data.
$activity_object = array_intersect_key(
$activity_object,
array_flip(
array(
'id',
@ -101,13 +102,13 @@ class Follow {
$user = Users::get_by_id( $user_id );
// get inbox
// Get inbox.
$inbox = $follower->get_shared_inbox();
// send "Accept" activity
// Send "Accept" activity.
$activity = new Activity();
$activity->set_type( 'Accept' );
$activity->set_object( $object );
$activity->set_object( $activity_object );
$activity->set_actor( $user->get_id() );
$activity->set_to( $actor );
$activity->set_id( $user->get_id() . '#follow-' . \preg_replace( '~^https?://~', '', $actor ) . '-' . \time() );

View File

@ -0,0 +1,70 @@
<?php
/**
* Like handler file.
*
* @package Activitypub
*/
namespace Activitypub\Handler;
use Activitypub\Comment;
use Activitypub\Collection\Interactions;
use function Activitypub\object_to_uri;
/**
* Handle Like requests.
*/
class Like {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
'activitypub_inbox_like',
array( self::class, 'handle_like' ),
10,
3
);
}
/**
* Handles "Like" requests.
*
* @param array $like The Activity array.
* @param int $user_id The ID of the local blog user.
*/
public static function handle_like( $like, $user_id ) {
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
return;
}
$url = object_to_uri( $like['object'] );
if ( empty( $url ) ) {
return;
}
$exists = Comment::object_id_to_comment( esc_url_raw( $url ) );
if ( $exists ) {
return;
}
$state = Interactions::add_reaction( $like );
$reaction = null;
if ( $state && ! is_wp_error( $state ) ) {
$reaction = get_comment( $state );
}
/**
* Fires after a Like has been handled.
*
* @param array $like The Activity array.
* @param int $user_id The ID of the local blog user.
* @param mixed $state The state of the reaction.
* @param mixed $reaction The reaction object.
*/
do_action( 'activitypub_handled_like', $like, $user_id, $state, $reaction );
}
}

View File

@ -1,17 +1,24 @@
<?php
/**
* Undo handler file.
*
* @package Activitypub
*/
namespace Activitypub\Handler;
use Activitypub\Collection\Users;
use Activitypub\Collection\Followers;
use Activitypub\Comment;
use function Activitypub\object_to_uri;
/**
* Handle Undo requests
* Handle Undo requests.
*/
class Undo {
/**
* Initialize the class, registering WordPress hooks
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
@ -21,23 +28,27 @@ class Undo {
}
/**
* Handle "Unfollow" requests
* Handle "Unfollow" requests.
*
* @param array $activity The JSON "Undo" Activity
* @param int $user_id The ID of the ID of the WordPress User
* @param array $activity The JSON "Undo" Activity.
*/
public static function handle_undo( $activity ) {
if (
isset( $activity['object']['type'] ) &&
'Follow' === $activity['object']['type'] &&
isset( $activity['object']['object'] )
! isset( $activity['object']['type'] ) ||
! isset( $activity['object']['object'] )
) {
return;
}
$type = $activity['object']['type'];
// Handle "Unfollow" requests.
if ( 'Follow' === $type ) {
$user_id = object_to_uri( $activity['object']['object'] );
$user = Users::get_by_resource( $user_id );
$user = Users::get_by_resource( $user_id );
if ( ! $user || is_wp_error( $user ) ) {
// If we can not find a user,
// we can not initiate a follow process
// If we can not find a user, we can not initiate a follow process.
return;
}
@ -46,5 +57,23 @@ class Undo {
Followers::remove_follower( $user_id, $actor );
}
// Handle "Undo" requests for "Like" and "Create" activities.
if ( in_array( $type, array( 'Like', 'Create', 'Announce' ), true ) ) {
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
return;
}
$object_id = object_to_uri( $activity['object'] );
$comment = Comment::object_id_to_comment( esc_url_raw( $object_id ) );
if ( empty( $comment ) ) {
return;
}
$state = wp_trash_comment( $comment );
do_action( 'activitypub_handled_undo', $activity, $user_id, isset( $state ) ? $state : null, null );
}
}
}

View File

@ -1,7 +1,12 @@
<?php
/**
* Update handler file.
*
* @package Activitypub
*/
namespace Activitypub\Handler;
use WP_Error;
use Activitypub\Collection\Interactions;
use function Activitypub\get_remote_metadata_by_actor;
@ -11,7 +16,7 @@ use function Activitypub\get_remote_metadata_by_actor;
*/
class Update {
/**
* Initialize the class, registering WordPress hooks
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
@ -23,24 +28,30 @@ class Update {
/**
* Handle "Update" requests
*
* @param array $array The activity-object
* @param int $user_id The id of the local blog-user
* @param array $activity The activity-object.
*/
public static function handle_update( $array ) {
$object_type = isset( $array['object']['type'] ) ? $array['object']['type'] : '';
public static function handle_update( $activity ) {
$object_type = isset( $activity['object']['type'] ) ? $activity['object']['type'] : '';
switch ( $object_type ) {
// Actor Types
// @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
/*
* Actor Types.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
*/
case 'Person':
case 'Group':
case 'Organization':
case 'Service':
case 'Application':
self::update_actor( $array );
self::update_actor( $activity );
break;
// Object and Link Types
// @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
/*
* Object and Link Types.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
*/
case 'Note':
case 'Article':
case 'Image':
@ -48,22 +59,23 @@ class Update {
case 'Video':
case 'Event':
case 'Document':
self::update_interaction( $array );
self::update_interaction( $activity );
break;
// Minimal Activity
// @see https://www.w3.org/TR/activitystreams-core/#example-1
/*
* Minimal Activity.
*
* @see https://www.w3.org/TR/activitystreams-core/#example-1
*/
default:
break;
}
}
/**
* Update an Interaction
* Update an Interaction.
*
* @param array $activity The activity-object
* @param int $user_id The id of the local blog-user
*
* @return void
* @param array $activity The activity-object.
*/
public static function update_interaction( $activity ) {
$commentdata = Interactions::update_comment( $activity );
@ -80,16 +92,14 @@ class Update {
}
/**
* Update an Actor
* Update an Actor.
*
* @param array $activity The activity-object
*
* @return void
* @param array $activity The activity-object.
*/
public static function update_actor( $activity ) {
// update cache
// Update cache.
get_remote_metadata_by_actor( $activity['actor'], false );
// @todo maybe also update all interactions
// @todo maybe also update all interactions.
}
}