Merge branch 'threaded-comments' into Comments

This commit is contained in:
Django Doucet 2023-02-04 15:35:01 -07:00
commit 627e5a9c0c
7 changed files with 254 additions and 77 deletions

View file

@ -20,7 +20,7 @@ class Hashtag {
/**
* Filter to save #tags as real WordPress tags
*
* @param int $id the rev-id
* @param int $id the rev-id
* @param WP_Post $post the post
*
* @return
@ -44,20 +44,31 @@ class Hashtag {
*/
public static function the_content( $the_content ) {
$protected_tags = array();
$protect = function( $m ) use ( &$protected_tags ) {
$c = count( $protected_tags );
$protect = '!#!#PROTECT' . $c . '#!#!';
$protected_tags[ $protect ] = $m[0];
return $protect;
};
$the_content = preg_replace_callback(
'#<!\[CDATA\[.*?\]\]>#is',
$protect,
$the_content
);
$the_content = preg_replace_callback(
'#<(pre|code|textarea|style)\b[^>]*>.*?</\1[^>]*>#is',
$protect,
$the_content
);
$the_content = preg_replace_callback(
'#<[^>]+>#i',
function( $m ) use ( &$protected_tags ) {
$c = count( $protected_tags );
$protect = '!#!#PROTECT' . $c . '#!#!';
$protected_tags[ $protect ] = $m[0];
return $protect;
},
$protect,
$the_content
);
$the_content = \preg_replace_callback( '/' . ACTIVITYPUB_HASHTAGS_REGEXP . '/i', array( '\Activitypub\Hashtag', 'replace_with_links' ), $the_content );
$the_content = str_replace( array_keys( $protected_tags ), array_values( $protected_tags ), $the_content );
$the_content = str_replace( array_reverse( array_keys( $protected_tags ) ), array_reverse( array_values( $protected_tags ) ), $the_content );
return $the_content;
}

View file

@ -24,20 +24,31 @@ class Mention {
*/
public static function the_content( $the_content ) {
$protected_tags = array();
$protect = function( $m ) use ( &$protected_tags ) {
$c = count( $protected_tags );
$protect = '!#!#PROTECT' . $c . '#!#!';
$protected_tags[ $protect ] = $m[0];
return $protect;
};
$the_content = preg_replace_callback(
'#<!\[CDATA\[.*?\]\]>#is',
$protect,
$the_content
);
$the_content = preg_replace_callback(
'#<(pre|code|textarea|style)\b[^>]*>.*?</\1[^>]*>#is',
$protect,
$the_content
);
$the_content = preg_replace_callback(
'#<a.*?href=[^>]+>.*?</a>#i',
function( $m ) use ( &$protected_tags ) {
$c = count( $protected_tags );
$protect = '!#!#PROTECT' . $c . '#!#!';
$protected_tags[ $protect ] = $m[0];
return $protect;
},
$protect,
$the_content
);
$the_content = \preg_replace_callback( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/', array( '\Activitypub\Mention', 'replace_with_links' ), $the_content );
$the_content = str_replace( array_keys( $protected_tags ), array_values( $protected_tags ), $the_content );
$the_content = str_replace( array_reverse( array_keys( $protected_tags ) ), array_reverse( array_values( $protected_tags ) ), $the_content );
return $the_content;
}
@ -68,7 +79,7 @@ class Mention {
/**
* Extract the mentions from the post_content.
*
* @param array $mentions The already found mentions.
* @param array $mentions The already found mentions.
* @param string $post_content The post content.
* @return mixed The discovered mentions.
*/

View file

@ -374,6 +374,47 @@ function url_to_authorid( $url ) {
return 0;
}
/**
* Examine a comment ID and look up an existing comment it represents.
*
* @param string $id ActivityPub object ID (usually a URL) to check.
*
* @return WP_Comment, or undef if no comment could be found.
*/
function object_id_to_comment( $id ) {
$comment_query = new \WP_Comment_Query(
array(
'meta_key' => 'source_id',
'meta_value' => $id,
)
);
if ( ! $comment_query->comments ) {
return;
}
if ( count( $comment_query->comments ) > 1 ) {
\error_log( "More than one comment under {$id}" );
return;
}
return $comment_query->comments[0];
}
/**
* Examine an activity object and find the post that the specified URL field refers to.
*
* @param string $field_name The name of the URL field in the object to query.
*
* @return int Post ID, or null on failure.
*/
function object_to_post_id_by_field_name( $object, $field_name ) {
if ( ! isset( $object['object'][ $field_name ] ) ) {
return;
}
$result = \url_to_postid( $object['object'][ $field_name ] );
if ( $result > 0 ) {
return $result;
}
}
/**
* Verify if in_replyto_url is a local Post,
* (For backwards compatibility)

View file

@ -426,6 +426,76 @@ class Inbox {
\add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
}
/**
* Converts a new ActivityPub object to comment data suitable for creating a comment
*
* @param array $object The activity-object.
*
* @return array Comment data suitable for creating a comment.
*/
public static function convert_object_to_comment_data( $object ) {
if ( ! isset( $object['object'] ) ) {
return false;
}
// check if Activity is public or not
if ( ! self::is_activity_public( $object ) ) {
// @todo maybe send email
return false;
}
$meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] );
// Objects must have IDs
if ( ! isset( $object['object']['id'] ) ) {
\error_log( 'Comment provided without ID' );
return;
}
$id = $object['object']['id'];
// Only handle replies
if ( ! isset( $object['object']['inReplyTo'] ) ) {
return;
}
$in_reply_to = $object['object']['inReplyTo'];
// Comment already exists
if ( \Activitypub\object_id_to_comment( $id ) ) {
return;
}
$parent_comment = \Activitypub\object_id_to_comment( $in_reply_to );
// save only replies and reactions
$comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'context' );
if ( ! $comment_post_id ) {
$comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'inReplyTo' );
}
if ( ! $comment_post_id ) {
$comment_post_id = $parent_comment->comment_post_ID;
}
if ( ! $comment_post_id ) {
return;
}
return array(
'comment_post_ID' => $comment_post_id,
'comment_author' => \esc_attr( $meta['name'] ),
'comment_author_url' => \esc_url_raw( $object['actor'] ),
'comment_content' => \wp_filter_kses( $object['object']['content'] ),
'comment_type' => '',
'comment_author_email' => '',
'comment_parent' => $parent_comment ? $parent_comment->comment_ID : 0,
'comment_meta' => array(
'ap_object' => \serialize( $object ),
'source_id' => \esc_url_raw( $id ),
'source_url' => \esc_url_raw( $object['object']['url'] ),
'avatar_url' => \esc_url_raw( $meta['icon']['url'] ),
'protocol' => 'activitypub',
),
);
}
/**
* Handles "Create" requests
*
@ -433,67 +503,11 @@ class Inbox {
* @param int $user_id The id of the local blog-user
*/
public static function handle_create( $object, $user_id ) {
$comment_post_id = 0;
$comment_parent = 0;
$comment_parent_id = 0;
$meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] );
// check if Activity is public or not
if ( ! self::is_activity_public( $object ) ) {
// @todo maybe send email
return;
$commentdata = self::convert_object_to_comment_data( $object );
if ( ! $commentdata ) {
return false;
}
$avatar_url = null;
$audience = \Activitypub\get_audience( $object );
if ( isset( $object['object']['inReplyTo'] ) ) {
$comment_parent_id = \Activitypub\url_to_commentid( \esc_url_raw( $object['object']['inReplyTo'] ) ); // Only checks ap_comment_id for local or source_url for remote
if ( ! is_null( $comment_parent_id ) ) {
//inReplyTo a known local comment
$comment_parent = \get_comment( $comment_parent_id );
$comment_post_id = $comment_parent->comment_post_ID;
} else {
//inReplyTo a known post
$comment_post_id = \Activitypub\url_to_postid( $object['object']['inReplyTo'] );
}
}
// if no name is set use peer username
if ( ! empty( $meta['name'] ) ) {
$name = \esc_attr( $meta['name'] );
} else {
$name = \esc_attr( $meta['preferredUsername'] );
}
// if avatar is set
if ( ! empty( $meta['icon']['url'] ) ) {
$avatar_url = \esc_url_raw( $meta['icon']['url'] );
}
//Only create WP_Comment for public replies to local posts
if ( ( 'public' === $audience )
|| ( 'unlisted' === $audience )
&& ( ! empty( $comment_post_id )
|| ! empty( $comment_parent_id )
) ) {
$commentdata = array(
'comment_post_ID' => $comment_post_id,
'comment_author' => $name,
'comment_author_url' => \esc_url_raw( $object['actor'] ),
'comment_content' => \wp_filter_kses( $object['object']['content'] ),
'comment_type' => '',
'comment_author_email' => '',
'comment_parent' => $comment_parent_id,
'comment_meta' => array(
'ap_object' => \serialize( $object ),
'source_url' => \esc_url_raw( $object['object']['id'] ),
'avatar_url' => $avatar_url,
'protocol' => 'activitypub',
),
);
// disable flood control
\remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );

View file

@ -5,6 +5,8 @@ class Test_Activitypub_Hashtag extends WP_UnitTestCase {
*/
public function test_the_content( $content, $content_with_hashtag ) {
\wp_create_term( 'object', 'post_tag' );
\wp_create_term( 'touch', 'post_tag' );
\wp_create_term( 'ccc', 'post_tag' );
$object = \get_term_by( 'name', 'object', 'post_tag' );
$link = \get_term_link( $object, 'post_tag' );
@ -14,6 +16,21 @@ class Test_Activitypub_Hashtag extends WP_UnitTestCase {
}
public function the_content_provider() {
$code = '<code>text with some #object and <a> tag inside</code>';
$style = <<<ENDSTYLE
<style type="text/css">
<![CDATA[
color: #ccc;
]]>
</style>
ENDSTYLE;
$pre = <<<ENDPRE
<pre>
Please don't #touch
this.
</pre>
ENDPRE;
$textarea = '<textarea name="test" rows="20">color: #ccc</textarea>';
return array(
array( 'test', 'test' ),
array( '#test', '#test' ),
@ -27,6 +44,10 @@ class Test_Activitypub_Hashtag extends WP_UnitTestCase {
array( '<div>#object</div>', '<div>#object</div>' ),
array( '<a>#object</a>', '<a>#object</a>' ),
array( '<div style="color: #ccc;">object</a>', '<div style="color: #ccc;">object</a>' ),
array( $code, $code ),
array( $style, $style ),
array( $textarea, $textarea ),
array( $pre, $pre ),
);
}
}

View file

@ -2,8 +2,8 @@
class Test_Activitypub_Mention extends ActivityPub_TestCase_Cache_HTTP {
public static $users = array(
'username@example.org' => array(
'url' => 'https://example.org/users/username',
'name' => 'username',
'url' => 'https://example.org/users/username',
'name' => 'username',
),
);
/**
@ -18,12 +18,21 @@ class Test_Activitypub_Mention extends ActivityPub_TestCase_Cache_HTTP {
}
public function the_content_provider() {
$code = 'hallo <code>@username@example.org</code> test';
$pre = <<<ENDPRE
<pre>
Please don't mention @username@example.org
here.
</pre>
ENDPRE;
return array(
array( 'hallo @username@example.org test', 'hallo <a rel="mention" class="u-url mention" href="https://example.org/users/username">@<span>username</span></a> test' ),
array( 'hallo @pfefferle@notiz.blog test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span></a> test' ),
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span>@notiz.blog</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@<span>pfefferle</span>@notiz.blog</a> test' ),
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@pfefferle@notiz.blog</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/author/matthias-pfefferle/">@pfefferle@notiz.blog</a> test' ),
array( 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/@pfefferle/">@pfefferle@notiz.blog</a> test', 'hallo <a rel="mention" class="u-url mention" href="https://notiz.blog/@pfefferle/">@pfefferle@notiz.blog</a> test' ),
array( $code, $code ),
array( $pre, $pre ),
);
}

View file

@ -0,0 +1,70 @@
<?php
class Test_Inbox extends WP_UnitTestCase {
#[\ReturnTypeWillChange]
var $post_permalink;
var $user_url;
public function setUp() {
$authordata = \get_userdata( 1 );
$this->user_url = $authordata->user_url;
$post = \wp_insert_post(
array(
'post_author' => 1,
'post_content' => 'test',
)
);
$this->post_permalink = \get_permalink( $post );
\add_filter( 'pre_get_remote_metadata_by_actor', array( '\Test_Inbox', 'get_remote_metadata_by_actor' ), 10, 2);
}
public static function get_remote_metadata_by_actor( $value, $actor ) {
return array(
"name" => "Example User",
"icon" => array(
"url" => "https://example.com/icon",
),
);
}
public function test_convert_object_to_comment_data_basic() {
$inbox = new \Activitypub\Rest\Inbox();
$object = array(
"actor" => $this->user_url,
"to" => [ $this->user_url ],
"cc" => [ "https://www.w3.org/ns/activitystreams#Public" ],
"object" => array(
"id" => "123",
"url" => "https://example.com/example",
"inReplyTo" => $this->post_permalink,
"content" => "example",
),
);
$converted = $inbox->convert_object_to_comment_data($object);
$this->assertGreaterThan(1, $converted["comment_post_ID"]);
$this->assertEquals($converted["comment_author"], "Example User");
$this->assertEquals($converted["comment_author_url"], "http://example.org");
$this->assertEquals($converted["comment_content"], "example");
$this->assertEquals($converted["comment_type"], "");
$this->assertEquals($converted["comment_author_email"], "");
$this->assertEquals($converted["comment_parent"], 0);
$this->assertArrayHasKey("comment_meta", $converted);
$this->assertEquals($converted["comment_meta"]["source_id"], "http://123");
$this->assertEquals($converted["comment_meta"]["source_url"], "https://example.com/example");
$this->assertEquals($converted["comment_meta"]["avatar_url"], "https://example.com/icon");
$this->assertEquals($converted["comment_meta"]["protocol"], "activitypub");
}
public function test_convert_object_to_comment_data_non_public_rejected() {
$inbox = new \Activitypub\Rest\Inbox();
$object = array(
"to" => ["https://example.com/profile/test"],
"cc" => [],
);
$converted = $inbox->convert_object_to_comment_data($object);
$this->assertFalse($converted);
}
}