diff --git a/includes/activity/class-activity.php b/includes/activity/class-activity.php index b8418315e..3ab28d388 100644 --- a/includes/activity/class-activity.php +++ b/includes/activity/class-activity.php @@ -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; } diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php index 29be63eb8..9540578af 100644 --- a/includes/class-activity-dispatcher.php +++ b/includes/class-activity-dispatcher.php @@ -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' ) ); diff --git a/tests/class-test-activitypub-activity.php b/tests/class-test-activitypub-activity.php index f8b9e9adc..9cbcbed4d 100644 --- a/tests/class-test-activitypub-activity.php +++ b/tests/class-test-activitypub-activity.php @@ -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-' ) ); + } }