allow sending blog-wide activities

This commit is contained in:
Matthias Pfefferle 2023-07-03 11:56:25 +02:00
parent dd67f76db1
commit 1685ec7cc8
3 changed files with 59 additions and 52 deletions

View file

@ -1,11 +1,14 @@
<?php
namespace Activitypub;
use WP_Post;
use Activitypub\User_Factory;
use Activitypub\Model\Post;
use Activitypub\Model\Activity;
use Activitypub\User_Factory;
use Activitypub\Collection\Followers;
use function Activitypub\is_user_disabled;
use function Activitypub\safe_remote_post;
/**
@ -20,59 +23,67 @@ class Activity_Dispatcher {
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
// legacy
\add_action( 'activitypub_send_post_activity', array( self::class, 'send_create_activity' ) );
\add_action( 'activitypub_send_create_activity', array( self::class, 'send_create_activity' ) );
\add_action( 'activitypub_send_update_activity', array( self::class, 'send_update_activity' ) );
\add_action( 'activitypub_send_delete_activity', array( self::class, 'send_delete_activity' ) );
\add_action( 'activitypub_send_activity', array( self::class, 'send_activity' ), 10, 2 );
}
/**
* Send "create" activities.
*
* @param Activitypub\Model\Post $activitypub_post
*/
public static function send_create_activity( Post $activitypub_post ) {
self::send_activity( $activitypub_post, 'Create' );
}
/**
* Send "update" activities.
*
* @param Activitypub\Model\Post $activitypub_post The ActivityPub Post.
*/
public static function send_update_activity( Post $activitypub_post ) {
self::send_activity( $activitypub_post, 'Update' );
}
/**
* Send "delete" activities.
*
* @param Activitypub\Model\Post $activitypub_post The ActivityPub Post.
*/
public static function send_delete_activity( Post $activitypub_post ) {
self::send_activity( $activitypub_post, 'Delete' );
\add_action( 'activitypub_send_activity', array( self::class, 'send_user_activity' ), 10, 2 );
\add_action( 'activitypub_send_activity', array( self::class, 'send_blog_activity' ), 10, 2 );
}
/**
* Send Activities to followers and mentioned users.
*
* @param Activitypub\Model\Post $activitypub_post The ActivityPub Post.
* @param string $activity_type The Activity-Type.
* @param WP_Post $wp_post The ActivityPub Post.
* @param string $activity_type The Activity-Type.
*
* @return void
*/
public static function send_activity( Post $activitypub_post, $activity_type ) {
public static function send_user_activity( WP_Post $wp_post, $activity_type ) {
// check if a migration is needed before sending new posts
Migration::maybe_migrate();
$activitypub_activity = new Activity( $activity_type );
$activitypub_activity->from_post( $activitypub_post );
if ( is_user_disabled( $wp_post->post_author ) ) {
return;
}
$user_id = $activitypub_post->get_user_id();
$post = new Post( $wp_post );
$activitypub_activity = new Activity( $activity_type );
$activitypub_activity->from_post( $post );
$user_id = $wp_post->post_author;
$follower_inboxes = Followers::get_inboxes( $user_id );
$mentioned_inboxes = Mention::get_inboxes( $activitypub_activity->get_cc() );
$inboxes = array_merge( $follower_inboxes, $mentioned_inboxes );
$inboxes = array_unique( $inboxes );
foreach ( $inboxes as $inbox ) {
$activity = $activitypub_activity->to_json();
safe_remote_post( $inbox, $activity, $user_id );
}
}
/**
* Send Activities to followers and mentioned users.
*
* @param WP_Post $wp_post The ActivityPub Post.
* @param string $activity_type The Activity-Type.
*
* @return void
*/
public static function send_blog_activity( WP_Post $wp_post, $activity_type ) {
// check if a migration is needed before sending new posts
Migration::maybe_migrate();
if ( is_user_disabled( User_Factory::BLOG_USER_ID ) ) {
return;
}
$post = new Post( $wp_post, User_Factory::BLOG_USER_ID );
$activitypub_activity = new Activity( $activity_type );
$activitypub_activity->from_post( $post );
$user_id = User_Factory::BLOG_USER_ID;
$follower_inboxes = Followers::get_inboxes( $user_id );
$mentioned_inboxes = Mention::get_inboxes( $activitypub_activity->get_cc() );

View file

@ -83,12 +83,10 @@ class Scheduler {
return;
}
$activitypub_post = new Post( $post, User_Factory::BLOG_USER_ID );
\wp_schedule_single_event(
\time(),
'activitypub_send_activity',
array( $activitypub_post, $type )
array( $post, $type )
);
\wp_schedule_single_event(
@ -97,7 +95,7 @@ class Scheduler {
'activitypub_send_%s_activity',
\strtolower( $type )
),
array( $activitypub_post )
array( $post )
);
}

View file

@ -34,10 +34,9 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT
$pre_http_request = new MockAction();
add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 );
$activitypub_post = new \Activitypub\Model\Post( $post );
\Activitypub\Activity_Dispatcher::send_create_activity( $activitypub_post );
$post = get_post( $post );
$this->assertNotEmpty( $activitypub_post->get_content() );
\Activitypub\Activity_Dispatcher::send_user_activity( $post, 'Create' );
$this->assertSame( 2, $pre_http_request->get_call_count() );
$all_args = $pre_http_request->get_args();
@ -77,10 +76,9 @@ class Test_Activitypub_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HT
$pre_http_request = new MockAction();
add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 );
$activitypub_post = new \Activitypub\Model\Post( $post );
\Activitypub\Activity_Dispatcher::send_create_activity( $activitypub_post );
$post = get_post( $post );
$this->assertNotEmpty( $activitypub_post->get_content() );
\Activitypub\Activity_Dispatcher::send_user_activity( $post, 'Create' );
$this->assertSame( 1, $pre_http_request->get_call_count() );
$all_args = $pre_http_request->get_args();