From 4d5664e090ec9e1ddca9b54c6cb738677f87e96b Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Fri, 26 Oct 2018 09:23:19 -0400 Subject: [PATCH] [WIP] Start implementing inbound comments --- includes/init.php | 1 + includes/server/activities/create.php | 92 +++++++++++++++++++++++++++ includes/server/deliver.php | 6 -- includes/server/outbox.php | 3 - 4 files changed, 93 insertions(+), 9 deletions(-) diff --git a/includes/init.php b/includes/init.php index 6e1cd81..b53dd37 100644 --- a/includes/init.php +++ b/includes/init.php @@ -9,6 +9,7 @@ require_once plugin_dir_path( __FILE__ ) . 'server/webfinger.php'; require_once plugin_dir_path( __FILE__ ) . 'client/posts.php'; require_once plugin_dir_path( __FILE__ ) . 'client/comments.php'; require_once plugin_dir_path( __FILE__ ) . 'server/async.php'; +require_once plugin_dir_path( __FILE__ ) . 'pgp.php'; add_action( 'rest_api_init', function() { \pterotype\api\register_routes(); diff --git a/includes/server/activities/create.php b/includes/server/activities/create.php index 3a64be3..449ecc5 100644 --- a/includes/server/activities/create.php +++ b/includes/server/activities/create.php @@ -3,6 +3,7 @@ namespace pterotype\activities\create; require_once plugin_dir_path( __FILE__ ) . '../objects.php'; require_once plugin_dir_path( __FILE__ ) . '../actors.php'; +require_once plugin_dir_path( __FILE__ ) . '../../util.php'; /* Create a new post or comment (depending on $activity["object"]["type"]), @@ -54,6 +55,7 @@ function handle_inbox( $actor_slug, $activity ) { if ( is_wp_error( $object_row ) ) { return $object_row; } + sync_comments( $activity ); return $activity; } @@ -103,4 +105,94 @@ function make_create( $actor_slug, $object ) { ); return $activity; } + +function sync_comments( $activity ) { + $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 ) ) { + return; + } + if ( ! array_key_exists( 'url', $parent ) ) { + return; + } + if ( ! \pterotype\util\is_local_url( $parent['url'] ) ) { + return; + } + $url = $parent['url']; + $post_id = \url_to_postid( $url ); + if ( $post_id === 0 ) { + return; + } + $parent_comment_id = null; + if ( strpos( $url, '?pterotype_comment=' ) !== false ) { + $matches = array(); + preg_match( '/\?pterotype_comment=comment-(\d+)/', $url, $matches ); + $parent_comment_id = $matches[1]; + } + $comment = make_comment_from_object( $object, $post_id, $parent_comment_id ); + \wp_new_comment( $comment ); +} + +function make_comment_from_object( $object, $post_id, $parent_comment_id = null ) { + $actor = null; + if ( array_key_exists( 'attributedTo', $object ) ) { + $actor = \pterotype\util\dereference_object( $object['attributedTo'] ); + } else if ( array_key_exists( 'actor', $object ) ) { + $actor = \pterotype\util\dereference_object( $object['actor'] ); + } + if ( ! $actor || is_wp_error( $actor ) ) { + return; + } + $comment = array( + 'comment_author' => get_actor_name( $actor ), + 'comment_content' => $object['content'], + 'comment_post_ID' => $post_id, + 'comment_author_email' => get_actor_email( $actor ), + 'comment_type' => '', + ); + if ( $parent_comment_id ) { + $comment['comment_parent'] = $parent_comment_id; + } + if ( array_key_exists( 'url', $actor ) ) { + $comment['comment_author_url'] = $actor['url']; + } + return $comment; +} + +function get_actor_name( $actor ) { + if ( array_key_exists( 'name', $actor ) && ! empty( $actor['name'] ) ) { + return $actor['name']; + } + if ( array_key_exists( 'preferredUsername', $actor ) && + ! empty( $actor['preferredUsername' ] ) ) { + return $actor['preferredUsername']; + } + if ( array_key_exists( 'url', $actor ) && ! empty( $actor['url' ] ) ) { + return $actor['url']; + } + return $actor['id']; +} + +function get_actor_email( $actor ) { + $preferredUsername = $actor['id']; + if ( array_key_exists( 'preferredUsername', $actor ) ) { + $preferredUsername = $actor['preferredUsername']; + } else if ( array_key_exists( 'name', $actor ) ) { + $preferredUsername = str_replace( ' ', '_', $actor['name'] ); + } + $parsed = parse_url( $actor['id'] ); + if ( $parsed && array_key_exists( 'host', $parsed ) ) { + $host = $parsed['host']; + if ( array_key_exists( 'port', $parsed ) ) { + $host = $host . ':' . $parsed['port']; + } + return $preferredUsername . '@' . $host; + } else { + return $preferredUsername . '@fakeemails.getpterotype.com'; + } +} ?> diff --git a/includes/server/deliver.php b/includes/server/deliver.php index 5cb3345..cc4c036 100644 --- a/includes/server/deliver.php +++ b/includes/server/deliver.php @@ -144,12 +144,7 @@ function post_activity_to_inboxes( $actor_id, $activity, $recipients ) { ), 'data_format' => 'body', ); - \pterotype\util\log( 'debug.html', 'Request:' ); - \pterotype\util\log( 'debug.html', "POST $inbox" ); - \pterotype\util\log_var( 'debug.html', $args ); $response = wp_remote_post( $inbox, $args ); - \pterotype\util\log( 'debug.html', 'Response:' ); - \pterotype\util\log_var( 'debug.html', $response ); } } } @@ -172,7 +167,6 @@ function signature_header( $inbox_url, $actor_id, $date_str ) { $actor = \pterotype\actors\get_actor( $actor_id ); $key_id = $actor['publicKey']['id']; $signing_string = get_signing_string( $inbox_url, $date_str ); - \pterotype\util\log_var( 'debug.html', $signing_string ); $signature = \pterotype\pgp\sign_data( $signing_string, $actor_id ); $headers = '(request-target) host date'; return "keyId=\"$key_id\",headers=\"$headers\",signature=\"$signature\""; diff --git a/includes/server/outbox.php b/includes/server/outbox.php index 4d9280a..8923bcb 100644 --- a/includes/server/outbox.php +++ b/includes/server/outbox.php @@ -24,9 +24,6 @@ require_once plugin_dir_path( __FILE__ ) . 'activities/undo.php'; require_once plugin_dir_path( __FILE__ ) . '../util.php'; function handle_activity( $actor_slug, $activity ) { - \pterotype\util\log( 'outbox.html', 'Got outbox request', false ); - \pterotype\util\log_var( 'outbox.html', $actor_slug ); - \pterotype\util\log_var( 'outbox.html', $activity ); // TODO handle authentication/authorization $activity = \pterotype\util\dereference_object( $activity ); if ( is_wp_error( $activity ) ) {