Skip to content

Commit d796e5e

Browse files
authored
Reply: Normalize for Pleroma (#1799)
1 parent 41909bc commit d796e5e

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
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+
Webfinger data from Pleroma instances no longer creates unexpected mention markup.

includes/transformer/class-post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public function generate_reply_link( $block_content, $block ) {
581581
// Get webfinger identifier.
582582
$webfinger = '';
583583
if ( ! empty( $author['webfinger'] ) ) {
584-
$webfinger = $author['webfinger'];
584+
$webfinger = \str_replace( 'acct:', '', $author['webfinger'] );
585585
} elseif ( ! empty( $author['preferredUsername'] ) && ! empty( $author['url'] ) ) {
586586
// Construct webfinger-style identifier from username and domain.
587587
$domain = \wp_parse_url( $author['url'], PHP_URL_HOST );

tests/includes/transformer/class-test-post.php

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Activitypub\Tests\Transformer;
99

1010
use Activitypub\Transformer\Post;
11-
use ReflectionClass;
1211

1312
/**
1413
* Test class for Post Transformer.
@@ -19,7 +18,7 @@ class Test_Post extends \WP_UnitTestCase {
1918
/**
2019
* Reflection method for testing protected method.
2120
*
22-
* @var ReflectionMethod
21+
* @var \ReflectionMethod
2322
*/
2423
private $reflection_method;
2524

@@ -32,7 +31,7 @@ public function set_up() {
3231
update_option( 'activitypub_object_type', 'wordpress-post-format' );
3332

3433
// Set up reflection method.
35-
$reflection = new ReflectionClass( Post::class );
34+
$reflection = new \ReflectionClass( Post::class );
3635
$this->reflection_method = $reflection->getMethod( 'get_type' );
3736
$this->reflection_method->setAccessible( true );
3837
}
@@ -654,4 +653,50 @@ public function test_preview_property() {
654653
// Check if the preview for a Note is null.
655654
$this->assertNull( $note_preview );
656655
}
656+
657+
/**
658+
* Test reply link generation.
659+
*
660+
* Pleroma prepends `acct:` to the webfinger identifier, which we'd want to normalize.
661+
*
662+
* @covers ::generate_reply_link
663+
*/
664+
public function test_generate_reply_link() {
665+
\add_filter( 'activitypub_pre_http_get_remote_object', array( $this, 'filter_pleroma_object' ), 10, 2 );
666+
667+
$transformer = new Post( self::factory()->post->create_and_get() );
668+
$reply_link = $transformer->generate_reply_link( '', array( 'attrs' => array( 'url' => 'https://devs.live/notice/AQ8N0Xl57y8bUQAb6e' ) ) );
669+
670+
$this->assertSame( '<p class="ap-reply-mention"><a rel="mention ugc" href="https://devs.live/notice/AQ8N0Xl57y8bUQAb6e" title="[email protected]">@tester</a></p>', $reply_link );
671+
672+
\remove_filter( 'activitypub_pre_http_get_remote_object', array( $this, 'filter_pleroma_object' ) );
673+
}
674+
675+
/**
676+
* Filter pleroma object.
677+
*
678+
* @param array|string|null $response The response.
679+
* @param array|string|null $url The Object URL.
680+
* @return string[]
681+
*/
682+
public function filter_pleroma_object( $response, $url ) {
683+
if ( 'https://devs.live/notice/AQ8N0Xl57y8bUQAb6e' === $url ) {
684+
$response = array(
685+
'type' => 'Note',
686+
'attributedTo' => 'https://devs.live/users/tester',
687+
'content' => 'Cake day it is',
688+
);
689+
}
690+
if ( 'https://devs.live/users/tester' === $url ) {
691+
$response = array(
692+
'id' => 'https://devs.live/users/tester',
693+
'type' => 'Person',
694+
'preferredUsername' => 'tester',
695+
'url' => 'https://devs.live/users/tester',
696+
'webfinger' => 'acct:[email protected]',
697+
);
698+
}
699+
700+
return $response;
701+
}
657702
}

0 commit comments

Comments
 (0)