Skip to content

Commit

Permalink
Merge branch 'trunk' into change/improve-conneg-extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
pfefferle authored Jan 15, 2025
2 parents 87c2355 + 79e8b9b commit 636b2fb
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 68 deletions.
2 changes: 1 addition & 1 deletion activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ function rest_init() {
Rest\Comment::init();
Rest\Server::init();
Rest\Collection::init();
Rest\Interaction::init();
Rest\Post::init();
( new Rest\Interaction_Controller() )->register_routes();
( new Rest\Application_Controller() )->register_routes();
( new Rest\Webfinger_Controller() )->register_routes();

Expand Down
4 changes: 2 additions & 2 deletions includes/collection/class-followers.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static function remove_follower( $user_id, $actor ) {
* @param int $user_id The ID of the WordPress User.
* @param string $actor The Actor URL.
*
* @return Follower|null The Follower object or null
* @return \Activitypub\Activity\Base_Object|WP_Error|null The Follower object or null
*/
public static function get_follower( $user_id, $actor ) {
global $wpdb;
Expand Down Expand Up @@ -266,7 +266,7 @@ public static function count_followers( $user_id ) {
}

/**
* Returns all Inboxes for a Users Followers.
* Returns all Inboxes for an Actor's Followers.
*
* @param int $user_id The ID of the WordPress User.
*
Expand Down
8 changes: 4 additions & 4 deletions includes/handler/class-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public static function init() {
}

/**
* Handle "Update" requests
* Handle "Update" requests.
*
* @param array $activity The activity-object.
* @param array $activity The Activity object.
*/
public static function handle_update( $activity ) {
$object_type = isset( $activity['object']['type'] ) ? $activity['object']['type'] : '';
Expand Down Expand Up @@ -75,7 +75,7 @@ public static function handle_update( $activity ) {
/**
* Update an Interaction.
*
* @param array $activity The activity-object.
* @param array $activity The Activity object.
*/
public static function update_interaction( $activity ) {
$commentdata = Interactions::update_comment( $activity );
Expand All @@ -102,7 +102,7 @@ public static function update_interaction( $activity ) {
/**
* Update an Actor.
*
* @param array $activity The activity-object.
* @param array $activity The Activity object.
*/
public static function update_actor( $activity ) {
// Update cache.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
<?php
/**
* ActivityPub Interaction REST-Class file.
* ActivityPub Interaction Controller file.
*
* @package Activitypub
*/

namespace Activitypub\Rest;

use WP_REST_Response;
use Activitypub\Http;

/**
* Interaction class.
* Interaction Controller.
*/
class Interaction {
class Interaction_Controller extends \WP_REST_Controller {
/**
* Initialize the class, registering WordPress hooks.
* The namespace of this controller's route.
*
* @var string
*/
public static function init() {
self::register_routes();
}
protected $namespace = ACTIVITYPUB_REST_NAMESPACE;

/**
* Register routes
* The base of this controller's route.
*
* @var string
*/
public static function register_routes() {
protected $rest_base = 'interactions';

/**
* Register routes.
*/
public function register_routes() {
\register_rest_route(
ACTIVITYPUB_REST_NAMESPACE,
'/interactions',
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( self::class, 'get' ),
'callback' => array( $this, 'get_item' ),
'permission_callback' => '__return_true',
'args' => array(
'uri' => array(
'type' => 'string',
'required' => true,
'sanitize_callback' => 'esc_url',
'description' => 'The URI of the object to interact with.',
'type' => 'string',
'format' => 'uri',
'required' => true,
),
),
),
Expand All @@ -46,27 +53,26 @@ public static function register_routes() {
}

/**
* Handle GET request.
* Retrieves the interaction URL for a given URI.
*
* @param \WP_REST_Request $request The request object.
*
* @return WP_REST_Response Redirect to the editor or die.
* @return \WP_REST_Response Response object on success, dies on failure.
*/
public static function get( $request ) {
public function get_item( $request ) {
$uri = $request->get_param( 'uri' );
$redirect_url = null;
$object = Http::get_remote_object( $uri );

if (
\is_wp_error( $object ) ||
! isset( $object['type'] )
) {
if ( \is_wp_error( $object ) || ! isset( $object['type'] ) ) {
// Use wp_die as this can be called from the front-end. See https://github.com/Automattic/wordpress-activitypub/pull/1149/files#r1915297109.
\wp_die(
\esc_html__(
'The URL is not supported!',
'activitypub'
),
400
esc_html__( 'The URL is not supported!', 'activitypub' ),
'',
array(
'response' => 400,
'back_link' => true,
)
);
}

Expand Down Expand Up @@ -104,31 +110,30 @@ public static function get( $request ) {
}

/**
* Filter the redirect URL.
* Filters the redirect URL.
*
* This filter runs after the type-specific filters and allows for final modifications
* to the interaction URL regardless of the object type.
*
* @param string $redirect_url The URL to redirect to.
* @param string $uri The URI of the object.
* @param array $object The object.
* @param array $object The object being interacted with.
*/
$redirect_url = \apply_filters( 'activitypub_interactions_url', $redirect_url, $uri, $object );

// Check if hook is implemented.
if ( ! $redirect_url ) {
// Use wp_die as this can be called from the front-end. See https://github.com/Automattic/wordpress-activitypub/pull/1149/files#r1915297109.
\wp_die(
esc_html__(
'This Interaction type is not supported yet!',
'activitypub'
),
400
esc_html__( 'This Interaction type is not supported yet!', 'activitypub' ),
'',
array(
'response' => 400,
'back_link' => true,
)
);
}

return new WP_REST_Response(
null,
302,
array(
'Location' => \esc_url( $redirect_url ),
)
);
return new \WP_REST_Response( null, 302, array( 'Location' => \esc_url( $redirect_url ) ) );
}
}
14 changes: 7 additions & 7 deletions tests/includes/class-test-activity-dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
class Test_Activity_Dispatcher extends ActivityPub_TestCase_Cache_HTTP {

/**
* Users.
* Actors.
*
* @var array[] $users
* @var array[]
*/
public static $users = array(
public static $actors = array(
'[email protected]' => array(
'id' => 'https://example.org/users/username',
'url' => 'https://example.org/users/username',
Expand Down Expand Up @@ -109,7 +109,7 @@ public function test_dispatch_mentions() {
)
);

self::$users['https://example.com/alex'] = array(
self::$actors['https://example.com/alex'] = array(
'id' => 'https://example.com/alex',
'url' => 'https://example.com/alex',
'inbox' => 'https://example.com/alex/inbox',
Expand Down Expand Up @@ -300,10 +300,10 @@ function ( $disabled, $user_id ) {
* @return array|bool
*/
public static function pre_get_remote_metadata_by_actor( $pre, $actor ) {
if ( isset( self::$users[ $actor ] ) ) {
return self::$users[ $actor ];
if ( isset( self::$actors[ $actor ] ) ) {
return self::$actors[ $actor ];
}
foreach ( self::$users as $data ) {
foreach ( self::$actors as $data ) {
if ( $data['url'] === $actor ) {
return $data;
}
Expand Down
10 changes: 5 additions & 5 deletions tests/includes/class-test-mention.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
class Test_Mention extends \WP_UnitTestCase {

/**
* Users.
* Actors.
*
* @var array
* @var array[]
*/
public static $users = array(
public static $actors = array(
'[email protected]' => array(
'id' => 'https://example.org/users/username',
'url' => 'https://example.org/users/username',
Expand Down Expand Up @@ -133,8 +133,8 @@ public function pre_http_request( $response, $parsed_args, $url ) {
public static function pre_get_remote_metadata_by_actor( $pre, $actor ) {
$actor = ltrim( $actor, '@' );

if ( isset( self::$users[ $actor ] ) ) {
return self::$users[ $actor ];
if ( isset( self::$actors[ $actor ] ) ) {
return self::$actors[ $actor ];
}

return $pre;
Expand Down
14 changes: 7 additions & 7 deletions tests/includes/collection/class-test-followers.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
class Test_Followers extends \WP_UnitTestCase {

/**
* Users.
* Actors.
*
* @var array[]
*/
public static $users = array(
public static $actors = array(
'[email protected]' => array(
'id' => 'https://example.org/users/username',
'url' => 'https://example.org/users/username',
Expand Down Expand Up @@ -399,8 +399,8 @@ public function test_migration_followers( $followers, $expected_count ) {
add_filter(
'pre_get_remote_metadata_by_actor',
function ( $pre, $actor ) {
if ( isset( self::$users[ $actor ] ) ) {
return self::$users[ $actor ];
if ( isset( self::$actors[ $actor ] ) ) {
return self::$actors[ $actor ];
}
return $pre;
},
Expand Down Expand Up @@ -549,10 +549,10 @@ public function test_get_all_followers() {
* @return array
*/
public static function pre_get_remote_metadata_by_actor( $pre, $actor ) {
if ( isset( self::$users[ $actor ] ) ) {
return self::$users[ $actor ];
if ( isset( self::$actors[ $actor ] ) ) {
return self::$actors[ $actor ];
}
foreach ( self::$users as $data ) {
foreach ( self::$actors as $data ) {
if ( $data['url'] === $actor ) {
return $data;
}
Expand Down
Loading

0 comments on commit 636b2fb

Please sign in to comment.