From 3feef1e8cf89181d8273a1a53ee6c342cb0fe740 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Thu, 25 May 2023 13:55:18 +0200 Subject: [PATCH] send user and blog activities and set the blog to "single-mode" --- activitypub.php | 1 + includes/class-activity-dispatcher.php | 25 +++++++++++++++++++++++-- includes/functions.php | 9 +++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/activitypub.php b/activitypub.php index 81639b8..7bc4306 100644 --- a/activitypub.php +++ b/activitypub.php @@ -28,6 +28,7 @@ function init() { \defined( 'ACTIVITYPUB_USERNAME_REGEXP' ) || \define( 'ACTIVITYPUB_USERNAME_REGEXP', '(?:([A-Za-z0-9_-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))' ); \defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "[ap_title]\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" ); \defined( 'ACTIVITYPUB_SECURE_MODE' ) || \define( 'ACTIVITYPUB_SECURE_MODE', apply_filters( 'activitypub_secure_mode', $value = false ) ); + \defined( 'ACTIVITYPUB_SINGLE_USER_MODE' ) || \define( 'ACTIVITYPUB_SINGLE_USER_MODE', false ); \define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php index ee435a7..811f923 100644 --- a/includes/class-activity-dispatcher.php +++ b/includes/class-activity-dispatcher.php @@ -3,6 +3,7 @@ namespace Activitypub; use Activitypub\Model\Post; use Activitypub\Model\Activity; +use Activitypub\User_Factory; use Activitypub\Collection\Followers; use function Activitypub\safe_remote_post; @@ -66,12 +67,32 @@ class Activity_Dispatcher { // check if a migration is needed before sending new posts Migration::maybe_migrate(); - // get latest version of post - $user_id = $activitypub_post->get_post_author(); + if ( ! is_single_user_mode() ) { + // send User-Activity + self::send_user_activity( $activitypub_post, $activity_type ); + } + + // send Blog-User-Activity + self::send_user_activity( $activitypub_post, $activity_type, User_Factory::BLOG_USER_ID ); + } + + public static function send_user_activity( Post $activitypub_post, $activity_type, $user_id = null ) { + if ( $user_id ) { + $user = User_Factory::get_by_id( $user_id ); + $actor = $user->get_url(); + } else { + // get latest version of post + $user_id = $activitypub_post->get_post_author(); + $actor = null; + } $activitypub_activity = new Activity( $activity_type ); $activitypub_activity->from_post( $activitypub_post ); + if ( $actor ) { + $activitypub_activity->set_actor( $actor ); + } + $follower_inboxes = Followers::get_inboxes( $user_id ); $mentioned_inboxes = Mention::get_inboxes( $activitypub_activity->get_cc() ); diff --git a/includes/functions.php b/includes/functions.php index 8811d9b..1f25e04 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -267,3 +267,12 @@ function is_activitypub_request() { return false; } + +/** + * Check if the current site is in single-user mode. + * + * @return boolean + */ +function is_single_user_mode() { + return ACTIVITYPUB_SINGLE_USER_MODE; +}