Skip to content

Commit

Permalink
use $data to generate ID if it is an URI
Browse files Browse the repository at this point in the history
  • Loading branch information
pfefferle committed Nov 20, 2024
1 parent c8597b7 commit 7f95415
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
8 changes: 8 additions & 0 deletions includes/activity/class-activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ public function set_object( $data ) {
// Set object.
$this->set( 'object', $data );

// Check if `$data` is a URL and use it to generate an ID then.
if ( is_string( $data ) && filter_var( $data, FILTER_VALIDATE_URL ) ) {
$this->set( 'id', $data . '#activity-' . strtolower( $this->get_type() ) . '-' . time() );

return;
}

// Check if `$data` is an object and copy some properties otherwise do nothing.
if ( ! is_object( $data ) ) {
return;
}
Expand Down
1 change: 0 additions & 1 deletion includes/class-activity-dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ public static function send_profile_update( $user_id ) {
// Build the update.
$activity = new Activity();
$activity->set_type( 'Update' );
$activity->set_id( $user->get_id() . '#update-' . time() );
$activity->set_actor( $user->get_id() );
$activity->set_object( $user->get_id() );
$activity->set_to( array( 'https://www.w3.org/ns/activitystreams#Public' ) );
Expand Down
16 changes: 16 additions & 0 deletions tests/class-test-activitypub-activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,20 @@ public function test_activity_object() {
$this->assertEquals( 'Hello world!', $activity->get_object()->get_content() );
Assert::assertArraySubset( $test_array, $activity->to_array() );
}

/**
* Test activity object.
*/
public function test_activity_object_url() {
$id = 'https://example.com/author/123';

// Build the update.
$activity = new \Activitypub\Activity\Activity();
$activity->set_type( 'Update' );
$activity->set_actor( $id );
$activity->set_object( $id );
$activity->set_to( array( 'https://www.w3.org/ns/activitystreams#Public' ) );

$this->assertTrue( str_starts_with( $activity->get_id(), 'https://example.com/author/123#activity-update-' ) );
}
}

0 comments on commit 7f95415

Please sign in to comment.