Implement comment syncing
This commit is contained in:
parent
7b9f99112b
commit
79f741f332
@ -30,6 +30,9 @@ function handle_transition_comment_status( $new_status, $old_status, $comment )
|
||||
}
|
||||
// This creates a new commenter actor if necessary
|
||||
$actor_slug = get_comment_actor_slug( $comment );
|
||||
if ( is_wp_error( $actor_slug ) ) {
|
||||
return;
|
||||
}
|
||||
$actor_outbox = get_rest_url(
|
||||
null, sprintf( 'pterotype/v1/actor/%s/outbox', $actor_slug )
|
||||
);
|
||||
@ -95,6 +98,7 @@ function get_comment_email_actor_slug( $comment ) {
|
||||
}
|
||||
|
||||
function comment_to_object( $comment, $actor_slug ) {
|
||||
global $wpdb;
|
||||
$post = \get_post( $comment->comment_post_ID );
|
||||
\setup_postdata( $post );
|
||||
$post_permalink = \get_permalink( $post );
|
||||
@ -102,11 +106,17 @@ function comment_to_object( $comment, $actor_slug ) {
|
||||
$inReplyTo = $post_object['id'];
|
||||
if ( $comment->comment_parent !== '0' ) {
|
||||
$parent_comment = \get_comment( $comment->comment_parent );
|
||||
$parent_object = \pterotype\objects\get_object_by(
|
||||
'url', get_comment_object_url( \get_comment_link( $parent_comment ) )
|
||||
);
|
||||
if ( $parent_object ) {
|
||||
$inReplyTo = $parent_object['id'];
|
||||
$parent_object_activitypub_id = $wpdb->get_var( $wpdb->prepare(
|
||||
"
|
||||
SELECT activitypub_id FROM {$wpdb->prefix}pterotype_comments
|
||||
JOIN {$wpdb->prefix}pterotype_objects
|
||||
ON {$wpdb->prefix}pterotype_comments.object_id = {$wpdb->prefix}pterotype_objects.id
|
||||
WHERE comment_id = %d
|
||||
",
|
||||
$parent_comment->comment_ID
|
||||
) );
|
||||
if ( $parent_object_activitypub_id ) {
|
||||
$inReplyTo = $parent_object_activitypub_id;
|
||||
}
|
||||
}
|
||||
$link = get_comment_object_url ( \get_comment_link( $comment ) );
|
||||
@ -120,9 +130,18 @@ function comment_to_object( $comment, $actor_slug ) {
|
||||
'url' => $link,
|
||||
'inReplyTo' => $inReplyTo,
|
||||
);
|
||||
$existing = \pterotype\objects\get_object_by( 'url', $link );
|
||||
if ( $existing ) {
|
||||
$object['id'] = $existing['id'];
|
||||
$existing_activitypub_id = $parent_object_activitypub_id = $wpdb->get_var( $wpdb->prepare(
|
||||
"
|
||||
SELECT activitypub_id FROM {$wpdb->prefix}pterotype_comments
|
||||
JOIN {$wpdb->prefix}pterotype_objects
|
||||
ON {$wpdb->prefix}pterotype_comments.object_id = {$wpdb->prefix}pterotype_objects.id
|
||||
WHERE comment_id = %d
|
||||
",
|
||||
$comment->comment_ID
|
||||
) );
|
||||
|
||||
if ( $existing_activitypub_id ) {
|
||||
$object['id'] = $existing_activitypub_id;
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
|
@ -131,32 +131,62 @@ function link_comment( $object ) {
|
||||
function sync_comments( $activity ) {
|
||||
global $wpdb;
|
||||
$object = $activity['object'];
|
||||
$object_id = \pterotype\objects\get_object_id( $object['id'] );
|
||||
$comment_exists = $wpdb->get_row( $wpdb->prepare(
|
||||
"SELECT * FROM {$wpdb->prefix}pterotype_comments WHERE object_id = %d",
|
||||
$object_id
|
||||
) );
|
||||
if ( $comment_exists ) {
|
||||
return;
|
||||
}
|
||||
if ( ! array_key_exists( 'inReplyTo', $object ) ) {
|
||||
return;
|
||||
}
|
||||
$inReplyTo = \pterotype\util\dereference_object( $object['inReplyTo'] );
|
||||
$parent_row = \pterotype\objects\get_object_row_by_activity_id( $inReplyTo['id'] );
|
||||
if ( is_array( $object['inReplyTo'] ) && array_key_exists( 'id', $object['inReplyTo'] ) ) {
|
||||
$inReplyToId = $object['inReplyTo']['id'];
|
||||
} else {
|
||||
$inReplyToId = $object['inReplyTo'];
|
||||
}
|
||||
$parent_row = \pterotype\objects\get_object_row_by_activity_id( $inReplyToId );
|
||||
if ( ! $parent_row || is_wp_error( $parent_row ) ) {
|
||||
return;
|
||||
}
|
||||
$parent = $parent_row->object;
|
||||
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 = $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 );
|
||||
if ( $parent_comment_id ) {
|
||||
$parent_comment = \get_comment( $parent_comment_id );
|
||||
if ( ! $parent_comment ) {
|
||||
return;
|
||||
}
|
||||
$comment = make_comment_from_object( $object, $parent_comment->comment_post_ID, $parent_comment_id );
|
||||
$comment_id = \wp_new_comment( $comment );
|
||||
link_new_comment( $comment_id, $object_id );
|
||||
return;
|
||||
} else {
|
||||
$parent = $parent_row->object;
|
||||
if ( ! array_key_exists( 'url', $parent ) ) {
|
||||
return;
|
||||
}
|
||||
$url = $parent['url'];
|
||||
$post_id = \url_to_postid( $url );
|
||||
if ( $post_id === 0 ) {
|
||||
return;
|
||||
}
|
||||
$comment = make_comment_from_object( $object, $post_id, $parent_comment_id );
|
||||
$comment_id = \wp_new_comment( $comment );
|
||||
link_new_comment( $comment_id, $object_id );
|
||||
}
|
||||
}
|
||||
|
||||
function link_new_comment( $comment_id, $object_id ) {
|
||||
global $wpdb;
|
||||
return $wpdb->insert(
|
||||
"{$wpdb->prefix}pterotype_comments",
|
||||
array( 'comment_id' => $comment_id, 'object_id' => $object_id ),
|
||||
'%d'
|
||||
);
|
||||
}
|
||||
|
||||
function get_comment_id_from_url( $url ) {
|
||||
|
@ -258,7 +258,10 @@ function upsert_commenter_actor( $email_address, $url = null, $name = null, $ico
|
||||
if ( $existing !== null ) {
|
||||
return $slug;
|
||||
}
|
||||
create_actor( $slug, 'commenter', $email_address, $url, $name, $icon );
|
||||
$res = create_actor( $slug, 'commenter', $email_address, $url, $name, $icon );
|
||||
if ( is_wp_error( $res ) ) {
|
||||
return $res;
|
||||
}
|
||||
$actor_id = get_actor_id( $slug );
|
||||
$keys_created = \pterotype\pgp\get_public_key( $actor_id );
|
||||
if ( ! $keys_created ) {
|
||||
|
@ -16,7 +16,11 @@ function get_actor( $request ) {
|
||||
|
||||
function post_to_outbox( $request ) {
|
||||
$actor_slug = $request->get_url_params()['actor'];
|
||||
$activity = json_decode( $request->get_body(), true );
|
||||
$body = $request->get_body();
|
||||
$activity = $body;
|
||||
if ( is_string( $body ) ) {
|
||||
$activity = json_decode( $body, true );
|
||||
}
|
||||
return \pterotype\outbox\handle_activity( $actor_slug, $activity );
|
||||
}
|
||||
|
||||
@ -27,7 +31,11 @@ function get_outbox( $request ) {
|
||||
|
||||
function post_to_inbox( $request ) {
|
||||
$actor_slug = $request->get_url_params()['actor'];
|
||||
$activity = json_decode( $request->get_body(), true );
|
||||
$body = $request->get_body();
|
||||
$activity = $body;
|
||||
if ( is_string( $body ) ) {
|
||||
$activity = json_decode( $body, true );
|
||||
}
|
||||
return \pterotype\inbox\handle_activity( $actor_slug, $activity );
|
||||
}
|
||||
|
||||
|
@ -157,7 +157,7 @@ function get_now_date() {
|
||||
function get_signing_string( $inbox_url, $date_str ) {
|
||||
$parsed = parse_url( $inbox_url );
|
||||
$host = $parsed['host'];
|
||||
if ( $parsed['port'] ) {
|
||||
if ( array_key_exists( 'port', $parsed ) ) {
|
||||
$host = $host . ':' . $parsed['port'];
|
||||
}
|
||||
return "(request-target): post $parsed[path]\nhost: $host\ndate: $date_str";
|
||||
|
Loading…
Reference in New Issue
Block a user