Skip to content

Commit

Permalink
Split out add_to_outbox method
Browse files Browse the repository at this point in the history
  • Loading branch information
obenland committed Jan 20, 2025
1 parent 981b5c2 commit 8766331
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions includes/class-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public static function create_outbox_items( $batch_size = 100, $offset = 0 ) {
if ( self::is_locked() ) {
\wp_schedule_single_event(
time() + ( 5 * MINUTE_IN_SECONDS ),
'activitypub_update_comment_counts',
'activitypub_create_outbox_items',
array(
'batch_size' => $batch_size,
'offset' => $offset,
Expand Down Expand Up @@ -551,29 +551,14 @@ public static function create_outbox_items( $batch_size = 100, $offset = 0 ) {
// Avoid multiple queries for post meta.
\update_postmeta_cache( \wp_list_pluck( $posts, 'ID' ) );

// Local function to add an activity to the outbox.
$add_to_outbox = function ( $comment, $activity_type, $user_id, $visibility = ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC ) {
$transformer = Factory::get_transformer( $comment );
if ( ! $transformer || \is_wp_error( $transformer ) ) {
return;
}

$activity = $transformer->to_object();
if ( ! $activity || \is_wp_error( $activity ) ) {
return;
}

Outbox::add( $activity, $activity_type, $user_id, $visibility );
};

foreach ( $posts as $post ) {
$visibility = \get_post_meta( $post->ID, 'activitypub_content_visibility', true );

$add_to_outbox( $post, 'Create', $post->post_author, $visibility );
self::add_to_outbox( $post, 'Create', $post->post_author, $visibility );

// Add Update activity when the post has been modified.
if ( $post->post_modified !== $post->post_date ) {
$add_to_outbox( $post, 'Update', $post->post_author, $visibility );
self::add_to_outbox( $post, 'Update', $post->post_author, $visibility );
}
}

Expand All @@ -586,14 +571,14 @@ public static function create_outbox_items( $batch_size = 100, $offset = 0 ) {
);

foreach ( $comments as $comment ) {
$add_to_outbox( $comment, 'Create', $comment->user_id );
self::add_to_outbox( $comment, 'Create', $comment->user_id );
}

if ( count( $posts ) === $batch_size || count( $comments ) === $batch_size ) {
// Schedule next batch.
\wp_schedule_single_event(
time() + MINUTE_IN_SECONDS,
'activitypub_update_comment_counts',
'activitypub_create_outbox_items',
array(
'batch_size' => $batch_size,
'offset' => $offset + $batch_size,
Expand All @@ -614,6 +599,28 @@ public static function add_default_settings() {
self::add_notification_defaults();
}

/**
* Add an activity to the outbox without federating it.
*
* @param \WP_Post|\WP_Comment $comment The comment or post object.
* @param string $activity_type The type of activity.
* @param int $user_id The user ID.
* @param string $visibility Optional. The visibility of the content. Default 'public'.
*/
private static function add_to_outbox( $comment, $activity_type, $user_id, $visibility = ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC ) {
$transformer = Factory::get_transformer( $comment );
if ( ! $transformer || \is_wp_error( $transformer ) ) {
return;
}

$activity = $transformer->to_object();
if ( ! $activity || \is_wp_error( $activity ) ) {
return;
}

Outbox::add( $activity, $activity_type, $user_id, $visibility );
}

/**
* Add the ActivityPub capability to all users that can publish posts.
*/
Expand Down

0 comments on commit 8766331

Please sign in to comment.