refactor support for threaded comments from ActivityPub
This commit is contained in:
parent
f1f3c3165d
commit
6a906e5fe2
2 changed files with 84 additions and 39 deletions
|
@ -311,3 +311,40 @@ function url_to_authorid( $url ) {
|
||||||
|
|
||||||
return 0;
|
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] );
|
||||||
|
\error_log( "@@@ found result for " . $field_name . ": " . $result );
|
||||||
|
if ( $result > 0 ) {
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -425,12 +425,13 @@ class Inbox {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles "Create" requests
|
* Converts a new ActivityPub object to comment data suitable for creating a comment
|
||||||
*
|
*
|
||||||
* @param array $object The activity-object
|
* @param array $object The activity-object.
|
||||||
* @param int $user_id The id of the local blog-user
|
*
|
||||||
|
* @return array Comment data suitable for creating a comment.
|
||||||
*/
|
*/
|
||||||
public static function handle_create( $object, $user_id ) {
|
private static function convert_object_to_comment_data( $object ) {
|
||||||
// check if Activity is public or not
|
// check if Activity is public or not
|
||||||
if ( ! self::is_activity_public( $object ) ) {
|
if ( ! self::is_activity_public( $object ) ) {
|
||||||
// @todo maybe send email
|
// @todo maybe send email
|
||||||
|
@ -439,55 +440,62 @@ class Inbox {
|
||||||
|
|
||||||
$meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] );
|
$meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] );
|
||||||
|
|
||||||
$comment_post_id = 0;
|
// Objects must have IDs
|
||||||
// TODO: search for an existing comment with the source url and abandon if it's already there
|
if ( ! isset( $object['object']['id'] ) ) {
|
||||||
|
\error_log( "Comment provided without ID" );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$id = $object['object']['id'];
|
||||||
|
|
||||||
if ( isset( $object['object']['context'] ) ) {
|
// Only handle replies
|
||||||
$comment_post_id = \url_to_postid( $object['object']['context'] );
|
if ( ! isset( $object['object']['inReplyTo'] ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$in_reply_to = $object['object']['inReplyTo'];
|
||||||
|
|
||||||
|
// Comment already exists
|
||||||
|
if ( \Activitypub\object_id_to_comment( $id ) ) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! $comment_post_id && isset( $object['object']['inReplyTo'] ) ) {
|
$parent_comment = \Activitypub\object_id_to_comment( $in_reply_to );
|
||||||
$comment_post_id = \url_to_postid( $object['object']['inReplyTo'] );
|
|
||||||
}
|
|
||||||
|
|
||||||
$comment_parent_id = 0;
|
// save only replies and reactions
|
||||||
$comment_meta = array(
|
$comment_post_id = \Activitypub\object_to_post_id_by_field_name( $object, 'context' ) ??
|
||||||
'source_id' => \esc_url_raw( $object['object']['id'] ),
|
\Activitypub\object_to_post_id_by_field_name( $object, 'inReplyTo' ) ??
|
||||||
'source_url' => \esc_url_raw( $object['object']['url'] ),
|
( $parent_comment ? $parent_comment->comment_post_ID : 0 );
|
||||||
'avatar_url' => \esc_url_raw( $meta['icon']['url'] ),
|
|
||||||
'protocol' => 'activitypub',
|
|
||||||
);
|
|
||||||
|
|
||||||
if ( isset( $object['object']['inReplyTo'] ) ) {
|
|
||||||
$comment_meta['parent_url'] = $object['object']['inReplyTo'];
|
|
||||||
$comment_query = new \WP_Comment_Query( array( 'meta_key' => 'source_id', 'meta_value' => $object['object']['inReplyTo'] ) );
|
|
||||||
if ( $comment_query->comments ) {
|
|
||||||
foreach ( $comment_query->comments as $comment ) {
|
|
||||||
if ( ! $comment_parent_id ) {
|
|
||||||
$comment_parent_id = $comment->comment_ID;
|
|
||||||
}
|
|
||||||
if ( ! $comment_post_id ) {
|
|
||||||
$comment_post_id = $comment->comment_post_ID;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// save only replys and reactions
|
|
||||||
if ( ! $comment_post_id ) {
|
if ( ! $comment_post_id ) {
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$commentdata = array(
|
return array(
|
||||||
'comment_post_ID' => $comment_post_id,
|
'comment_post_ID' => $comment_post_id,
|
||||||
'comment_author' => \esc_attr( $meta['name'] ),
|
'comment_author' => \esc_attr( $meta['name'] ),
|
||||||
'comment_author_url' => \esc_url_raw( $object['actor'] ),
|
'comment_author_url' => \esc_url_raw( $object['actor'] ),
|
||||||
'comment_content' => \wp_filter_kses( $object['object']['content'] ),
|
'comment_content' => \wp_filter_kses( $object['object']['content'] ),
|
||||||
'comment_type' => '',
|
'comment_type' => '',
|
||||||
'comment_author_email' => '',
|
'comment_author_email' => '',
|
||||||
'comment_parent' => $comment_parent_id,
|
'comment_parent' => $parent_comment ? $parent_comment->comment_ID : 0,
|
||||||
'comment_meta' => $comment_meta,
|
'comment_meta' => array(
|
||||||
|
'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
|
||||||
|
*
|
||||||
|
* @param array $object The activity-object
|
||||||
|
* @param int $user_id The id of the local blog-user
|
||||||
|
*/
|
||||||
|
public static function handle_create( $object, $user_id ) {
|
||||||
|
$commentdata = self::convert_object_to_comment_data( $object );
|
||||||
|
if ( !$commentdata ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// disable flood control
|
// disable flood control
|
||||||
\remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );
|
\remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );
|
||||||
|
|
Loading…
Reference in a new issue