first draft of join handler
Some checks failed
PHP Code Checker / PHP Code Checker (pull_request) Failing after 50s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Successful in 1m0s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Successful in 1m1s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Successful in 1m0s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Successful in 58s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Successful in 57s
Some checks failed
PHP Code Checker / PHP Code Checker (pull_request) Failing after 50s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Successful in 1m0s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Successful in 1m1s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Successful in 1m0s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Successful in 58s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Successful in 57s
This commit is contained in:
parent
7eb011859a
commit
08f136d52f
2 changed files with 142 additions and 0 deletions
137
includes/activitypub/handler/class-join.php
Normal file
137
includes/activitypub/handler/class-join.php
Normal file
|
@ -0,0 +1,137 @@
|
|||
<?php
|
||||
/**
|
||||
* Join handler file.
|
||||
*
|
||||
* @package ActivityPub_Event_Bridge
|
||||
* @license AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace ActivityPub_Event_Bridge\Activitypub\Handler;
|
||||
|
||||
use Activitypub\Activity\Activity;
|
||||
use Activitypub\Collection\Actors;
|
||||
use Activitypub\Http;
|
||||
use Activitypub\Transformer\Factory;
|
||||
use ActivityPub_Event_Bridge\Activitypub\Transformer\Event as Event_Transformer;
|
||||
|
||||
use function Activitypub\is_same_domain;
|
||||
use function Activitypub\object_to_uri;
|
||||
|
||||
/**
|
||||
* Handle Join requests.
|
||||
*/
|
||||
class Join {
|
||||
/**
|
||||
* Initialize the class, registering WordPress hooks.
|
||||
*/
|
||||
public static function init() {
|
||||
\add_action(
|
||||
'activitypub_inbox_join',
|
||||
array( self::class, 'handle_join' )
|
||||
);
|
||||
|
||||
\add_action(
|
||||
'activitypub_event_bridge_ignore_join',
|
||||
array( self::class, 'send_ignore_response' ),
|
||||
10,
|
||||
4
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle ActivityPub "Join" requests.
|
||||
*
|
||||
* @param array $activity The activity object.
|
||||
*/
|
||||
public static function handle_follow( $activity ) {
|
||||
$actor = Actors::get_by_resource( $activity['actor'] );
|
||||
|
||||
if ( ! $actor || is_wp_error( $actor ) ) {
|
||||
// If we can not find a user, we can not proceed the join process.
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! array_key_exists( 'object', $activity ) ) {
|
||||
// If the object is not set, we can not proceed the join process.
|
||||
return;
|
||||
}
|
||||
|
||||
$object_id = object_to_uri( $activity['object'] );
|
||||
|
||||
if ( ! is_same_domain( $object_id ) ) {
|
||||
// If the "Join" object is not owned by this WordPress site, abort.
|
||||
return;
|
||||
}
|
||||
|
||||
$post_id = url_to_postid( $object_id );
|
||||
|
||||
if ( ! $post_id ) {
|
||||
// No post is found for this URL/ID.
|
||||
return;
|
||||
}
|
||||
|
||||
$transformer = Factory::get_transformer( get_post( $post_id ) );
|
||||
|
||||
if ( ! $transformer instanceof Event_Transformer ) {
|
||||
// The target post is not an event post.
|
||||
return;
|
||||
}
|
||||
|
||||
// Pass over to Event plugin specific handler if implemented here.
|
||||
// Until then just send an ignore.
|
||||
|
||||
do_action(
|
||||
'activitypub_event_bridge_ignore_join',
|
||||
$transformer->get_actor_object()->get_id(),
|
||||
$activity,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send "Ignore" response.
|
||||
*
|
||||
* @param string $actor The actors ActivityPub ID which sends the response.
|
||||
* @param array $activity_object The Activity object that gets ignored.
|
||||
* @param string $to The target actor.
|
||||
*/
|
||||
public static function send_ignore_response( $actor, $activity_object, $to = null ) {
|
||||
if ( ! $to && array_key_exists( 'actor', $activity_object ) ) {
|
||||
$to = object_to_uri( $activity_object['actor'] );
|
||||
}
|
||||
|
||||
if ( ! $to ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only send minimal data.
|
||||
$activity_object = array_intersect_key(
|
||||
$activity_object,
|
||||
array_flip(
|
||||
array(
|
||||
'id',
|
||||
'type',
|
||||
'actor',
|
||||
'object',
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$to = Actors::get_by_resource( $to );
|
||||
$actor = Actors::get_by_resource( $actor );
|
||||
|
||||
// Get inbox.
|
||||
$inbox = $to->get_shared_inbox();
|
||||
|
||||
// Send "Ignore" activity.
|
||||
$activity = new Activity();
|
||||
$activity->set_type( 'Ignore' );
|
||||
$activity->set_object( $activity_object );
|
||||
$activity->set_actor( $actor->get_id() );
|
||||
$activity->set_to( $to );
|
||||
$activity->set_id( $actor->get_id() . '#ignore-' . \preg_replace( '~^https?://~', '', $activity_object['id'] ) );
|
||||
|
||||
$activity = $activity->to_json();
|
||||
|
||||
Http::post( $inbox, $activity, $actor->get__id() );
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@ namespace ActivityPub_Event_Bridge;
|
|||
// Exit if accessed directly.
|
||||
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
|
||||
|
||||
use ActivityPub_Event_Bridge\ActivityPub\Handler\Join;
|
||||
use ActivityPub_Event_Bridge\Admin\Event_Plugin_Admin_Notices;
|
||||
use ActivityPub_Event_Bridge\Admin\General_Admin_Notices;
|
||||
use ActivityPub_Event_Bridge\Admin\Health_Check;
|
||||
|
@ -187,6 +188,10 @@ class Setup {
|
|||
return;
|
||||
}
|
||||
|
||||
// Register the handler for incoming ActivityPub "Join" activities.
|
||||
add_action( 'init', array( Join::class, 'init' ) );
|
||||
|
||||
// Register the custom ActivityPub transformers.
|
||||
add_filter( 'activitypub_transformer', array( $this, 'register_activitypub_event_transformer' ), 10, 3 );
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue