From 9453fd6299556a0aef67eb33a2ce63f01e714356 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Fri, 26 Oct 2018 15:01:14 -0400 Subject: [PATCH] Properly link comments to AP objects --- includes/server/activities/create.php | 43 +++++++++++++++++++++++---- includes/server/objects.php | 15 ++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/includes/server/activities/create.php b/includes/server/activities/create.php index 449ecc5..ef8511e 100644 --- a/includes/server/activities/create.php +++ b/includes/server/activities/create.php @@ -39,6 +39,7 @@ function handle_outbox( $actor_slug, $activity ) { return $object; } $activity['object'] = $object; + link_comment( $object ); return $activity; } @@ -106,16 +107,39 @@ function make_create( $actor_slug, $object ) { return $activity; } +function link_comment( $object ) { + global $wpdb; + $object = \pterotype\util\dereference_object( $object ); + if ( ! array_key_exists( 'url', $object ) ) { + return; + } + if ( ! \pterotype\util\is_local_url( $object['url'] ) ) { + return; + } + $comment_id = get_comment_id_from_url( $object['url'] ); + if ( ! $comment_id ) { + return; + } + $object_id = \pterotype\objects\get_object_id( $object['id'] ); + $wpdb->insert( + "{$wpdb->prefix}pterotype_comments", + array( 'comment_id' => $comment_id, 'object_id' => $object_id ), + '%d' + ); +} + function sync_comments( $activity ) { + global $wpdb; $object = $activity['object']; if ( ! array_key_exists( 'inReplyTo', $object ) ) { return; } $inReplyTo = \pterotype\util\dereference_object( $object['inReplyTo'] ); - $parent = \pterotype\objects\get_object_by_activitypub_id( $inReplyTo['id'] ); - if ( ! $parent || is_wp_error( $parent ) ) { + $parent_row = \pterotype\objects\get_object_row_by_activity_id( $inReplyTo['id'] ); + if ( ! $parent_row || is_wp_error( $parent_row ) ) { return; } + $parent = $parent_row->object; if ( ! array_key_exists( 'url', $parent ) ) { return; } @@ -127,14 +151,21 @@ function sync_comments( $activity ) { if ( $post_id === 0 ) { return; } - $parent_comment_id = null; + $parent_comment_id = $wpdb->get_var( $wpdb->prepare( + "SELECT comment_id FROM {$wpdb->prefix}pterotype_comments WHERE object_id = %d", + $parent_row->id + ) ); + $comment = make_comment_from_object( $object, $post_id, $parent_comment_id ); + \wp_new_comment( $comment ); +} + +function get_comment_id_from_url( $url ) { if ( strpos( $url, '?pterotype_comment=' ) !== false ) { $matches = array(); preg_match( '/\?pterotype_comment=comment-(\d+)/', $url, $matches ); - $parent_comment_id = $matches[1]; + return $matches[1]; } - $comment = make_comment_from_object( $object, $post_id, $parent_comment_id ); - \wp_new_comment( $comment ); + return null; } function make_comment_from_object( $object, $post_id, $parent_comment_id = null ) { diff --git a/includes/server/objects.php b/includes/server/objects.php index 57ec5b4..8a40c59 100644 --- a/includes/server/objects.php +++ b/includes/server/objects.php @@ -197,6 +197,21 @@ function get_object_by_activitypub_id( $activitypub_id ) { return json_decode( $object_json, true ); } +function get_object_row_by_activity_id( $activitypub_id ) { + global $wpdb; + $row = $wpdb->get_row( $wpdb->prepare( + "SELECT * FROM {$wpdb->prefix}pterotype_objects WHERE activitypub_id = %s", + $activitypub_id + ) ); + if ( is_null( $row ) ) { + return new \WP_Error( + 'not_found', __( 'Object not found', 'pterotype' ), array( 'status' => 404 ) + ); + } + $row->object = json_decode( $row->object, true ); + return $row; +} + function get_object_id( $activitypub_id ) { global $wpdb; return $wpdb->get_var( $wpdb->prepare(