Compare commits

...

4 commits

Author SHA1 Message Date
Matthias Pfefferle
bf35836245
Merge branch 'master' into fix/accept-follow 2023-10-23 08:17:36 +02:00
Matthias Pfefferle
0b8f360e5b 10 sec should be enough 2023-10-18 13:19:40 +02:00
Matthias Pfefferle
5990c090dc schedule Accept Activity 2023-10-18 12:42:37 +02:00
Matthias Pfefferle
e6bb085354 add CLI to re-send Accept activities
This is mostly for debugging purposes
2023-10-18 11:26:42 +02:00
4 changed files with 110 additions and 0 deletions

View file

@ -15,6 +15,8 @@
namespace Activitypub; namespace Activitypub;
use WP_CLI;
use function Activitypub\is_blog_public; use function Activitypub\is_blog_public;
use function Activitypub\site_supports_blocks; use function Activitypub\site_supports_blocks;
@ -212,3 +214,9 @@ function get_plugin_version() {
return $meta['Version']; return $meta['Version'];
} }
// Check for CLI env, to add the CLI commands
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once __DIR__ . '/includes/class-cli.php';
WP_CLI::add_command( 'activitypub', __NAMESPACE__ . '\Cli' );
}

View file

@ -2,6 +2,7 @@
namespace Activitypub; namespace Activitypub;
use WP_Post; use WP_Post;
use Activitypub\Http;
use Activitypub\Activity\Activity; use Activitypub\Activity\Activity;
use Activitypub\Collection\Users; use Activitypub\Collection\Users;
use Activitypub\Collection\Followers; use Activitypub\Collection\Followers;
@ -25,6 +26,8 @@ class Activity_Dispatcher {
public static function init() { public static function init() {
\add_action( 'activitypub_send_activity', array( self::class, 'send_activity' ), 10, 2 ); \add_action( 'activitypub_send_activity', array( self::class, 'send_activity' ), 10, 2 );
\add_action( 'activitypub_send_activity', array( self::class, 'send_activity_or_announce' ), 10, 2 ); \add_action( 'activitypub_send_activity', array( self::class, 'send_activity_or_announce' ), 10, 2 );
\add_action( 'activitypub_send_accept_activity', array( self::class, 'send_accept_activity' ), 10, 3 );
} }
/** /**
@ -122,4 +125,17 @@ class Activity_Dispatcher {
safe_remote_post( $inbox, $json, $wp_post->post_author ); safe_remote_post( $inbox, $json, $wp_post->post_author );
} }
} }
/**
* Send Accept Activities.
*
* @param string $inbox The inbox url.
* @param array $activity The Activity Array
* @param int $user_id The user id.
*
* @return void
*/
public static function send_accept_activity( $inbox, $activity, $user_id ) {
Http::post( $inbox, $activity, $user_id );
}
} }

74
includes/class-cli.php Normal file
View file

@ -0,0 +1,74 @@
<?php
namespace Activitypub;
use WP_CLI;
use WP_CLI_Command;
use Activitypub\Collection\Users;
use Activitypub\Activity\Activity;
use Activitypub\Collection\Followers;
/**
* The ActivityPub CLI
*/
class Cli extends WP_CLI_Command {
/**
* Re-Send Accept Activity as response to a Follow
*
* ## OPTIONS
*
* [--follower=<url>]
* : The Follower ID, this is generally a URL
*
* [--user_id=<id>]
* : The ActivityPub User ID
*
* ## EXAMPLES
*
* # Re-Send Accept Activity as response to a Follow
* $ wp activitypub accept --follower=https://mastodon.social/@pfefferle --user=1
*/
public function accept( $args, $assoc_args ) {
$user_id = (int) $assoc_args['user_id'];
$user = Users::get_by_id( $user_id );
if ( ! $user ) {
WP_CLI::error( __( 'User is not allow to send Activities', 'activitypub' ) );
}
$follower = esc_url( $assoc_args['follower'] );
$follower = Followers::get_follower( $user->get__id(), $follower );
if ( ! $follower ) {
WP_CLI::error( __( 'Unknown Follower', 'activitypub' ) );
}
// get inbox
$inbox = $follower->get_shared_inbox();
$object = array(
'id' => $follower->get_id(),
'type' => 'Follow',
'actor' => $follower->get_id(),
'object' => $user->get_id(),
);
// send "Accept" activity
$activity = new Activity();
$activity->set_type( 'Accept' );
$activity->set_object( $object );
$activity->set_actor( $user->get_id() );
$activity->set_to( $follower->get_id() );
$activity->set_id( $user->get_id() . '#follow-' . \preg_replace( '~^https?://~', '', $follower->get_id() ) . '-' . \time() );
$activity = $activity->to_json();
$response = Http::post( $inbox, $activity, $user_id );
if ( is_wp_error( $response ) ) {
WP_CLI::error( $response->get_error_message() );
} else {
WP_CLI::success( __( 'Activity sent', 'activitypub' ) );
}
}
}

View file

@ -288,6 +288,18 @@ class Followers {
$activity = $activity->to_json(); $activity = $activity->to_json();
// schedule Accept Activity so that we can get sure it will be sent...
\wp_schedule_single_event(
\strtotime( '+10 seconds' ),
'activitypub_send_accept_activity',
array(
$inbox,
$activity,
$user_id,
)
);
// ...and send it directly
Http::post( $inbox, $activity, $user_id ); Http::post( $inbox, $activity, $user_id );
} }