optimize publishing

This commit is contained in:
Matthias Pfefferle 2023-02-02 01:42:15 +01:00
parent d4b1edcf39
commit e015da7f8f
5 changed files with 48 additions and 56 deletions

View file

@ -13,7 +13,10 @@ class Activity_Dispatcher {
* Initialize the class, registering WordPress hooks. * Initialize the class, registering WordPress hooks.
*/ */
public static function init() { public static function init() {
\add_action( 'activitypub_send_post_activity', array( '\Activitypub\Activity_Dispatcher', 'send_post_activity' ) ); // legacy
\add_action( 'activitypub_send_post_activity', array( '\Activitypub\Activity_Dispatcher', 'send_create_activity' ) );
\add_action( 'activitypub_send_create_activity', array( '\Activitypub\Activity_Dispatcher', 'send_create_activity' ) );
\add_action( 'activitypub_send_update_activity', array( '\Activitypub\Activity_Dispatcher', 'send_update_activity' ) ); \add_action( 'activitypub_send_update_activity', array( '\Activitypub\Activity_Dispatcher', 'send_update_activity' ) );
\add_action( 'activitypub_send_delete_activity', array( '\Activitypub\Activity_Dispatcher', 'send_delete_activity' ) ); \add_action( 'activitypub_send_delete_activity', array( '\Activitypub\Activity_Dispatcher', 'send_delete_activity' ) );
} }
@ -23,38 +26,8 @@ class Activity_Dispatcher {
* *
* @param \Activitypub\Model\Post $activitypub_post * @param \Activitypub\Model\Post $activitypub_post
*/ */
public static function send_post_activity( Model\Post $activitypub_post ) { public static function send_create_activity( Model\Post $activitypub_post ) {
// get latest version of post self::send_activity( $activitypub_post, 'Create' );
$user_id = $activitypub_post->get_post_author();
$activitypub_activity = new \Activitypub\Model\Activity( 'Create', \Activitypub\Model\Activity::TYPE_FULL );
$activitypub_activity->from_post( $activitypub_post );
$inboxes = \Activitypub\get_follower_inboxes( $user_id );
$followers_url = \get_rest_url( null, '/activitypub/1.0/users/' . intval( $user_id ) . '/followers' );
foreach ( $activitypub_activity->get_cc() as $cc ) {
if ( $cc === $followers_url ) {
continue;
}
$inbox = \Activitypub\get_inbox_by_actor( $cc );
if ( ! $inbox || \is_wp_error( $inbox ) ) {
continue;
}
// init array if empty
if ( ! isset( $inboxes[ $inbox ] ) ) {
$inboxes[ $inbox ] = array();
}
$inboxes[ $inbox ][] = $cc;
}
foreach ( $inboxes as $inbox => $to ) {
$to = array_values( array_unique( $to ) );
$activitypub_activity->set_to( $to );
$activity = $activitypub_activity->to_json();
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
}
} }
/** /**
@ -62,19 +35,8 @@ class Activity_Dispatcher {
* *
* @param \Activitypub\Model\Post $activitypub_post * @param \Activitypub\Model\Post $activitypub_post
*/ */
public static function send_update_activity( $activitypub_post ) { public static function send_update_activity( Model\Post $activitypub_post ) {
// get latest version of post self::send_activity( $activitypub_post, 'Update' );
$user_id = $activitypub_post->get_post_author();
$activitypub_activity = new \Activitypub\Model\Activity( 'Update', \Activitypub\Model\Activity::TYPE_FULL );
$activitypub_activity->from_post( $activitypub_post );
foreach ( \Activitypub\get_follower_inboxes( $user_id ) as $inbox => $to ) {
$activitypub_activity->set_to( $to );
$activity = $activitypub_activity->to_json(); // phpcs:ignore
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
}
} }
/** /**
@ -82,16 +44,30 @@ class Activity_Dispatcher {
* *
* @param \Activitypub\Model\Post $activitypub_post * @param \Activitypub\Model\Post $activitypub_post
*/ */
public static function send_delete_activity( $activitypub_post ) { public static function send_delete_activity( Model\Post $activitypub_post ) {
self::send_activity( $activitypub_post, 'Delete' );
}
/**
* Undocumented function
*
* @param [type] $activitypub_post
* @param [type] $activity_type
* @return void
*/
public function send_activity( Model\Post $activitypub_post, $activity_type ) {
// get latest version of post // get latest version of post
$user_id = $activitypub_post->get_post_author(); $user_id = $activitypub_post->get_post_author();
$activitypub_activity = new \Activitypub\Model\Activity( 'Delete', \Activitypub\Model\Activity::TYPE_FULL ); $activitypub_activity = new \Activitypub\Model\Activity( $activity_type, \Activitypub\Model\Activity::TYPE_FULL );
$activitypub_activity->from_post( $activitypub_post ); $activitypub_activity->from_post( $activitypub_post );
foreach ( \Activitypub\get_follower_inboxes( $user_id ) as $inbox => $to ) { $inboxes = \Activitypub\get_follower_inboxes( $user_id, $activitypub_activity->get_cc() );
$activitypub_activity->set_to( $to );
$activity = $activitypub_activity->to_json(); // phpcs:ignore foreach ( $inboxes as $inbox => $cc ) {
$cc = array_values( array_unique( $cc ) );
$activitypub_activity->add_cc( $cc );
$activity = $activitypub_activity->to_json();
\Activitypub\safe_remote_post( $inbox, $activity, $user_id ); \Activitypub\safe_remote_post( $inbox, $activity, $user_id );
} }

View file

@ -117,7 +117,7 @@ class Activitypub {
$activitypub_post = new \Activitypub\Model\Post( $post ); $activitypub_post = new \Activitypub\Model\Post( $post );
if ( 'publish' === $new_status && 'publish' !== $old_status ) { if ( 'publish' === $new_status && 'publish' !== $old_status ) {
\wp_schedule_single_event( \time(), 'activitypub_send_post_activity', array( $activitypub_post ) ); \wp_schedule_single_event( \time(), 'activitypub_send_create_activity', array( $activitypub_post ) );
} elseif ( 'publish' === $new_status ) { } elseif ( 'publish' === $new_status ) {
\wp_schedule_single_event( \time(), 'activitypub_send_update_activity', array( $activitypub_post ) ); \wp_schedule_single_event( \time(), 'activitypub_send_update_activity', array( $activitypub_post ) );
} elseif ( 'trash' === $new_status ) { } elseif ( 'trash' === $new_status ) {

View file

@ -219,8 +219,11 @@ function get_publickey_by_actor( $actor, $key_id ) {
return new \WP_Error( 'activitypub_no_public_key', \__( 'No "Public-Key" found', 'activitypub' ), $metadata ); return new \WP_Error( 'activitypub_no_public_key', \__( 'No "Public-Key" found', 'activitypub' ), $metadata );
} }
function get_follower_inboxes( $user_id ) { function get_follower_inboxes( $user_id, $cc = array() ) {
$followers = \Activitypub\Peer\Followers::get_followers( $user_id ); $followers = \Activitypub\Peer\Followers::get_followers( $user_id );
$followers = array_merge( $followers, $cc );
$followers = array_unique( $followers );
$inboxes = array(); $inboxes = array();
foreach ( $followers as $follower ) { foreach ( $followers as $follower ) {

View file

@ -43,6 +43,18 @@ class Activity {
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) { if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
$this->$var = $params[0]; $this->$var = $params[0];
} }
if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
if ( ! is_array( $this->$var ) ) {
$this->$var = $params[0];
}
if ( is_array( $params[0] ) ) {
$this->$var = array_merge( $this->$var, $params[0] );
} else {
array_push( $this->$var, $params[0] );
}
}
} }
public function from_post( Post $post ) { public function from_post( Post $post ) {
@ -51,7 +63,8 @@ class Activity {
if ( isset( $object['published'] ) ) { if ( isset( $object['published'] ) ) {
$this->published = $object['published']; $this->published = $object['published'];
} }
$this->cc = array( \get_rest_url( null, '/activitypub/1.0/users/' . intval( $post->get_post_author() ) . '/followers' ) );
$this->add_to( \get_rest_url( null, '/activitypub/1.0/users/' . intval( $post->get_post_author() ) . '/followers' ) );
if ( isset( $this->object['attributedTo'] ) ) { if ( isset( $this->object['attributedTo'] ) ) {
$this->actor = $this->object['attributedTo']; $this->actor = $this->object['attributedTo'];
@ -59,7 +72,7 @@ class Activity {
foreach ( $post->get_tags() as $tag ) { foreach ( $post->get_tags() as $tag ) {
if ( 'Mention' === $tag['type'] ) { if ( 'Mention' === $tag['type'] ) {
$this->cc[] = $tag['href']; $this->add_cc( $tag['href'] );
} }
} }

View file

@ -386,7 +386,7 @@ class Post {
$filtered_content = \apply_filters( 'activitypub_the_content', $content, $post ); $filtered_content = \apply_filters( 'activitypub_the_content', $content, $post );
$decoded_content = \html_entity_decode( $filtered_content, \ENT_QUOTES, 'UTF-8' ); $decoded_content = \html_entity_decode( $filtered_content, \ENT_QUOTES, 'UTF-8' );
$content = \trim( \preg_replace( '/[\n\r]/', '', $content ) ); $content = \trim( \preg_replace( '/[\n\r\t]/', '', $content ) );
$this->content = $content; $this->content = $content;