Skip to content

Commit 6f3c6d3

Browse files
authored
Fix: duplicate content check (#1304)
* Fix: Duplicate comment check If a comment is sent twice and (because of a race condition) both will be stored in the db, this false check will allow that all new comments will be added as separate comments instead of extending the first one! * get lastest comment first * Add changelog * Fix Test * Add unittests * more descriptive changelogs
1 parent 44f91e5 commit 6f3c6d3

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
### Fixed
2424

2525
* Enforce 200 status header for valid ActivityPub requests.
26+
* `object_id_to_comment` returns a commment now, even if there are more than one matching comment in the DB.
2627
* Integration of content-visibility setup in the block editor.
2728
* Update CLI commands to the new scheduler refactorings.
2829
* Do not add an audience to the Actor-Profiles.

includes/class-comment.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,15 @@ public static function object_id_to_comment( $id ) {
264264
array(
265265
'meta_key' => 'source_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
266266
'meta_value' => $id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
267+
'orderby' => 'comment_date',
268+
'order' => 'DESC',
267269
)
268270
);
269271

270272
if ( ! $comment_query->comments ) {
271273
return false;
272274
}
273275

274-
if ( count( $comment_query->comments ) > 1 ) {
275-
return false;
276-
}
277-
278276
return $comment_query->comments[0];
279277
}
280278

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ For reasons of data protection, it is not possible to see the followers of other
139139
* Changed: Enabled querying of Outbox posts through the REST API to improve troubleshooting and debugging.
140140
* Changed: Updated terminology to be client-neutral in the Federated Reply block.
141141
* Fixed: Enforce 200 status header for valid ActivityPub requests.
142+
* Fixed: `object_id_to_comment` returns a commment now, even if there are more than one matching comment in the DB.
142143
* Fixed: Integration of content-visibility setup in the block editor.
143144
* Fixed: Update CLI commands to the new scheduler refactorings.
144145
* Fixed: Do not add an audience to the Actor-Profiles.

tests/includes/class-test-comment.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,54 @@ public function test_get_comment_type_names() {
592592
$this->assertNotContains( 'Repost', $names );
593593
$this->assertNotContains( 'Like', $names );
594594
}
595+
596+
/**
597+
* Test object_id_to_comment method.
598+
*
599+
* @covers ::object_id_to_comment
600+
*/
601+
public function test_object_id_to_comment() {
602+
$source_id = 'https://example.com/1';
603+
604+
// No comment with the same source_id.
605+
$comment_0 = Comment::object_id_to_comment( $source_id );
606+
$this->assertFalse( $comment_0 );
607+
608+
// Create a comment with the same source_id.
609+
$id_1 = self::factory()->comment->create();
610+
add_comment_meta( $id_1, 'source_id', $source_id, true );
611+
612+
// Get the comment with the same source_id.
613+
$comment_1 = Comment::object_id_to_comment( $source_id );
614+
$this->assertEquals( $id_1, $comment_1->comment_ID );
615+
616+
// Create another comment with the same source_id.
617+
$id_2 = self::factory()->comment->create(
618+
array(
619+
'comment_date' => '2024-01-01 00:00:00',
620+
)
621+
);
622+
add_comment_meta( $id_2, 'source_id', $source_id, true );
623+
624+
// Get the comment with the same source_id.
625+
$comment_2 = Comment::object_id_to_comment( $source_id );
626+
$this->assertEquals( $id_1, $comment_2->comment_ID );
627+
628+
// Create another comment with the same source_id.
629+
$id_3 = self::factory()->comment->create(
630+
array(
631+
'comment_date' => '2024-01-01 00:00:00',
632+
)
633+
);
634+
add_comment_meta( $id_3, 'source_id', $source_id, true );
635+
636+
// Get the comment with the same source_id.
637+
$comment_3 = Comment::object_id_to_comment( $source_id );
638+
$this->assertEquals( $id_1, $comment_3->comment_ID );
639+
640+
// Delete the comments.
641+
wp_delete_comment( $id_1, true );
642+
wp_delete_comment( $id_2, true );
643+
wp_delete_comment( $id_3, true );
644+
}
595645
}

tests/includes/class-test-functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function test_object_id_to_comment_duplicate() {
122122
remove_filter( 'wp_is_comment_flood', '__return_false', 99 );
123123

124124
$query_result = \Activitypub\object_id_to_comment( $duplicate_comment_source_id );
125-
$this->assertFalse( $query_result );
125+
$this->assertInstanceOf( \WP_Comment::class, $query_result );
126126
}
127127

128128
/**

0 commit comments

Comments
 (0)