diff --git a/CHANGELOG.md b/CHANGELOG.md index 5254fef11..4c07f008c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +* Post meta to log Outbox processing progress in the Outbox post type API endpoint. + ### Fixed * Updates to certain user meta fields did not trigger an Update activity. diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 0870d7613..b0e42012d 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -658,6 +658,35 @@ private static function register_post_types() { ) ); + \register_post_meta( + Outbox::POST_TYPE, + '_activitypub_outbox_processing', + array( + 'type' => 'object', + 'single' => true, + 'description' => 'Information about the processing of the outbox item.', + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'object', + 'properties' => array( + 'started' => array( + 'type' => 'string', + ), + 'completed' => array( + 'type' => 'string', + ), + 'total_inboxes' => array( + 'type' => 'integer', + ), + 'error_count' => array( + 'type' => 'integer', + ), + ), + ), + ), + ) + ); + // Both User and Blog Extra Fields types have the same args. $args = array( 'labels' => array( diff --git a/includes/class-dispatcher.php b/includes/class-dispatcher.php index 4ef43f886..78618c109 100644 --- a/includes/class-dispatcher.php +++ b/includes/class-dispatcher.php @@ -70,6 +70,9 @@ function ( $inboxes, $actor_id, $activity ) { 10, 3 ); + + \add_filter( 'activitypub_outbox_processing_complete', array( self::class, 'log_progress' ), 10, 7 ); + \add_filter( 'activitypub_outbox_processing_batch_complete', array( self::class, 'log_progress' ), 10, 7 ); } /** @@ -142,10 +145,11 @@ public static function send_to_followers( $outbox_item_id, $batch_size = 50, $of * @param string $json The ActivityPub Activity JSON * @param int $actor_id The actor ID. * @param int $outbox_item_id The Outbox item ID. + * @param array $retries The failed inboxes. * @param int $batch_size The batch size. * @param int $offset The offset. */ - \do_action( 'activitypub_outbox_processing_complete', $inboxes, $json, $actor->get__id(), $outbox_item_id, $batch_size, $offset ); + \do_action( 'activitypub_outbox_processing_complete', $inboxes, $json, $actor->get__id(), $outbox_item_id, $retries, $batch_size, $offset ); // No more followers to process for this update. \wp_publish_post( $outbox_item_id ); @@ -159,10 +163,11 @@ public static function send_to_followers( $outbox_item_id, $batch_size = 50, $of * @param string $json The ActivityPub Activity JSON * @param int $actor_id The actor ID. * @param int $outbox_item_id The Outbox item ID. + * @param array $retries The failed inboxes. * @param int $batch_size The batch size. * @param int $offset The offset. */ - \do_action( 'activitypub_outbox_processing_batch_complete', $inboxes, $json, $actor->get__id(), $outbox_item_id, $batch_size, $offset ); + \do_action( 'activitypub_outbox_processing_batch_complete', $inboxes, $json, $actor->get__id(), $outbox_item_id, $retries, $batch_size, $offset ); return array( $outbox_item_id, $batch_size, $offset + $batch_size ); } @@ -355,6 +360,39 @@ public static function add_inboxes_of_replied_urls( $inboxes, $actor_id, $activi return $inboxes; } + /** + * Track progress of outbox processing. + * + * @param array $inboxes The inboxes. + * @param string $json The ActivityPub Activity JSON. + * @param int $actor_id The actor ID. + * @param int $outbox_item_id The Outbox item ID. + * @param array $retries The failed inboxes. + * @param int $batch_size The batch size. + * @param int $offset The offset. + */ + public static function log_progress( $inboxes, $json, $actor_id, $outbox_item_id, $retries, $batch_size, $offset ) { + // Initialize processing data if this is the first batch. + if ( 0 === $offset ) { + $processing = array( + 'started' => current_time( 'mysql' ), + 'total_inboxes' => 0, + 'error_count' => 0, + ); + } else { + $processing = \get_post_meta( $outbox_item_id, '_activitypub_outbox_processing', true ); + } + + $processing['total_inboxes'] += count( $inboxes ); + $processing['error_count'] += count( $retries ); + + if ( 'activitypub_outbox_processing_complete' === current_filter() ) { + $processing['completed'] = current_time( 'mysql' ); + } + + \update_post_meta( $outbox_item_id, '_activitypub_outbox_processing', $processing ); + } + /** * Adds Blog Actor inboxes to Updates so the Blog User's followers are notified of edits. * diff --git a/readme.txt b/readme.txt index 6143c391c..d1ab56b6b 100644 --- a/readme.txt +++ b/readme.txt @@ -131,6 +131,7 @@ For reasons of data protection, it is not possible to see the followers of other = Unreleased = +* Added: Post meta to log Outbox processing progress in the Outbox post type API endpoint. * Fixed: Updates to certain user meta fields did not trigger an Update activity. = 5.4.1 =