From a9c1862369acde2d17fd675dca4484f5a0efce52 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 6 Oct 2023 15:45:58 +0200 Subject: [PATCH] some simplifications --- includes/class-activity-dispatcher.php | 65 ++++++++------------------ includes/class-activitypub.php | 7 ++- includes/class-comments.php | 25 ---------- includes/class-scheduler.php | 25 ++++++++++ includes/functions.php | 5 +- includes/transformer/class-factory.php | 21 +++++++++ templates/comment-json.php | 4 +- 7 files changed, 75 insertions(+), 77 deletions(-) create mode 100644 includes/transformer/class-factory.php diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php index 06b7bb1..c3946d6 100644 --- a/includes/class-activity-dispatcher.php +++ b/includes/class-activity-dispatcher.php @@ -33,12 +33,12 @@ class Activity_Dispatcher { /** * Send Activities to followers and mentioned users or `Announce` (boost) a blog post. * - * @param WP_Post $wp_post The ActivityPub Post. - * @param string $type The Activity-Type. + * @param mixed $wp_object The ActivityPub Post. + * @param string $type The Activity-Type. * * @return void */ - public static function send_activity_or_announce( WP_Post $wp_post, $type ) { + public static function send_activity_or_announce( $wp_object, $type ) { // check if a migration is needed before sending new posts Migration::maybe_migrate(); @@ -46,35 +46,35 @@ class Activity_Dispatcher { return; } - $wp_post->post_author = Users::BLOG_USER_ID; + $wp_object->post_author = Users::BLOG_USER_ID; if ( is_single_user() ) { - self::send_activity( $wp_post, $type ); + self::send_activity( $wp_object, $type ); } else { - self::send_announce( $wp_post, $type ); + self::send_announce( $wp_object, $type ); } } /** * Send Activities to followers and mentioned users. * - * @param WP_Post $wp_post The ActivityPub Post. - * @param string $type The Activity-Type. + * @param mixed $wp_object The ActivityPub Post. + * @param string $type The Activity-Type. * * @return void */ - public static function send_activity( WP_Post $wp_post, $type ) { - if ( is_user_disabled( $wp_post->post_author ) ) { + public static function send_activity( $wp_object, $type ) { + if ( is_user_disabled( $wp_object->post_author ) ) { return; } - $object = Post::transform( $wp_post )->to_object(); + $object = Factory::get_transformer( $wp_object )->to_object(); $activity = new Activity(); $activity->set_type( $type ); $activity->set_object( $object ); - $follower_inboxes = Followers::get_inboxes( $wp_post->post_author ); + $follower_inboxes = Followers::get_inboxes( $wp_object->post_author ); $mentioned_inboxes = Mention::get_inboxes( $activity->get_cc() ); $inboxes = array_merge( $follower_inboxes, $mentioned_inboxes ); @@ -83,19 +83,19 @@ class Activity_Dispatcher { $json = $activity->to_json(); foreach ( $inboxes as $inbox ) { - safe_remote_post( $inbox, $json, $wp_post->post_author ); + safe_remote_post( $inbox, $json, $wp_object->post_author ); } } /** * Send Announces to followers and mentioned users. * - * @param WP_Post $wp_post The ActivityPub Post. - * @param string $type The Activity-Type. + * @param mixed $wp_object The ActivityPub Post. + * @param string $type The Activity-Type. * * @return void */ - public static function send_announce( WP_Post $wp_post, $type ) { + public static function send_announce( $wp_object, $type ) { if ( ! in_array( $type, array( 'Create', 'Update' ), true ) ) { return; } @@ -104,7 +104,7 @@ class Activity_Dispatcher { return; } - $object = Post::transform( $wp_post )->to_object(); + $object = Factory::get_transformer( $wp_object )->to_object(); $activity = new Activity(); $activity->set_type( 'Announce' ); @@ -113,7 +113,7 @@ class Activity_Dispatcher { // send only the id $activity->set_object( $object->get_id() ); - $follower_inboxes = Followers::get_inboxes( $wp_post->post_author ); + $follower_inboxes = Followers::get_inboxes( $wp_object->post_author ); $mentioned_inboxes = Mention::get_inboxes( $activity->get_cc() ); $inboxes = array_merge( $follower_inboxes, $mentioned_inboxes ); @@ -122,34 +122,7 @@ class Activity_Dispatcher { $json = $activity->to_json(); foreach ( $inboxes as $inbox ) { - safe_remote_post( $inbox, $json, $wp_post->post_author ); - } - } - - /** - * Send Activities to followers and mentioned users. - * - * @param $wp_comment The ActivityPub Comment. - * @param string $type The Activity-Type. - */ - public static function send_comment_activity( $wp_comment, $type ) { - - $object = Comment::transform( $wp_comment )->to_object(); - - $activity = new Activity(); - $activity->set_type( $type ); - $activity->set_object( $object ); - - $follower_inboxes = Followers::get_inboxes( $wp_comment->user_id ); - $mentioned_inboxes = Mention::get_inboxes( $activity->get_cc() ); - - $inboxes = array_merge( $follower_inboxes, $mentioned_inboxes ); - $inboxes = array_unique( $inboxes ); - - $json = $activity->to_json(); - - foreach ( $inboxes as $inbox ) { - safe_remote_post( $inbox, $json, $wp_comment->user_id ); + safe_remote_post( $inbox, $json, $wp_object->post_author ); } } } diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 41124b1..a7cca7c 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -4,6 +4,9 @@ namespace Activitypub; use Activitypub\Signature; use Activitypub\Collection\Users; +use function Activitypub\is_comment; +use function Activitypub\is_replies; + /** * ActivityPub Class * @@ -92,9 +95,9 @@ class Activitypub { if ( \is_author() ) { $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/author-json.php'; - } elseif ( \Activitypub\is_ap_replies() ) { + } elseif ( is_replies() ) { $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/replies-json.php'; - } elseif ( \Activitypub\is_ap_comment() ) { + } elseif ( is_comment() ) { $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/comment-json.php'; } elseif ( \is_singular() ) { $json_template = ACTIVITYPUB_PLUGIN_DIR . '/templates/post-json.php'; diff --git a/includes/class-comments.php b/includes/class-comments.php index 309616c..5af6f77 100644 --- a/includes/class-comments.php +++ b/includes/class-comments.php @@ -18,7 +18,6 @@ class Comments { \add_filter( 'comment_post', array( self::class, 'postprocess_comment' ), 10, 3 ); \add_filter( 'comment_reply_link', array( self::class, 'comment_reply_link' ), 10, 4 ); \add_action( 'edit_comment', array( self::class, 'edit_comment' ), 20, 2 ); //schedule_admin_comment_activity - \add_action( 'transition_comment_status', array( self::class, 'schedule_comment_activity' ), 20, 3 ); } /** @@ -133,28 +132,4 @@ class Comments { \wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $wp_comment, 'Update' ) ); } } - - /** - * Schedule Comment Activities - * - * transition_comment_status() - * @param int $comment - */ - public static function schedule_comment_activity( $new_status, $old_status, $activitypub_comment ) { - if ( 'approved' === $new_status && 'approved' !== $old_status ) { - //should only federate replies from local actors - //should only federate replies to federated actors - - $ap_object = unserialize( \get_comment_meta( $activitypub_comment->comment_ID, 'ap_object', true ) ); - if ( empty( $ap_object ) ) { - \wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $activitypub_comment, 'Create' ) ); - } - } elseif ( 'trash' === $new_status ) { - \wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $activitypub_comment, 'Delete' ) ); - } elseif ( $old_status === $new_status ) { - //TODO Test with non-admin user - \wp_schedule_single_event( \time(), 'activitypub_send_comment_activity', array( $activitypub_comment, 'Update' ) ); - } - } - } diff --git a/includes/class-scheduler.php b/includes/class-scheduler.php index 63f9273..257efc8 100644 --- a/includes/class-scheduler.php +++ b/includes/class-scheduler.php @@ -17,6 +17,7 @@ class Scheduler { */ public static function init() { \add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 ); + \add_action( 'transition_comment_status', array( self::class, 'schedule_comment_activity' ), 33, 3 ); \add_action( 'activitypub_update_followers', array( self::class, 'update_followers' ) ); \add_action( 'activitypub_cleanup_followers', array( self::class, 'cleanup_followers' ) ); @@ -99,6 +100,30 @@ class Scheduler { ); } + /** + * Schedule Comment Activities + * + * transition_comment_status() + * + * @param int $comment + */ + public static function schedule_comment_activity( $new_status, $old_status, $comment ) { + $comment = get_comment( $comment ); + + if ( ! $comment->user_id ) { + // Registered comment author + return; + } + + if ( 'approved' === $new_status && 'approved' !== $old_status ) { + \wp_schedule_single_event( \time(), 'activitypub_send_activity', array( $comment, 'Create' ) ); + } elseif ( 'trash' === $new_status ) { + \wp_schedule_single_event( \time(), 'activitypub_send_activity', array( $comment, 'Delete' ) ); + } elseif ( $old_status === $new_status ) { + \wp_schedule_single_event( \time(), 'activitypub_send_activity', array( $comment, 'Update' ) ); + } + } + /** * Update followers * diff --git a/includes/functions.php b/includes/functions.php index 3f37328..2037cf8 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -358,9 +358,10 @@ function get_source_id( $comment ) { /** * Verify if url is a wp_ap_comment, * Or if it is a previously received remote comment + * * @return int comment_id */ -function is_ap_comment() { +function is_comment() { $comment_id = get_query_var( 'replytocom', null ); if ( ! is_null( $comment_id ) ) { $comment = \get_comment( $comment_id ); @@ -376,7 +377,7 @@ function is_ap_comment() { * Verify if url has a replies query, * @return bool */ -function is_ap_replies() { +function is_replies() { $replies = get_query_var( 'replies' ); if ( $replies ) { return $replies; diff --git a/includes/transformer/class-factory.php b/includes/transformer/class-factory.php new file mode 100644 index 0000000..c43403e --- /dev/null +++ b/includes/transformer/class-factory.php @@ -0,0 +1,21 @@ + \Activitypub\get_context() ), $activitypub_comment->to_array() ); +$object = new \Activitypub\Transformer\Comment( $comment ); +$json = \array_merge( array( '@context' => \Activitypub\get_context() ), $object->to_object()->to_array() ); // filter output $json = \apply_filters( 'activitypub_json_comment_array', $json );