Skip to content

Commit e394264

Browse files
pfefferleobenlandmatticbot
authored
Fix: Announces (#1500)
* Fix: Announces With the re-write of how we store Activities in the Outbox, the Announces do no longer work. This PR fixes that. * Update includes/class-scheduler.php Co-authored-by: Konstantin Obenland <obenland@gmx.de> * simplify namings * Add unittests * Add changelog --------- Co-authored-by: Konstantin Obenland <obenland@gmx.de> Co-authored-by: Automattic Bot <sysops+ghmatticbot@automattic.com>
1 parent 77599ba commit e394264

File tree

3 files changed

+85
-17
lines changed

3 files changed

+85
-17
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
`Scheduler::schedule_announce_activity` to handle Activities instead of Activity-Objects.

includes/class-scheduler.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Activitypub;
99

10+
use Activitypub\Activity\Activity;
1011
use Activitypub\Scheduler\Post;
1112
use Activitypub\Scheduler\Actor;
1213
use Activitypub\Scheduler\Comment;
@@ -391,11 +392,11 @@ private static function next_scheduled_hook( $hook ) {
391392
* Send announces.
392393
*
393394
* @param int $outbox_activity_id The outbox activity ID.
394-
* @param \Activitypub\Activity\Activity $activity_object The activity object.
395+
* @param \Activitypub\Activity\Activity $activity The activity object.
395396
* @param int $actor_id The actor ID.
396397
* @param int $content_visibility The content visibility.
397398
*/
398-
public static function schedule_announce_activity( $outbox_activity_id, $activity_object, $actor_id, $content_visibility ) {
399+
public static function schedule_announce_activity( $outbox_activity_id, $activity, $actor_id, $content_visibility ) {
399400
// Only if we're in both Blog and User modes.
400401
if ( ACTIVITYPUB_ACTOR_AND_BLOG_MODE !== \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) ) {
401402
return;
@@ -411,28 +412,26 @@ public static function schedule_announce_activity( $outbox_activity_id, $activit
411412
return;
412413
}
413414

414-
$activity_type = \get_post_meta( $outbox_activity_id, '_activitypub_activity_type', true );
415-
416415
// Only if the activity is a Create, Update or Delete.
417-
if ( ! in_array( $activity_type, array( 'Create', 'Update', 'Delete' ), true ) ) {
416+
if ( ! in_array( $activity->get_type(), array( 'Create', 'Update', 'Delete' ), true ) ) {
418417
return;
419418
}
420419

421-
// Check if the object is an article, image, audio, video, event or document and ignore profile updates and other activities.
422-
if ( ! in_array( $activity_object->get_type(), array( 'Note', 'Article', 'Image', 'Audio', 'Video', 'Event', 'Document' ), true ) ) {
420+
if ( ! is_object( $activity->get_object() ) ) {
423421
return;
424422
}
425423

426-
$transformer = Factory::get_transformer( $activity_object );
427-
if ( ! $transformer || \is_wp_error( $transformer ) ) {
424+
// Check if the object is an article, image, audio, video, event, or document and ignore profile updates and other activities.
425+
if ( ! in_array( $activity->get_object()->get_type(), array( 'Note', 'Article', 'Image', 'Audio', 'Video', 'Event', 'Document' ), true ) ) {
428426
return;
429427
}
430428

431-
$post = get_post( $outbox_activity_id );
432-
$activity = $transformer->to_activity( $activity_type );
433-
$activity->set_id( $post->guid );
429+
$announce = new Activity();
430+
$announce->set_type( 'Announce' );
431+
$announce->set_actor( Actors::get_by_id( Actors::BLOG_USER_ID )->get_id() );
432+
$announce->set_object( $activity );
434433

435-
$outbox_activity_id = Outbox::add( $activity, 'Announce', Actors::BLOG_USER_ID );
434+
$outbox_activity_id = Outbox::add( $announce, Actors::BLOG_USER_ID );
436435

437436
if ( ! $outbox_activity_id ) {
438437
return;

tests/includes/class-test-scheduler.php

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77

88
namespace Activitypub\Tests;
99

10-
use Activitypub\Activity\Activity;
1110
use Activitypub\Scheduler;
12-
use Activitypub\Collection\Outbox;
11+
use Activitypub\Activity\Activity;
1312
use Activitypub\Activity\Base_Object;
14-
use WP_UnitTestCase;
13+
use Activitypub\Collection\Outbox;
14+
use Activitypub\Collection\Actors;
15+
1516
use function Activitypub\add_to_outbox;
1617

1718
/**
1819
* Test class for Scheduler.
1920
*
2021
* @coversDefaultClass \Activitypub\Scheduler
2122
*/
22-
class Test_Scheduler extends WP_UnitTestCase {
23+
class Test_Scheduler extends \WP_UnitTestCase {
2324
/**
2425
* Test user ID.
2526
*
@@ -285,4 +286,68 @@ public function test_async_batch_with_invalid_callback() {
285286
// Run async_batch with invalid callback.
286287
Scheduler::async_batch( array( $mock_class, 'callback' ) );
287288
}
289+
290+
/**
291+
* Test schedule_announce_activity method.
292+
*
293+
* @covers ::schedule_announce_activity
294+
*/
295+
public function test_schedule_announce_activity() {
296+
// Set the actor mode to both blog and user mode.
297+
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE );
298+
299+
$activity = new Activity();
300+
$activity->set_type( 'Create' );
301+
$activity->set_id( 'https://example.com/test-id' );
302+
303+
// Create a Note object for the activity.
304+
$note = new Base_Object();
305+
$note->set_type( 'Note' );
306+
$note->set_content( 'Test content' );
307+
$note->set_id( 'https://example.com/note/1' );
308+
$activity->set_object( $note );
309+
310+
$outbox_activity_id = Outbox::add( $activity, self::$user_id );
311+
312+
$scheduled_events = array();
313+
add_filter(
314+
'schedule_event',
315+
function ( $event ) use ( &$scheduled_events ) {
316+
if ( 'activitypub_process_outbox' === $event->hook ) {
317+
$scheduled_events[] = $event->args[0];
318+
}
319+
return $event;
320+
}
321+
);
322+
323+
Scheduler::schedule_announce_activity( $outbox_activity_id, $activity, self::$user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC );
324+
325+
// Get the most recent outbox item for the blog actor.
326+
$announce_outbox_items = get_posts(
327+
array(
328+
'post_type' => Outbox::POST_TYPE,
329+
'post_author' => Actors::BLOG_USER_ID,
330+
'post_status' => 'pending',
331+
'orderby' => 'ID',
332+
'order' => 'DESC',
333+
'posts_per_page' => 1,
334+
)
335+
);
336+
337+
$this->assertNotEmpty( $announce_outbox_items, 'No announce outbox items found' );
338+
$announce_outbox_id = $announce_outbox_items[0]->ID;
339+
340+
$this->assertCount( 1, $scheduled_events, 'Should schedule 1 event' );
341+
$this->assertContains( $announce_outbox_id, $scheduled_events, 'Should schedule the announce outbox activity' );
342+
343+
// Check for Announce activity in the outbox.
344+
$announce_post = get_post( $announce_outbox_id );
345+
$announce_activity = json_decode( $announce_post->post_content, true );
346+
$this->assertEquals( 'Announce', $announce_activity['type'] );
347+
348+
// Clean up.
349+
wp_delete_post( $outbox_activity_id, true );
350+
wp_delete_post( $announce_outbox_id, true );
351+
remove_all_filters( 'schedule_event' );
352+
}
288353
}

0 commit comments

Comments
 (0)