Skip to content

Commit

Permalink
Send Updates to Blog Actor in dual mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwiebe committed Jan 21, 2025
1 parent 186547d commit 106b390
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
31 changes: 31 additions & 0 deletions includes/class-dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static function init() {
\add_filter( 'activitypub_send_to_inboxes', array( self::class, 'add_inboxes_of_follower' ), 10, 2 );
\add_filter( 'activitypub_send_to_inboxes', array( self::class, 'add_inboxes_by_mentioned_actors' ), 10, 3 );
\add_filter( 'activitypub_send_to_inboxes', array( self::class, 'add_inboxes_of_replied_urls' ), 10, 3 );
\add_filter( 'activitypub_send_to_inboxes', array( self::class, 'maybe_add_inboxes_of_blog_user' ), 10, 3 );
}

/**
Expand Down Expand Up @@ -203,4 +204,34 @@ public static function add_inboxes_of_replied_urls( $inboxes, $actor_id, $activi

return $inboxes;
}

/**
* Adds Blog Actor inboxes to Updates so the Blog User's followers are notified of edits.
*
* @param array $inboxes The list of Inboxes.
* @param int $actor_id The WordPress Actor-ID.
* @param array $activity The ActivityPub Activity.
*
* @return array The filtered Inboxes
*/
public static function maybe_add_inboxes_of_blog_user( $inboxes, $actor_id, $activity ) {
// Only if we're in both Blog and User modes.
if ( ACTIVITYPUB_ACTOR_AND_BLOG_MODE !== \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) ) {
return $inboxes;
}
// Only if this isn't the Blog Actor.
if ( Actors::BLOG_USER_ID === $actor_id ) {
return $inboxes;
}
// Only if this is an Update.
if ( 'Update' !== $activity->get_type() ) {
return $inboxes;
}

$blog_inboxes = Followers::get_inboxes( Actors::BLOG_USER_ID );
$inboxes = array_merge( $inboxes, $blog_inboxes );
$inboxes = array_unique( $inboxes );

return $inboxes;
}
}
64 changes: 64 additions & 0 deletions tests/includes/class-test-dispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Test Dispatcher Class.
*
* @package ActivityPub
*/

use Activitypub\Activity\Activity;
use Activitypub\Collection\Actors;
use Activitypub\Collection\Followers;
use Activitypub\Dispatcher;


/**
* Test class for Activitypub Dispatcher.
*
* @covers Activitypub\Dispatcher
*/
class Test_Dispatcher extends WP_UnitTestCase {
/**
* Test maybe_add_inboxes_of_blog_user when actor mode is not ACTIVITYPUB_ACTOR_AND_BLOG_MODE
*/
public function test_maybe_add_inboxes_of_blog_user_wrong_mode() {
update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE );

$inboxes = array( 'https://example.com/inbox' );
$activity = $this->createMock( Activity::class );

$result = Dispatcher::maybe_add_inboxes_of_blog_user( $inboxes, 123, $activity );
$this->assertEquals( $inboxes, $result );
}

/**
* Test maybe_add_inboxes_of_blog_user when actor is blog user
*/
public function test_maybe_add_inboxes_of_blog_user_is_blog_user() {
update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE );

$inboxes = array( 'https://example.com/inbox' );
$activity = $this->createMock( Activity::class );

$result = Dispatcher::maybe_add_inboxes_of_blog_user( $inboxes, Actors::BLOG_USER_ID, $activity );
$this->assertEquals( $inboxes, $result );
}

/**
* Test maybe_add_inboxes_of_blog_user when activity type is not Update
*/
public function test_maybe_add_inboxes_of_blog_user_not_update() {
update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE );

$inboxes = array( 'https://example.com/inbox' );
$activity = $this->createMock( Activity::class );

// Mock the static method using reflection.
$activity->expects( $this->once() )
->method( '__call' )
->with( 'get_type' )
->willReturn( 'Create' );

$result = Dispatcher::maybe_add_inboxes_of_blog_user( $inboxes, 123, $activity );
$this->assertEquals( $inboxes, $result );
}
}

0 comments on commit 106b390

Please sign in to comment.