Skip to content

Commit

Permalink
Outbox: Show Outbox processing in Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
obenland committed Jan 28, 2025
1 parent 395a90f commit b5ca30a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 5 deletions.
9 changes: 5 additions & 4 deletions includes/class-dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ private static function send_activity_to_followers( $activity, $actor_id, $outbo
/**
* Filters the list of inboxes to send the Activity to.
*
* @param array $inboxes The list of inboxes to send to.
* @param int $actor_id The actor ID.
* @param Activity $activity The ActivityPub Activity.
* @param array $inboxes The list of inboxes to send to.
* @param int $actor_id The actor ID.
* @param Activity $activity The ActivityPub Activity.
* @param \WP_Post $outbox_item The WordPress object.
*/
$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $actor_id, $activity );
$inboxes = apply_filters( 'activitypub_send_to_inboxes', array(), $actor_id, $activity, $outbox_item );
$inboxes = array_unique( $inboxes );

$json = $activity->to_json();
Expand Down
93 changes: 92 additions & 1 deletion integration/class-stream-connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Activitypub\Integration;

use function Activitypub\url_to_commentid;

/**
* Stream Connector for ActivityPub.
*
Expand All @@ -29,6 +31,7 @@ class Stream_Connector extends \WP_Stream\Connector {
*/
public $actions = array(
'activitypub_notification_follow',
'activitypub_send_to_inboxes',
);

/**
Expand All @@ -55,7 +58,9 @@ public function get_context_labels() {
* @return array
*/
public function get_action_labels() {
return array();
return array(
'processed' => __( 'Processed', 'activitypub' ),
);
}

/**
Expand All @@ -79,4 +84,90 @@ public function callback_activitypub_notification_follow( $notification ) {
$notification->target
);
}

/**
* Add action links to Stream drop row in admin list screen
*
* @filter wp_stream_action_links_{connector}
*
* @param array $links Previous links registered.
* @param Record $record Stream record.
*
* @return array Action links
*/
public function action_links( $links, $record ) {
if ( 'processed' === $record->action ) {
$inboxes = $record->get_meta( 'inboxes', true );
if ( empty( $inboxes ) ) {
$inboxes = __( 'No inboxes to notify about this activity.', 'activitypub' );
} else {
$inboxes = implode( "\n", $inboxes );
}

$message = sprintf(
'<details><summary>%1$s</summary><pre>%2$s</pre></details>',
__( 'Notified Inboxes', 'activitypub' ),
$inboxes
);

$links[ $message ] = '';
}

return $links;
}

/**
* Callback for activitypub_send_to_inboxes.
*
* @param array $inboxes The list of inboxes to send to.
* @param int $actor_id The actor ID.
* @param \Activitypub\Activity\Activity $activity The ActivityPub Activity.
* @param \WP_Post $outbox_item The WordPress object.
*/
public function callback_activitypub_send_to_inboxes( $inboxes, $actor_id, $activity, $outbox_item ) {
static $initial_run = true;

// Jump back in priority to catch modified inboxes.
if ( $initial_run ) {
add_action( 'activitypub_send_to_inboxes', array( $this, __FUNCTION__ ), 99, 4 );
$initial_run = false;
return $inboxes;
}

$object_id = $outbox_item->ID;
$object_type = $outbox_item->post_type;
$object_title = $outbox_item->post_title;

$post_id = url_to_postid( $outbox_item->post_title );
if ( $post_id ) {
$post = get_post( $post_id );

$object_id = $post_id;
$object_type = $post->post_type;
$object_title = $post->post_title;
}

$comment_id = url_to_commentid( $outbox_item->post_title );
if ( $comment_id ) {
$comment = get_comment( $comment_id );

$object_id = $comment_id;
$object_type = $comment->comment_type;
$object_title = $comment->comment_content;
}

$this->log(
// translators: 1: post title.
sprintf( __( 'Outbox processed for "%1$s"', 'activitypub' ), $object_title ),
array(
'inboxes' => $inboxes,
),
$object_id,
$object_type,
'processed'
);

// We're in a filter, so we need to return the inboxes.
return $inboxes;
}
}

0 comments on commit b5ca30a

Please sign in to comment.