Skip to content

Commit b646fcb

Browse files
authored
Merge branch 'trunk' into store-quotes
2 parents adc9bc1 + a5ed897 commit b646fcb

File tree

3 files changed

+144
-6
lines changed

3 files changed

+144
-6
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+
Prevented self-announcing by ignoring announces from the blog actor, while still processing announces from user and external actors.

includes/handler/class-announce.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Activitypub\Handler;
99

10+
use Activitypub\Collection\Actors;
1011
use Activitypub\Collection\Interactions;
1112
use Activitypub\Comment;
1213
use Activitypub\Http;
@@ -40,6 +41,11 @@ public static function handle_announce( $announcement, $user_ids, $activity = nu
4041
return;
4142
}
4243

44+
// Ignore announces from the blog actor.
45+
if ( Actors::BLOG_USER_ID === Actors::get_id_by_resource( $announcement['actor'] ) ) {
46+
return;
47+
}
48+
4349
// Check if reposts are allowed.
4450
if ( ! Comment::is_comment_type_enabled( 'repost' ) ) {
4551
return;

tests/phpunit/tests/includes/handler/class-test-announce.php

Lines changed: 134 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Activitypub\Activity\Activity;
1111
use Activitypub\Handler\Announce;
12+
use Activitypub\Model\Blog;
1213

1314
/**
1415
* Test class for Activitypub Announce Handler.
@@ -104,13 +105,13 @@ public static function create_test_object() {
104105
* @covers ::handle_announce
105106
*/
106107
public function test_handle_announce() {
107-
$user_url = \get_userdata( $this->user_id )->user_url;
108+
$external_actor = 'https://example.com/users/testuser';
108109

109110
$object = array(
110-
'actor' => $user_url,
111+
'actor' => $external_actor,
111112
'type' => 'Announce',
112113
'id' => 'https://example.com/id/' . microtime( true ),
113-
'to' => array( $user_url ),
114+
'to' => array( $external_actor ),
114115
'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
115116
'object' => $this->post_permalink,
116117
);
@@ -156,13 +157,13 @@ public function test_handle_announces( $announce, $recursion, $message ) {
156157
* @covers ::maybe_save_announce
157158
*/
158159
public function test_maybe_save_announce() {
159-
$user_url = \get_userdata( $this->user_id )->user_url;
160+
$external_actor = 'https://example.com/users/testuser';
160161

161162
$activity = array(
162-
'actor' => $user_url,
163+
'actor' => $external_actor,
163164
'type' => 'Announce',
164165
'id' => 'https://example.com/id/' . microtime( true ),
165-
'to' => array( $user_url ),
166+
'to' => array( $external_actor ),
166167
'object' => $this->post_permalink,
167168
);
168169

@@ -221,4 +222,131 @@ public static function data_handle_announces() {
221222
),
222223
);
223224
}
225+
226+
/**
227+
* Test that announces from the blog actor are ignored.
228+
*
229+
* @covers ::handle_announce
230+
*/
231+
public function test_ignore_blog_actor_announce() {
232+
$blog = new Blog();
233+
$blog_url = $blog->get_id();
234+
235+
$object = array(
236+
'actor' => $blog_url,
237+
'type' => 'Announce',
238+
'id' => 'https://example.com/id/' . microtime( true ),
239+
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
240+
'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
241+
'object' => $this->post_permalink,
242+
);
243+
244+
// Set up mock action to track whether the announce is handled (should be ignored).
245+
$handled_action = new \MockAction();
246+
\add_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) );
247+
248+
// Call with blog actor as sender - should be ignored.
249+
Announce::handle_announce( $object, $this->user_id );
250+
251+
// Verify the announce was NOT handled.
252+
$this->assertEquals( 0, $handled_action->get_call_count() );
253+
254+
// Verify no comment was created.
255+
$args = array(
256+
'type' => 'repost',
257+
'post_id' => $this->post_id,
258+
);
259+
260+
$query = new \WP_Comment_Query( $args );
261+
$result = $query->comments;
262+
263+
$this->assertEmpty( $result );
264+
265+
\remove_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) );
266+
}
267+
268+
/**
269+
* Test that announces from external actors are not ignored.
270+
*
271+
* @covers ::handle_announce
272+
*/
273+
public function test_external_actor_announce_not_ignored() {
274+
$external_actor = 'https://external.example.com/users/someone';
275+
276+
$object = array(
277+
'actor' => $external_actor,
278+
'type' => 'Announce',
279+
'id' => 'https://external.example.com/id/' . microtime( true ),
280+
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
281+
'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
282+
'object' => $this->post_permalink,
283+
);
284+
285+
// Set up mock action to verify the announce is handled.
286+
$handled_action = new \MockAction();
287+
\add_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) );
288+
289+
// Call with external actor - should be processed.
290+
Announce::handle_announce( $object, $this->user_id );
291+
292+
// Verify the announce WAS handled.
293+
$this->assertEquals( 1, $handled_action->get_call_count() );
294+
295+
// Verify comment was created.
296+
$args = array(
297+
'type' => 'repost',
298+
'post_id' => $this->post_id,
299+
);
300+
301+
$query = new \WP_Comment_Query( $args );
302+
$result = $query->comments;
303+
304+
$this->assertNotEmpty( $result );
305+
$this->assertInstanceOf( 'WP_Comment', $result[0] );
306+
307+
\remove_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) );
308+
}
309+
310+
/**
311+
* Test that announces from same domain but different actor are not ignored.
312+
*
313+
* @covers ::handle_announce
314+
*/
315+
public function test_same_domain_different_actor_not_ignored() {
316+
// Get a regular user actor URL (not the blog actor).
317+
$user_url = \get_author_posts_url( $this->user_id );
318+
319+
$object = array(
320+
'actor' => $user_url,
321+
'type' => 'Announce',
322+
'id' => \home_url( '/activity/' . microtime( true ) ),
323+
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
324+
'cc' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
325+
'object' => $this->post_permalink,
326+
);
327+
328+
// Set up mock action to verify the announce is handled.
329+
$handled_action = new \MockAction();
330+
\add_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) );
331+
332+
// Call with same domain but user actor - should be processed.
333+
Announce::handle_announce( $object, $this->user_id );
334+
335+
// Verify the announce WAS handled.
336+
$this->assertEquals( 1, $handled_action->get_call_count() );
337+
338+
// Verify comment was created.
339+
$args = array(
340+
'type' => 'repost',
341+
'post_id' => $this->post_id,
342+
);
343+
344+
$query = new \WP_Comment_Query( $args );
345+
$result = $query->comments;
346+
347+
$this->assertNotEmpty( $result );
348+
$this->assertInstanceOf( 'WP_Comment', $result[0] );
349+
350+
\remove_action( 'activitypub_handled_announce', array( $handled_action, 'action' ) );
351+
}
224352
}

0 commit comments

Comments
 (0)