Skip to content

Commit

Permalink
Prevent scheduler overload (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfefferle authored Feb 23, 2024
1 parent 4d529e8 commit 9cf521c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 29 deletions.
53 changes: 53 additions & 0 deletions includes/class-activity-dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class Activity_Dispatcher {
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action( 'activitypub_send_post', array( self::class, 'send_post' ), 10, 2 );
\add_action( 'activitypub_send_comment', array( self::class, 'send_comment' ), 10, 2 );

\add_action( 'activitypub_send_activity', array( self::class, 'send_activity' ), 10, 2 );
\add_action( 'activitypub_send_activity', array( self::class, 'send_activity_or_announce' ), 10, 2 );
\add_action( 'activitypub_send_update_profile_activity', array( self::class, 'send_profile_update' ), 10, 1 );
Expand Down Expand Up @@ -172,4 +175,54 @@ private static function send_activity_to_followers( $activity, $user_id, $wp_obj

set_wp_object_state( $wp_object, 'federated' );
}

/**
* Send a "Create" or "Update" Activity for a WordPress Post.
*
* @param int $id The WordPress Post ID.
* @param string $type The Activity-Type.
*
* @return void
*/
public static function send_post( $id, $type ) {
$post = get_post( $id );

if ( ! $post ) {
return;
}

do_action( 'activitypub_send_activity', $post, $type );
do_action(
sprintf(
'activitypub_send_%s_activity',
\strtolower( $type )
),
$post
);
}

/**
* Send a "Create" or "Update" Activity for a WordPress Comment.
*
* @param int $id The WordPress Comment ID.
* @param string $type The Activity-Type.
*
* @return void
*/
public static function send_comment( $id, $type ) {
$comment = get_comment( $id );

if ( ! $comment ) {
return;
}

do_action( 'activitypub_send_activity', $comment, $type );
do_action(
sprintf(
'activitypub_send_%s_activity',
\strtolower( $type )
),
$comment
);
}
}
1 change: 0 additions & 1 deletion includes/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ function ( $allcaps, $caps, $arg ) {
public static function comment_row_actions( $actions, $comment ) {
if ( was_comment_received( $comment ) ) {
unset( $actions['edit'] );
unset( $actions['reply'] );
unset( $actions['quickedit'] );
}

Expand Down
40 changes: 12 additions & 28 deletions includes/class-scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,13 @@ public static function schedule_post_activity( $new_status, $old_status, $post )
return;
}

\wp_schedule_single_event(
\time(),
'activitypub_send_activity',
array( $post, $type )
);
$hook = 'activitypub_send_post';
$args = array( $post->ID, $type );

\wp_schedule_single_event(
\time(),
sprintf(
'activitypub_send_%s_activity',
\strtolower( $type )
),
array( $post )
);
if ( false === wp_next_scheduled( $hook, $args ) ) {
set_wp_object_state( $post, 'federate' );
\wp_schedule_single_event( \time(), $hook, $args );
}
}

/**
Expand Down Expand Up @@ -204,22 +197,13 @@ public static function schedule_comment_activity( $new_status, $old_status, $com
return;
}

set_wp_object_state( $comment, 'federate' );
$hook = 'activitypub_send_comment';
$args = array( $comment->comment_ID, $type );

\wp_schedule_single_event(
\time(),
'activitypub_send_activity',
array( $comment, $type )
);

\wp_schedule_single_event(
\time(),
sprintf(
'activitypub_send_%s_activity',
\strtolower( $type )
),
array( $comment )
);
if ( false === wp_next_scheduled( $hook, $args ) ) {
set_wp_object_state( $comment, 'federate' );
\wp_schedule_single_event( \time(), $hook, $args );
}
}

/**
Expand Down

0 comments on commit 9cf521c

Please sign in to comment.