diff --git a/.github/changelog/1562-from-description b/.github/changelog/1562-from-description new file mode 100644 index 000000000..acf5ca947 --- /dev/null +++ b/.github/changelog/1562-from-description @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Incoming interactions create an Announce activity so other instances get notified about it. diff --git a/includes/class-dispatcher.php b/includes/class-dispatcher.php index cac61e66c..153eed96f 100644 --- a/includes/class-dispatcher.php +++ b/includes/class-dispatcher.php @@ -33,7 +33,7 @@ class Dispatcher { * @see https://github.com/tfredrich/RestApiTutorial.com/blob/fd08b0f67f07450521d143b123cd6e1846cb2e3b/content/advanced/responses/retries.md * @var int[] */ - public static $retry_error_codes = array( 408, 429, 500, 502, 503, 504 ); + public static $retry_error_codes = array( 400, 408, 429, 500, 502, 503, 504 ); /** * Initialize the class, registering WordPress hooks. diff --git a/includes/collection/class-interactions.php b/includes/collection/class-interactions.php index f532ecf00..aeecb6f1b 100644 --- a/includes/collection/class-interactions.php +++ b/includes/collection/class-interactions.php @@ -270,8 +270,9 @@ public static function activity_to_comment( $activity ) { 'comment_date' => \get_date_from_gmt( \gmdate( 'Y-m-d H:i:s', \strtotime( $date ) ) ), 'comment_date_gmt' => \gmdate( 'Y-m-d H:i:s', \strtotime( $date ) ), 'comment_meta' => array( - 'source_id' => \esc_url_raw( object_to_uri( $activity['object'] ) ), - 'protocol' => 'activitypub', + 'source_id' => \esc_url_raw( object_to_uri( $activity['object'] ) ), + 'protocol' => 'activitypub', + '_activitypub_activity' => $activity, ), ); diff --git a/includes/scheduler/class-comment.php b/includes/scheduler/class-comment.php index 09535f335..905a90fd7 100644 --- a/includes/scheduler/class-comment.php +++ b/includes/scheduler/class-comment.php @@ -7,6 +7,8 @@ namespace Activitypub\Scheduler; +use Activitypub\Activity\Activity; +use Activitypub\Collection\Actors; use Activitypub\Comment as Comment_Utils; use function Activitypub\add_to_outbox; @@ -45,9 +47,12 @@ public static function schedule_comment_activity( $new_status, $old_status, $com } $comment = get_comment( $comment ); + if ( ! $comment ) { + return; + } - // Federate only comments that are written by a registered user. - if ( ! $comment || ! $comment->user_id ) { + if ( ! $comment->user_id ) { + self::maybe_announce_interaction( $new_status, $old_status, $comment ); return; } @@ -81,6 +86,46 @@ public static function schedule_comment_activity( $new_status, $old_status, $com add_to_outbox( $comment, $type, $comment->user_id ); } + /** + * Announce an interaction. + * + * @param string $new_status The new comment status. + * @param string $old_status The old comment status. + * @param \WP_Comment $comment The comment object. + */ + public static function maybe_announce_interaction( $new_status, $old_status, $comment ) { + // Only if we're in both Blog and User modes. + if ( ACTIVITYPUB_ACTOR_AND_BLOG_MODE !== \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) ) { + return; + } + + if ( 'approved' !== $new_status || 'approved' === $old_status ) { + return; + } + + if ( ! Comment_Utils::was_received( $comment ) ) { + return; + } + + // Get activity from comment meta. + $activity = \get_comment_meta( $comment->comment_ID, '_activitypub_activity', true ); + + if ( ! $activity ) { + return; + } + + $activity['cc'][] = Actors::get_by_id( Actors::BLOG_USER_ID )->get_id(); + $activity['to'][] = Actors::get_by_id( Actors::BLOG_USER_ID )->get_id(); + $activity['object']['cc'][] = Actors::get_by_id( Actors::BLOG_USER_ID )->get_id(); + $activity['object']['to'][] = Actors::get_by_id( Actors::BLOG_USER_ID )->get_id(); + + $announce = new Activity(); + $announce->set_type( 'Announce' ); + $announce->set_object( $activity ); + + add_to_outbox( $announce, null, Actors::BLOG_USER_ID, ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC ); + } + /** * Schedule Comment Activities on insert. *