Skip to content

Commit b81e286

Browse files
committed
Scheduler: Pass guid so it can be queried later (#1915)
1 parent 8e4ce39 commit b81e286

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
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+
When deleting interactions for cleaned up actors, we use the actor's URL again to retrieve their information instead of our internal ID.

includes/class-scheduler.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,8 @@ public static function cleanup_remote_actors() {
198198
\wp_delete_post( $actor->ID );
199199
} elseif ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) {
200200
if ( Actors::count_errors( $actor->ID ) >= 5 ) {
201+
\wp_schedule_single_event( \time(), 'activitypub_delete_actor_interactions', array( $actor->guid ) );
201202
\wp_delete_post( $actor->ID );
202-
\wp_schedule_single_event(
203-
\time(),
204-
'activitypub_delete_actor_interactions',
205-
array( $actor->ID )
206-
);
207203
} else {
208204
Actors::add_error( $actor->ID, $meta );
209205
}

tests/includes/class-test-scheduler.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,70 @@ function ( $event ) use ( &$scheduled_events ) {
427427
wp_delete_post( $announce_outbox_id, true );
428428
remove_all_filters( 'schedule_event' );
429429
}
430+
431+
/**
432+
* Test cleanup_remote_actors method.
433+
*
434+
* @covers ::cleanup_remote_actors
435+
*/
436+
public function test_cleanup_remote_actors() {
437+
// Mock actor metadata.
438+
\add_filter(
439+
'activitypub_pre_http_get_remote_object',
440+
function () {
441+
return array(
442+
'name' => 'Test User',
443+
'preferredUsername' => 'test',
444+
'id' => 'https://example.com/users/test',
445+
'url' => 'https://example.com/@test',
446+
'inbox' => 'https://example.com/users/test/inbox',
447+
);
448+
}
449+
);
450+
451+
$actor = Actors::fetch_remote_by_uri( 'https://example.com/users/test' );
452+
453+
for ( $i = 0; $i < 6; $i++ ) {
454+
Actors::add_error( $actor->ID, 'Failed to fetch or parse metadata ' . $i );
455+
}
456+
457+
// Track scheduled events.
458+
$scheduled_events = array();
459+
\add_filter(
460+
'schedule_event',
461+
function ( $event ) use ( &$scheduled_events ) {
462+
if ( 'activitypub_delete_actor_interactions' === $event->hook ) {
463+
$scheduled_events[] = array(
464+
'hook' => $event->hook,
465+
'args' => $event->args,
466+
'time' => $event->timestamp,
467+
);
468+
}
469+
return $event;
470+
}
471+
);
472+
\add_filter(
473+
'pre_get_remote_metadata_by_actor',
474+
function () {
475+
return new \WP_Error( 'no_actor', 'No actor found' );
476+
}
477+
);
478+
479+
// Run the cleanup function.
480+
Scheduler::cleanup_remote_actors();
481+
482+
// Verify that the event was scheduled with the actor URL as parameter.
483+
$this->assertCount( 1, $scheduled_events, 'Should schedule 1 event' );
484+
$this->assertEquals( 'activitypub_delete_actor_interactions', $scheduled_events[0]['hook'], 'Should schedule the correct hook' );
485+
$this->assertCount( 1, $scheduled_events[0]['args'], 'Should have 1 argument' );
486+
$this->assertEquals( 'https://example.com/users/test', $scheduled_events[0]['args'][0], 'Should pass actor URL as parameter' );
487+
488+
// Verify the actor was deleted.
489+
$this->assertNull( \get_post( $actor->ID ), 'Actor should be deleted' );
490+
491+
// Clean up.
492+
\remove_all_filters( 'activitypub_pre_http_get_remote_object' );
493+
\remove_all_filters( 'pre_get_remote_metadata_by_actor' );
494+
\remove_all_filters( 'schedule_event' );
495+
}
430496
}

0 commit comments

Comments
 (0)