diff --git a/activitypub.php b/activitypub.php index 9819617..2b9d28b 100644 --- a/activitypub.php +++ b/activitypub.php @@ -26,11 +26,14 @@ function init() { require_once dirname( __FILE__ ) . '/includes/db/class-followers.php'; require_once dirname( __FILE__ ) . '/includes/functions.php'; + require_once dirname( __FILE__ ) . '/includes/class-activity-dispatcher.php'; + \Activitypub\Activity_Dispatcher::init(); + require_once dirname( __FILE__ ) . '/includes/class-post.php'; \Activitypub\Post::init(); require_once dirname( __FILE__ ) . '/includes/class-activitypub.php'; - \Activitypub\ActivityPub::init(); + \Activitypub\Activitypub::init(); // Configure the REST API route require_once dirname( __FILE__ ) . '/includes/rest/class-outbox.php'; diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php new file mode 100644 index 0000000..560b403 --- /dev/null +++ b/includes/class-activity-dispatcher.php @@ -0,0 +1,83 @@ +post_author; + + $activitypub_post = new \Activitypub\Post( $post ); + $activitypub_activity = new \Activitypub\Activity( 'Create', \Activitypub\Activity::TYPE_FULL ); + $activitypub_activity->from_post( $activitypub_post->to_array() ); + + 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 ); + } + } + + /** + * Send "update" activities + * + * @param int $post_id + */ + public static function send_update_activity( $post_id ) { + $post = get_post( $post_id ); + $user_id = $post->post_author; + + $activitypub_post = new \Activitypub\Post( $post ); + $activitypub_activity = new \Activitypub\Activity( 'Update', \Activitypub\Activity::TYPE_FULL ); + $activitypub_activity->from_post( $activitypub_post->to_array() ); + + 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 ); + } + } + + /** + * Send "delete" activities + * + * @param int $post_id + */ + public static function send_delete_activity( $post_id ) { + $post = get_post( $post_id ); + $user_id = $post->post_author; + + $activitypub_post = new \Activitypub\Post( $post ); + $activitypub_activity = new \Activitypub\Activity( 'Delete', \Activitypub\Activity::TYPE_FULL ); + $activitypub_activity->from_post( $activitypub_post->to_array() ); + + 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 ); + } + } +} diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index fdf6845..6b10c01 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -14,8 +14,6 @@ class Outbox { */ public static function init() { add_action( 'rest_api_init', array( '\Activitypub\Rest\Outbox', 'register_routes' ) ); - add_action( 'activitypub_send_post_activity', array( '\Activitypub\Rest\Outbox', 'send_post_activity' ) ); - add_action( 'activitypub_send_update_activity', array( '\Activitypub\Rest\Outbox', 'send_update_activity' ) ); } /** @@ -125,46 +123,4 @@ class Outbox { return $params; } - - /** - * Send "create" activities - * - * @param int $post_id - */ - public static function send_post_activity( $post_id ) { - $post = get_post( $post_id ); - $user_id = $post->post_author; - - $activitypub_post = new \Activitypub\Post( $post ); - $activitypub_activity = new \Activitypub\Activity( 'Create', \Activitypub\Activity::TYPE_FULL ); - $activitypub_activity->from_post( $activitypub_post->to_array() ); - - 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 ); - } - } - - /** - * Send "update" activities - * - * @param int $post_id - */ - public static function send_update_activity( $post_id ) { - $post = get_post( $post_id ); - $user_id = $post->post_author; - - $activitypub_post = new \Activitypub\Post( $post ); - $activitypub_activity = new \Activitypub\Activity( 'Update', \Activitypub\Activity::TYPE_FULL ); - $activitypub_activity->from_post( $activitypub_post->to_array() ); - - 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 ); - } - } }