Skip to content

Commit 6ef67e3

Browse files
authored
Merge branch 'trunk' into fix/outbox-object-id
2 parents e8039b9 + 958c452 commit 6ef67e3

File tree

6 files changed

+128
-1
lines changed

6 files changed

+128
-1
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: fixed
3+
4+
Use the `$from` account for the object in Move activity for internal Moves
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Prevent self-replies on local comments.

includes/class-dispatcher.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ public static function add_inboxes_of_replied_urls( $inboxes, $actor_id, $activi
335335
}
336336

337337
foreach ( $in_reply_to as $url ) {
338+
// No need to self-notify.
339+
if ( is_same_domain( $url ) ) {
340+
continue;
341+
}
342+
338343
$object = Http::get_remote_object( $url );
339344

340345
if (

includes/class-move.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static function internally( $from, $to ) {
130130
$activity->set_type( 'Move' );
131131
$activity->set_actor( $actor );
132132
$activity->set_origin( $actor );
133-
$activity->set_object( $to );
133+
$activity->set_object( $actor );
134134
$activity->set_target( $to );
135135

136136
return add_to_outbox( $activity, null, $user->get__id(), ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC );

tests/includes/class-test-dispatcher.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,84 @@ public function add_follower( $pre, $actor ) {
344344
'endpoints' => array( 'sharedInbox' => 'https://example.org/sharedInbox' ),
345345
);
346346
}
347+
348+
/**
349+
* Test that in_reply_to URLs from the same domain are ignored.
350+
*
351+
* @covers ::add_inboxes_of_replied_urls
352+
*/
353+
public function test_ignore_same_domain_in_reply_to() {
354+
// Create a test activity with in_reply_to pointing to same domain.
355+
$activity = new Activity();
356+
$activity->set_type( 'Create' );
357+
$activity->set_id( 'https://example.com/test-id' );
358+
$activity->set_in_reply_to( 'https://example.com/post/123' );
359+
360+
// Create a test actor.
361+
$actor_id = self::$user_id;
362+
363+
// Get inboxes for the activity.
364+
$inboxes = Dispatcher::add_inboxes_of_replied_urls( array(), $actor_id, $activity );
365+
366+
// Verify that no inboxes were added since the in_reply_to is from same domain.
367+
$this->assertEmpty( $inboxes, 'Inboxes should be empty for same domain in_reply_to URLs' );
368+
}
369+
370+
/**
371+
* Test that in_reply_to URLs from different domains are processed.
372+
*
373+
* @covers ::add_inboxes_of_replied_urls
374+
*/
375+
public function test_process_different_domain_in_reply_to() {
376+
// Create a test activity with in_reply_to pointing to different domain.
377+
$activity = new Activity();
378+
$activity->set_type( 'Create' );
379+
$activity->set_id( 'https://example.com/test-id' );
380+
$activity->set_in_reply_to( 'https://mastodon.social/@user/123456789' );
381+
382+
// Create a test actor.
383+
$actor_id = self::$user_id;
384+
385+
$callback = function ( $pre, $parsed_args, $url ) {
386+
if ( 'https://mastodon.social/@user/123456789' === $url ) {
387+
return array(
388+
'response' => array( 'code' => 200 ),
389+
'body' => \wp_json_encode(
390+
array(
391+
'type' => 'Note',
392+
'id' => 'https://mastodon.social/@user/123456789',
393+
'attributedTo' => 'https://mastodon.social/@user',
394+
)
395+
),
396+
);
397+
}
398+
399+
if ( 'https://mastodon.social/@user' === $url ) {
400+
return array(
401+
'response' => array( 'code' => 200 ),
402+
'body' => \wp_json_encode(
403+
array(
404+
'type' => 'Person',
405+
'id' => 'https://mastodon.social/@user',
406+
'inbox' => 'https://mastodon.social/inbox',
407+
)
408+
),
409+
);
410+
}
411+
412+
return $pre;
413+
};
414+
415+
// Mock the HTTP response for the remote object.
416+
add_filter( 'pre_http_request', $callback, 10, 3 );
417+
418+
// Get inboxes for the activity.
419+
$inboxes = Dispatcher::add_inboxes_of_replied_urls( array(), $actor_id, $activity );
420+
421+
// Verify that the inbox was added.
422+
$this->assertContains( 'https://mastodon.social/inbox', $inboxes, 'Inbox should be added for different domain in_reply_to URLs' );
423+
424+
// Clean up.
425+
remove_filter( 'pre_http_request', $callback );
426+
}
347427
}

tests/includes/class-test-move.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,38 @@ public function test_internally_with_valid_input() {
160160
$also_known_as = Actors::get_by_id( self::$user_id )->get_also_known_as();
161161
$this->assertContains( $from, $also_known_as );
162162
}
163+
164+
/**
165+
* Test that the Move Activity created by internally() has the correct properties.
166+
*
167+
* @covers ::internally
168+
*/
169+
public function test_internally_activity_object_properties() {
170+
$from = get_author_posts_url( self::$user_id );
171+
$to = Actors::get_by_id( self::$user_id )->get_id();
172+
173+
// Call the method and get the outbox item ID.
174+
$outbox_id = \Activitypub\Move::internally( $from, $to );
175+
176+
// Verify we got a valid outbox ID.
177+
$this->assertIsInt( $outbox_id );
178+
179+
// Get the outbox item from the database.
180+
$outbox_item = get_post( $outbox_id );
181+
182+
// Verify the outbox item exists.
183+
$this->assertNotNull( $outbox_item );
184+
185+
// Get the activity JSON from the outbox item.
186+
$activity = json_decode( $outbox_item->post_content );
187+
188+
// Verify the activity type is Move.
189+
$this->assertEquals( 'Move', $activity->type );
190+
191+
// Verify the activity object is set to the actor, not the target.
192+
$this->assertEquals( $from, $activity->object );
193+
$this->assertEquals( $from, $activity->actor );
194+
$this->assertEquals( $from, $activity->origin );
195+
$this->assertEquals( $to, $activity->target );
196+
}
163197
}

0 commit comments

Comments
 (0)