wordpress-activitypub/includes/class-activity-dispatcher.php
2023-04-28 09:54:09 +02:00

85 lines
2.4 KiB
PHP

<?php
namespace Activitypub;
use Activitypub\Model\Post;
use Activitypub\Model\Activity;
use Activitypub\Collection\Followers;
/**
* ActivityPub Activity_Dispatcher Class
*
* @author Matthias Pfefferle
*
* @see https://www.w3.org/TR/activitypub/
*/
class Activity_Dispatcher {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
// legacy
\add_action( 'activitypub_send_post_activity', array( self::class, 'send_create_activity' ) );
\add_action( 'activitypub_send_create_activity', array( self::class, 'send_create_activity' ) );
\add_action( 'activitypub_send_update_activity', array( self::class, 'send_update_activity' ) );
\add_action( 'activitypub_send_delete_activity', array( self::class, 'send_delete_activity' ) );
}
/**
* Send "create" activities.
*
* @param Activitypub\Model\Post $activitypub_post
*/
public static function send_create_activity( Post $activitypub_post ) {
self::send_activity( $activitypub_post, 'Create' );
}
/**
* Send "update" activities.
*
* @param Activitypub\Model\Post $activitypub_post
*/
public static function send_update_activity( Post $activitypub_post ) {
self::send_activity( $activitypub_post, 'Update' );
}
/**
* Send "delete" activities.
*
* @param Activitypub\Model\Post $activitypub_post
*/
public static function send_delete_activity( Post $activitypub_post ) {
self::send_activity( $activitypub_post, 'Delete' );
}
/**
* Undocumented function
*
* @param Activitypub\Model\Post $activitypub_post
* @param [type] $activity_type
*
* @return void
*/
public static function send_activity( Post $activitypub_post, $activity_type ) {
// check if a migration is needed before sending new posts
\Activitypub\Migration::maybe_migrate();
// get latest version of post
$user_id = $activitypub_post->get_post_author();
$activitypub_activity = new Activity( $activity_type );
$activitypub_activity->from_post( $activitypub_post );
$follower_inboxes = Followers::get_inboxes( $user_id );
$mentioned_inboxes = Mention::get_inboxes( $activitypub_activity->get_cc() );
$inboxes = array_merge( $follower_inboxes, $mentioned_inboxes );
$inboxes = array_unique( $inboxes );
foreach ( $inboxes as $inbox ) {
$activity = $activitypub_activity->to_json();
\Activitypub\safe_remote_post( $inbox, $activity, $user_id );
}
}
}