Skip to content

Commit d7b8404

Browse files
authored
Add CLI actor delete/update and improve tombstone handling (#2118)
1 parent 7f2edaa commit d7b8404

File tree

6 files changed

+86
-3
lines changed

6 files changed

+86
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Added a new WP-CLI command to manage Actors.

includes/class-cli.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Activitypub\Activity\Activity;
1111
use Activitypub\Collection\Actors;
1212
use Activitypub\Collection\Outbox;
13+
use Activitypub\Scheduler\Actor;
1314

1415
/**
1516
* WP-CLI commands.
@@ -395,6 +396,51 @@ public function comment( $args ) {
395396
}
396397
}
397398

399+
/**
400+
* Delete or Update an Actor.
401+
*
402+
* ## OPTIONS
403+
*
404+
* <action>
405+
* : The action to perform. Either `delete` or `update`.
406+
* ---
407+
* options:
408+
* - delete
409+
* - update
410+
* ---
411+
*
412+
* <id>
413+
* : The id of the Actor.
414+
*
415+
* ## EXAMPLES
416+
*
417+
* $ wp activitypub actor delete 1
418+
*
419+
* @synopsis <action> <id>
420+
*
421+
* @param array $args The arguments.
422+
*/
423+
public function actor( $args ) {
424+
if ( Actors::APPLICATION_USER_ID === (int) $args[1] ) {
425+
\WP_CLI::error( 'You cannot delete the application actor.' );
426+
}
427+
428+
switch ( $args[0] ) {
429+
case 'delete':
430+
\add_filter( 'activitypub_user_can_activitypub', '__return_true' );
431+
Actor::schedule_user_delete( $args[1] );
432+
\remove_filter( 'activitypub_user_can_activitypub', '__return_true' );
433+
\WP_CLI::success( '"Delete" activity is queued.' );
434+
break;
435+
case 'update':
436+
Actor::schedule_profile_update( $args[1] );
437+
\WP_CLI::success( '"Update" activity is queued.' );
438+
break;
439+
default:
440+
\WP_CLI::error( 'Unknown action.' );
441+
}
442+
}
443+
398444
/**
399445
* Undo an activity that was sent to the Fediverse.
400446
*

includes/functions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ function is_post_disabled( $post ) {
342342
* This function checks if a user is enabled for ActivityPub.
343343
*
344344
* @param int|string $user_id The user ID.
345+
*
345346
* @return boolean True if the user is enabled, false otherwise.
346347
*/
347348
function user_can_activitypub( $user_id ) {

includes/handler/class-delete.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public static function init() {
2424
\add_action( 'activitypub_inbox_delete', array( self::class, 'handle_delete' ) );
2525
\add_filter( 'activitypub_defer_signature_verification', array( self::class, 'defer_signature_verification' ), 10, 2 );
2626
\add_action( 'activitypub_delete_actor_interactions', array( self::class, 'delete_interactions' ) );
27+
2728
\add_filter( 'activitypub_get_outbox_activity', array( self::class, 'outbox_activity' ) );
29+
\add_action( 'post_activitypub_add_to_outbox', array( self::class, 'post_add_to_outbox' ), 10, 2 );
2830
}
2931

3032
/**
@@ -182,6 +184,7 @@ public static function defer_signature_verification( $defer, $request ) {
182184
* Set the object to the object ID.
183185
*
184186
* @param \Activitypub\Activity\Activity $activity The Activity object.
187+
*
185188
* @return \Activitypub\Activity\Activity The filtered Activity object.
186189
*/
187190
public static function outbox_activity( $activity ) {
@@ -191,4 +194,17 @@ public static function outbox_activity( $activity ) {
191194

192195
return $activity;
193196
}
197+
198+
/**
199+
* Add the activity to the outbox.
200+
*
201+
* @param int $outbox_id The ID of the outbox activity.
202+
* @param \Activitypub\Activity\Activity $activity The Activity object.
203+
*/
204+
public static function post_add_to_outbox( $outbox_id, $activity ) {
205+
// Set Tombstones for deleted objects.
206+
if ( 'Delete' === $activity->get_type() ) {
207+
Tombstone::bury( object_to_uri( $activity->get_object() ) );
208+
}
209+
}
194210
}

includes/scheduler/class-actor.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,6 @@ public static function schedule_user_delete( $user_id ) {
186186
return;
187187
}
188188

189-
Tombstone::bury( $actor->get_id() );
190-
Tombstone::bury( $actor->get_url() );
191-
192189
$activity = new Activity();
193190
$activity->set_actor( $actor->get_id() );
194191
$activity->set_object( $actor->get_id() );

tests/includes/class-test-tombstone.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ public function test_check_object() {
144144
$this->assertFalse( $response );
145145
}
146146

147+
/**
148+
* Response code is 404 -> is_tombstone returns true
149+
*
150+
* @covers ::bury
151+
*/
152+
public function test_bury() {
153+
$url = 'https://fake.test/object/123';
154+
155+
$response = Tombstone::exists_local( $url );
156+
$this->assertFalse( $response );
157+
158+
Tombstone::bury( $url );
159+
160+
$response = Tombstone::exists_local( $url );
161+
$this->assertTrue( $response );
162+
163+
\delete_option( 'activitypub_tombstone_urls' );
164+
}
165+
147166
/**
148167
* Response code is 404 -> is_tombstone returns true
149168
*

0 commit comments

Comments
 (0)