Skip to content

Commit 6b0a2cf

Browse files
obenlandpfefferle
andauthored
Add WP-CLI command to reprocess inbox items (#2500)
Co-authored-by: Matthias Pfefferle <pfefferle@users.noreply.github.com>
1 parent c99298e commit 6b0a2cf

File tree

5 files changed

+86
-36
lines changed

5 files changed

+86
-36
lines changed

includes/handler/class-create.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,7 @@ public static function create_interaction( $activity, $user_ids, $activity_objec
7878

7979
// If comment exists, call update action.
8080
if ( $check_dupe ) {
81-
/**
82-
* Fires when a Create activity is received for an existing comment.
83-
*
84-
* @param array $activity The activity-object.
85-
* @param int[] $user_ids The ids of the local blog-users.
86-
* @param \Activitypub\Activity\Activity $activity_object The activity object.
87-
*/
88-
\do_action( 'activitypub_inbox_update', $activity, (array) $user_ids, $activity_object );
89-
return false;
81+
return Update::handle_update( $activity, (array) $user_ids, $activity_object );
9082
}
9183

9284
if ( is_self_ping( $activity['object']['id'] ) ) {
@@ -116,15 +108,7 @@ public static function create_post( $activity, $user_ids, $activity_object = nul
116108

117109
// If comment exists, call update action.
118110
if ( ! \is_wp_error( $check_dupe ) ) {
119-
/**
120-
* Fires when a Create activity is received for an existing object.
121-
*
122-
* @param array $activity The activity-object.
123-
* @param int[] $user_ids The id of the local blog-user.
124-
* @param \Activitypub\Activity\Activity $activity_object The activity object.
125-
*/
126-
\do_action( 'activitypub_inbox_update', $activity, (array) $user_ids, $activity_object );
127-
return false;
111+
return Update::handle_update( $activity, (array) $user_ids, $activity_object );
128112
}
129113

130114
return Posts::add( $activity, $user_ids );

includes/handler/class-update.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,7 @@ public static function update_object( $activity, $user_ids, $activity_object ) {
104104

105105
// There is no object to update, try to trigger create instead.
106106
if ( ! $updated ) {
107-
/**
108-
* Fires when a Create activity is received for an existing object.
109-
*
110-
* @param array $activity The activity-object.
111-
* @param int[] $user_ids The ids of the local blog-users.
112-
* @param \Activitypub\Activity\Activity $activity_object The activity object.
113-
*/
114-
\do_action( 'activitypub_inbox_create', $activity, $user_ids, $activity_object );
115-
return false;
107+
return Create::handle_create( $activity, $user_ids, $activity_object );
116108
}
117109

118110
$success = ( $result && ! \is_wp_error( $result ) );

local/class-cli.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
namespace Activitypub\Development;
99

10+
use Activitypub\Activity\Activity;
1011
use Activitypub\Collection\Followers;
12+
use Activitypub\Collection\Inbox;
1113
use Activitypub\Comment;
1214

15+
use function Activitypub\camel_to_snake_case;
1316
use function WP_CLI\Utils\get_flag_value;
1417
use function WP_CLI\Utils\make_progress_bar;
1518

@@ -168,4 +171,72 @@ private function generate_comments( $args, $assoc_args ) {
168171
$notify->finish();
169172
}
170173
}
174+
175+
/**
176+
* Reprocess an inbox item.
177+
*
178+
* ## OPTIONS
179+
*
180+
* <post_id>
181+
* : The post ID of the ap_inbox item to reprocess.
182+
*
183+
* ## EXAMPLES
184+
*
185+
* # Reprocess inbox item with ID 123
186+
* $ wp activitypub reprocess_inbox 123
187+
* Success: Inbox item 123 has been reprocessed.
188+
*
189+
* @param array $args The arguments.
190+
*/
191+
public function reprocess_inbox( $args ) {
192+
$post_id = absint( $args[0] );
193+
194+
if ( ! $post_id ) {
195+
\WP_CLI::error( 'Invalid post ID provided.' );
196+
}
197+
198+
$post = Inbox::get( $post_id );
199+
200+
if ( ! $post ) {
201+
\WP_CLI::error( sprintf( 'Post with ID %d not found.', $post_id ) );
202+
}
203+
204+
\WP_CLI::log( sprintf( 'Reprocessing inbox item %d...', $post_id ) );
205+
206+
// Get the activity data from the inbox post.
207+
$activity_data = json_decode( $post->post_content, true );
208+
209+
if ( ! $activity_data ) {
210+
\WP_CLI::error( 'Failed to decode activity data.' );
211+
}
212+
213+
// Get the activity type.
214+
if ( ! isset( $activity_data['type'] ) ) {
215+
\WP_CLI::error( 'Activity data does not contain a type field.' );
216+
}
217+
218+
$type = camel_to_snake_case( $activity_data['type'] );
219+
220+
// Get recipients from post meta.
221+
$user_ids = Inbox::get_recipients( $post_id );
222+
223+
if ( empty( $user_ids ) ) {
224+
\WP_CLI::error( 'No recipients found for this inbox item.' );
225+
}
226+
227+
// Create Activity object from the activity data.
228+
$activity = Activity::init_from_array( $activity_data );
229+
230+
if ( \is_wp_error( $activity ) ) {
231+
\WP_CLI::error( sprintf( 'Failed to initialize activity: %s', $activity->get_error_message() ) );
232+
}
233+
234+
// Trigger both sets of action hooks that handlers may be registered on.
235+
\do_action( 'activitypub_inbox', $activity_data, $user_ids, $type, $activity, Inbox::CONTEXT_INBOX );
236+
\do_action( 'activitypub_inbox_' . $type, $activity_data, $user_ids, $activity, Inbox::CONTEXT_INBOX );
237+
\do_action( 'activitypub_handled_inbox', $activity_data, $user_ids, $type, $activity, $post_id, Inbox::CONTEXT_INBOX );
238+
\do_action( 'activitypub_handled_inbox_' . $type, $activity_data, $user_ids, $activity, $post_id, Inbox::CONTEXT_INBOX );
239+
240+
\WP_CLI::success( sprintf( 'Inbox item %d has been reprocessed as %s activity.', $post_id, $activity_data['type'] ) );
241+
}
171242
}

local/load.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@
3434

3535
// Defer signature verification on local development to better test API requests.
3636
\add_filter( 'activitypub_defer_signature_verification', '__return_true', 20 );
37+
38+
\add_filter( 'option_activitypub_create_posts', '__return_true' );

tests/phpunit/tests/includes/handler/class-test-update.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class Test_Update extends \WP_UnitTestCase {
2121

2222
/**
23-
* Test that the activitypub_inbox_create fallback is triggered.
23+
* Test that the activitypub_handled_create fallback is triggered.
2424
*/
2525
public function test_activitypub_inbox_create_fallback() {
2626
\update_option( 'activitypub_create_posts', true );
@@ -35,31 +35,32 @@ public function test_activitypub_inbox_create_fallback() {
3535
'type' => 'Update',
3636
'actor' => $test_actor,
3737
'object' => array(
38-
'id' => 'https://example.com/objects/12345',
39-
'type' => 'Note',
40-
'content' => 'Test note',
38+
'id' => 'https://example.com/objects/12345',
39+
'type' => 'Note',
40+
'content' => 'Test note',
41+
'attributedTo' => $test_actor,
4142
),
4243
);
4344

4445
// Add a fallback handler for the action.
4546
\add_action(
46-
'activitypub_inbox_create',
47+
'activitypub_handled_create',
4748
function ( $activity_data ) use ( &$called, $test_actor ) {
4849
if ( isset( $activity_data['actor'] ) && $activity_data['actor'] === $test_actor ) {
4950
$called = true;
5051
}
5152
},
5253
10,
53-
3
54+
4
5455
);
5556

56-
// Call the handler via the new handled_inbox_update hook.
57+
// Call the handler via the handled_inbox_update hook.
5758
\do_action( 'activitypub_handled_inbox_update', $activity, array( $this->user_id ), null );
5859

59-
$this->assertTrue( $called, 'The fallback activitypub_inbox_create action should be triggered.' );
60+
$this->assertTrue( $called, 'The fallback activitypub_handled_create action should be triggered.' );
6061

6162
// Clean up by removing the action.
62-
\remove_all_actions( 'activitypub_inbox_create' );
63+
\remove_all_actions( 'activitypub_handled_create' );
6364
\remove_all_actions( 'activitypub_handled_inbox_update' );
6465
\delete_option( 'activitypub_create_posts' );
6566
}

0 commit comments

Comments
 (0)