add dummy handlers for incoming event source related activities
Some checks failed
PHP Code Checker / PHP Code Checker (pull_request) Failing after 48s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Successful in 1m7s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Failing after 56s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Failing after 55s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Failing after 55s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Failing after 55s
PHPUnit / PHPUnit – PHP 8.4 (pull_request) Failing after 54s

and phpcs
This commit is contained in:
André Menrath 2024-12-08 21:57:53 +01:00
parent d77cdf1430
commit 970f3e7754
13 changed files with 320 additions and 29 deletions

View file

@ -0,0 +1,36 @@
<?php
/**
* Class responsible for registering handlers for incoming activities to the ActivityPub plugin.
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
namespace Event_Bridge_For_ActivityPub\ActivityPub;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use Event_Bridge_For_ActivityPub\ActivityPub\Handler\Accept;
use Event_Bridge_For_ActivityPub\ActivityPub\Handler\Announce;
use Event_Bridge_For_ActivityPub\ActivityPub\Handler\Update;
use Event_Bridge_For_ActivityPub\ActivityPub\Handler\Create;
use Event_Bridge_For_ActivityPub\ActivityPub\Handler\Delete;
/**
* Class responsible for registering handlers for incoming activities to the ActivityPub plugin.
*/
class Handler {
/**
* Register all ActivityPub handlers.
*/
public static function register_handlers() {
Accept::init();
Announce::init();
Update::init();
Create::init();
Delete::init();
}
}

View file

@ -3,6 +3,7 @@
* Event sources collection file.
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
@ -294,7 +295,7 @@ class Event_Sources {
$actor = Event_Source::init_from_cpt( get_post( $post_id ) );
if ( is_wp_error( $actor ) ) {
if ( ! $actor instanceof Event_Source ) {
return $actor;
}
@ -309,7 +310,7 @@ class Event_Sources {
$activity->set_cc( null );
$activity->set_actor( $application->get_id() );
$activity->set_object( $to );
$activity->set_id( $actor . '#follow-' . \preg_replace( '~^https?://~', '', $to ) );
$activity->set_id( $application->get_id() . '#follow-' . \preg_replace( '~^https?://~', '', $to ) );
$activity = $activity->to_json();
\Activitypub\safe_remote_post( $inbox, $activity, \Activitypub\Collection\Actors::APPLICATION_USER_ID );
}
@ -345,7 +346,7 @@ class Event_Sources {
$actor = Event_Source::init_from_cpt( get_post( $post_id ) );
if ( is_wp_error( $actor ) ) {
if ( ! $actor instanceof Event_Source ) {
return $actor;
}

View file

@ -0,0 +1,50 @@
<?php
/**
* Accept handler file.
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later */
namespace Event_Bridge_For_ActivityPub\ActivityPub\Handler;
use Activitypub\Notification;
use Activitypub\Collection\Actors;
/**
* Handle Accept requests.
*/
class Accept {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
'activitypub_inbox_accept',
array( self::class, 'handle_accept' )
);
}
/**
* Handle "Follow" requests.
*
* @param array $activity The activity object.
*/
public static function handle_accept( $activity ) {
if ( ! isset( $activity['object'] ) ) {
return;
}
$object = Actors::get_by_resource( $activity['object'] );
if ( ! $object || is_wp_error( $object ) ) {
// If we can not find a actor, we handle the `Accept` activity.
return;
}
// We only expect `Accept` activities being answers to follow requests by the application actor.
if ( Actors::APPLICATION_USER_ID !== $object->get__id() ) {
return;
}
}
}

View file

@ -0,0 +1,49 @@
<?php
/**
* Accept handler file.
*
* @package Event_Bridge_For_ActivityPub
*/
namespace Event_Bridge_For_ActivityPub\ActivityPub\Handler;
use Activitypub\Notification;
use Activitypub\Collection\Actors;
/**
* Handle Accept requests.
*/
class Announce {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
'activitypub_inbox_announce',
array( self::class, 'handle_announce' )
);
}
/**
* Handle "Announce" requests.
*
* @param array $activity The activity object.
*/
public static function handle_announce( $activity ) {
if ( ! isset( $activity['object'] ) ) {
return;
}
$object = Actors::get_by_resource( $activity['object'] );
if ( ! $object || is_wp_error( $object ) ) {
// If we can not find a actor, we handle the `Accept` activity.
return;
}
// We only expect `Accept` activities being answers to follow requests by the application actor.
if ( Actors::APPLICATION_USER_ID !== $object->get__id() ) {
return;
}
}
}

View file

@ -0,0 +1,49 @@
<?php
/**
* Create handler file.
*
* @package Event_Bridge_For_ActivityPub
*/
namespace Event_Bridge_For_ActivityPub\ActivityPub\Handler;
use Activitypub\Notification;
use Activitypub\Collection\Actors;
/**
* Handle Create requests.
*/
class Create {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
'activitypub_inbox_create',
array( self::class, 'handle_create' )
);
}
/**
* Handle "Create" requests.
*
* @param array $activity The activity object.
*/
public static function handle_create( $activity ) {
if ( ! isset( $activity['object'] ) ) {
return;
}
$object = Actors::get_by_resource( $activity['object'] );
if ( ! $object || is_wp_error( $object ) ) {
// If we can not find a actor, we handle the `create` activity.
return;
}
// We only expect `create` activities being answers to follow requests by the application actor.
if ( Actors::APPLICATION_USER_ID !== $object->get__id() ) {
return;
}
}
}

View file

@ -0,0 +1,49 @@
<?php
/**
* Delete handler file.
*
* @package Event_Bridge_For_ActivityPub
*/
namespace Event_Bridge_For_ActivityPub\ActivityPub\Handler;
use Activitypub\Notification;
use Activitypub\Collection\Actors;
/**
* Handle Delete requests.
*/
class Delete {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
'activitypub_inbox_delete',
array( self::class, 'handle_delete' )
);
}
/**
* Handle "Follow" requests.
*
* @param array $activity The activity object.
*/
public static function handle_delete( $activity ) {
if ( ! isset( $activity['object'] ) ) {
return;
}
$object = Actors::get_by_resource( $activity['object'] );
if ( ! $object || is_wp_error( $object ) ) {
// If we can not find a actor, we handle the `Delete` activity.
return;
}
// We only expect `Delete` activities being answers to follow requests by the application actor.
if ( Actors::APPLICATION_USER_ID !== $object->get__id() ) {
return;
}
}
}

View file

@ -0,0 +1,49 @@
<?php
/**
* Update handler file.
*
* @package Event_Bridge_For_ActivityPub
*/
namespace Event_Bridge_For_ActivityPub\ActivityPub\Handler;
use Activitypub\Notification;
use Activitypub\Collection\Actors;
/**
* Handle Update requests.
*/
class Update {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
'activitypub_inbox_update',
array( self::class, 'handle_update' )
);
}
/**
* Handle "Follow" requests.
*
* @param array $activity The activity object.
*/
public static function handle_update( $activity ) {
if ( ! isset( $activity['object'] ) ) {
return;
}
$object = Actors::get_by_resource( $activity['object'] );
if ( ! $object || is_wp_error( $object ) ) {
// If we can not find a actor, we handle the `Update` activity.
return;
}
// We only expect `Update` activities being answers to follow requests by the application actor.
if ( Actors::APPLICATION_USER_ID !== $object->get__id() ) {
return;
}
}
}

View file

@ -77,8 +77,7 @@ class Settings_Page {
if ( isset( $url['path'] ) && isset( $url['host'] ) && isset( $url['scheme'] ) ) {
$actor_url = $event_source;
} else {
if ( preg_match( '/^@?' . Event_Source::ACTIVITYPUB_USER_HANDLE_REGEXP . '$/i', $event_source ) ) {
} elseif ( preg_match( '/^@?' . Event_Source::ACTIVITYPUB_USER_HANDLE_REGEXP . '$/i', $event_source ) ) {
$actor_url = Webfinger::resolve( $event_source );
if ( is_wp_error( $actor_url ) ) {
return;
@ -91,7 +90,6 @@ class Settings_Page {
$actor_url = Event_Sources::get_application_actor( $event_source );
}
}
}
if ( ! $actor_url ) {
return;

View file

@ -3,6 +3,8 @@
* Class for handling and saving the ActivityPub event sources (i.e. follows).
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
namespace Event_Bridge_For_ActivityPub;

View file

@ -15,12 +15,13 @@ namespace Event_Bridge_For_ActivityPub;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use Event_Bridge_For_ActivityPub\ActivityPub\Collection\Event_Sources as Event_Sources_Collection;
use Event_Bridge_For_ActivityPub\ActivityPub\Handler;
use Event_Bridge_For_ActivityPub\Admin\Event_Plugin_Admin_Notices;
use Event_Bridge_For_ActivityPub\Admin\General_Admin_Notices;
use Event_Bridge_For_ActivityPub\Admin\Health_Check;
use Event_Bridge_For_ActivityPub\Admin\Settings_Page;
use Event_Bridge_For_ActivityPub\Integrations\Event_Plugin;
use Event_Bridge_For_ActivityPub\ActivityPub\Collection\Event_Sources as Event_Sources_Collection;
require_once ABSPATH . 'wp-admin/includes/plugin.php';
@ -178,6 +179,8 @@ class Setup {
array( Settings_Page::class, 'settings_link' )
);
add_action( 'activitypub_register_handlers', array( Handler::class, 'register_activitypub_handlers' ) ) ;
// If we don't have any active event plugins, or the ActivityPub plugin is not enabled, abort here.
if ( empty( $this->active_event_plugins ) || ! $this->activitypub_plugin_is_active ) {
return;

View file

@ -6,6 +6,7 @@
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
namespace Event_Bridge_For_ActivityPub\Integrations;

View file

@ -3,6 +3,8 @@
* Event Sources Table-Class file.
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
namespace Event_Bridge_For_ActivityPub\Table;

View file

@ -3,6 +3,8 @@
* Event Sources management page for the ActivityPub Event Bridge.
*
* @package Event_Bridge_For_ActivityPub
* @since 1.0.0
* @license AGPL-3.0-or-later
*/
// Exit if accessed directly.