Skip to content

Commit 2e197e9

Browse files
authored
Interactions: Don't show likes and reposts for comments (#1735)
1 parent 4235839 commit 2e197e9

File tree

7 files changed

+73
-14
lines changed

7 files changed

+73
-14
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Likes and Reposts for comments to a post are no longer attributed to the post itself.

includes/class-mailer.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,26 @@ public static function comment_notification_text( $message, $comment_id ) {
8888
$post = \get_post( $comment->comment_post_ID );
8989
$comment_author_domain = \gethostbyaddr( $comment->comment_author_IP );
9090

91-
/* translators: 1: Comment type, 2: Post title */
92-
$notify_message = \sprintf( html_entity_decode( esc_html__( 'New %1$s on your post “%2$s”.', 'activitypub' ) ), \esc_html( $comment_type['singular'] ), \esc_html( $post->post_title ) ) . "\r\n\r\n";
91+
// Check if this is a reaction to a post or a comment.
92+
if ( 0 === (int) $comment->comment_parent ) {
93+
$notify_message = \sprintf(
94+
/* translators: 1: Comment type, 2: Post title */
95+
\html_entity_decode( esc_html__( 'New %1$s on your post “%2$s”.', 'activitypub' ) ),
96+
\esc_html( $comment_type['singular'] ),
97+
\esc_html( $post->post_title )
98+
) . PHP_EOL . PHP_EOL;
99+
100+
} else {
101+
$parent_comment = \get_comment( $comment->comment_parent );
102+
$notify_message = \sprintf(
103+
/* translators: 1: Comment type, 2: Post title, 3: Parent comment author */
104+
\html_entity_decode( esc_html__( 'New %1$s on your post “%2$s” in reply to %3$s’s comment.', 'activitypub' ) ),
105+
\esc_html( $comment_type['singular'] ),
106+
\esc_html( $post->post_title ),
107+
\esc_html( $parent_comment->comment_author )
108+
) . PHP_EOL . PHP_EOL;
109+
}
110+
93111
/* translators: 1: Website name, 2: Website IP address, 3: Website hostname. */
94112
$notify_message .= \sprintf( \esc_html__( 'From: %1$s (IP address: %2$s, %3$s)', 'activitypub' ), \esc_html( $comment->comment_author ), \esc_html( $comment->comment_author_IP ), \esc_html( $comment_author_domain ) ) . "\r\n";
95113
/* translators: Reaction author URL. */

includes/collection/class-interactions.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,9 @@ public static function update_comment( $activity ) {
8989
*
9090
* @param array $activity Activity array.
9191
*
92-
* @return array|false Comment data or `false` on failure.
92+
* @return array|false Comment data or `false` on failure.
9393
*/
9494
public static function add_reaction( $activity ) {
95-
$comment_data = self::activity_to_comment( $activity );
96-
97-
if ( ! $comment_data ) {
98-
return false;
99-
}
100-
10195
$url = object_to_uri( $activity['object'] );
10296
$comment_post_id = \url_to_postid( $url );
10397
$parent_comment_id = url_to_commentid( $url );
@@ -113,16 +107,19 @@ public static function add_reaction( $activity ) {
113107
}
114108

115109
$comment_type = Comment::get_comment_type_by_activity_type( $activity['type'] );
116-
117110
if ( ! $comment_type ) {
118111
// Not a valid comment type.
119112
return false;
120113
}
121114

122-
$comment_content = $comment_type['excerpt'];
115+
$comment_data = self::activity_to_comment( $activity );
116+
if ( ! $comment_data ) {
117+
return false;
118+
}
123119

124120
$comment_data['comment_post_ID'] = $comment_post_id;
125-
$comment_data['comment_content'] = \esc_html( $comment_content );
121+
$comment_data['comment_parent'] = $parent_comment_id ? $parent_comment_id : 0;
122+
$comment_data['comment_content'] = \esc_html( $comment_type['excerpt'] );
126123
$comment_data['comment_type'] = \esc_attr( $comment_type['type'] );
127124
$comment_data['comment_meta']['source_id'] = \esc_url_raw( $activity['id'] );
128125

includes/rest/class-post-controller.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public function get_reactions( $request ) {
9898
'post_id' => $post_id,
9999
'type' => $type_object['type'],
100100
'status' => 'approve',
101+
'parent' => 0,
101102
)
102103
);
103104

src/reactions/render.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'post_id' => $_post_id,
3434
'type' => $_type,
3535
'status' => 'approve',
36+
'parent' => 0,
3637
)
3738
);
3839

tests/includes/collection/class-test-interactions.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
namespace Activitypub\Tests\Collection;
99

1010
use Activitypub\Collection\Interactions;
11-
use WP_UnitTestCase;
1211

1312
/**
1413
* Test class for Activitypub Interactions.
1514
*
1615
* @coversDefaultClass \Activitypub\Collection\Interactions
1716
*/
18-
class Test_Interactions extends WP_UnitTestCase {
17+
class Test_Interactions extends \WP_UnitTestCase {
1918

2019
/**
2120
* User ID.
@@ -489,6 +488,8 @@ public function test_no_likes_reposts_when_disabled() {
489488

490489
$result = Interactions::add_reaction( $activity );
491490
$this->assertFalse( $result, 'Likes and reposts should not be collected when disabled.' );
491+
492+
\delete_option( 'activitypub_allow_likes' );
492493
}
493494

494495
/**
@@ -508,6 +509,8 @@ public function test_no_reposts_when_disabled() {
508509

509510
$result = Interactions::add_reaction( $activity );
510511
$this->assertFalse( $result, 'Reposts should not be collected when disabled.' );
512+
513+
\delete_option( 'activitypub_allow_reposts' );
511514
}
512515

513516
/**

tests/includes/rest/class-test-post-controller.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,39 @@ public function test_get_reactions_respects_approval() {
191191
$this->assertEquals( 200, $response->get_status() );
192192
$this->assertEmpty( $response->get_data() );
193193
}
194+
195+
/**
196+
* Test getting reactions respects comment approval status.
197+
*
198+
* @covers ::get_reactions
199+
*/
200+
public function test_get_reactions_skips_comment_reactions() {
201+
$post_id = self::factory()->post->create();
202+
$comment_id = self::factory()->comment->create(
203+
array(
204+
'comment_post_ID' => $post_id,
205+
'comment_type' => 'comment',
206+
'comment_content' => 'Test Comment',
207+
)
208+
);
209+
self::factory()->comment->create(
210+
array(
211+
'comment_post_ID' => $post_id,
212+
'comment_author' => 'Test User',
213+
'comment_author_url' => 'https://example.com/user',
214+
'comment_author_email' => '',
215+
'comment_content' => '',
216+
'comment_type' => 'like',
217+
'comment_parent' => $comment_id,
218+
'user_id' => 0,
219+
'comment_approved' => 1,
220+
)
221+
);
222+
223+
$request = new WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/posts/' . $post_id . '/reactions' );
224+
$response = $this->server->dispatch( $request );
225+
226+
$this->assertEquals( 200, $response->get_status() );
227+
$this->assertEmpty( $response->get_data() );
228+
}
194229
}

0 commit comments

Comments
 (0)