Skip to content

Commit

Permalink
Outbox Collection (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
pfefferle authored Jan 31, 2025
1 parent a6fd182 commit 708c592
Show file tree
Hide file tree
Showing 64 changed files with 5,979 additions and 2,319 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Untitled]
## [Unreleased]

### Changed

Expand Down Expand Up @@ -33,6 +33,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Support for WPML post locale

### Added

* Outbox queue

### Changed

* Rewrite the current dispatcher system, to use the Outbox instead of the Scheduler.

### Removed

* Built-in support for nodeinfo2. Use the [NodeInfo plugin](https://wordpress.org/plugins/nodeinfo/) instead.
Expand Down
6 changes: 3 additions & 3 deletions activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@
*/
function rest_init() {
Rest\Actors::init();
Rest\Outbox::init();
Rest\Inbox::init();
Rest\Followers::init();
Rest\Following::init();
Rest\Comment::init();
Rest\Server::init();
Rest\Collection::init();
Rest\Post::init();
( new Rest\Interaction_Controller() )->register_routes();
( new Rest\Application_Controller() )->register_routes();
( new Rest\Interaction_Controller() )->register_routes();
( new Rest\Outbox_Controller() )->register_routes();
( new Rest\Webfinger_Controller() )->register_routes();

// Load NodeInfo endpoints only if blog is public.
Expand All @@ -65,7 +65,7 @@ function rest_init() {
function plugin_init() {
\add_action( 'init', array( __NAMESPACE__ . '\Migration', 'init' ) );
\add_action( 'init', array( __NAMESPACE__ . '\Activitypub', 'init' ) );
\add_action( 'init', array( __NAMESPACE__ . '\Activity_Dispatcher', 'init' ) );
\add_action( 'init', array( __NAMESPACE__ . '\Dispatcher', 'init' ) );
\add_action( 'init', array( __NAMESPACE__ . '\Handler', 'init' ) );
\add_action( 'init', array( __NAMESPACE__ . '\Admin', 'init' ) );
\add_action( 'init', array( __NAMESPACE__ . '\Hashtag', 'init' ) );
Expand Down
12 changes: 11 additions & 1 deletion includes/activity/class-activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

use Activitypub\Link;

use function Activitypub\is_actor;
use function Activitypub\is_activity;

/**
* \Activitypub\Activity\Activity implements the common
* attributes of an Activity.
Expand Down Expand Up @@ -154,7 +157,14 @@ class Activity extends Base_Object {
public function set_object( $data ) {
// Convert array to object.
if ( is_array( $data ) ) {
$data = self::init_from_array( $data );
// Check if the item is an Activity or an Object.
if ( is_activity( $data ) ) {
$data = self::init_from_array( $data );
} elseif ( is_actor( $data ) ) {
$data = Actor::init_from_array( $data );
} else {
$data = Base_Object::init_from_array( $data );
}
}

// Set object.
Expand Down
29 changes: 17 additions & 12 deletions includes/activity/class-base-object.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public function __call( $method, $params ) {
}

if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
$this->add( $var, $params[0] );
return $this->add( $var, $params[0] );
}
}

Expand Down Expand Up @@ -566,10 +566,19 @@ public function add( $key, $value ) {
$this->$key = array();
}

$attributes = $this->$key;
$attributes[] = $value;
if ( is_string( $this->$key ) ) {
$this->$key = array( $this->$key );
}

$attributes = $this->$key;

if ( is_array( $value ) ) {
$attributes = array_merge( $attributes, $value );
} else {
$attributes[] = $value;
}

$this->$key = $attributes;
$this->$key = array_unique( $attributes );

return $this->$key;
}
Expand All @@ -579,13 +588,13 @@ public function add( $key, $value ) {
*
* @param string $json The JSON string.
*
* @return Base_Object An Object built from the JSON string.
* @return Base_Object|WP_Error An Object built from the JSON string or WP_Error when it's not a JSON string.
*/
public static function init_from_json( $json ) {
$array = \json_decode( $json, true );

if ( ! is_array( $array ) ) {
$array = array();
return new WP_Error( 'invalid_json', __( 'Invalid JSON', 'activitypub' ), array( 'status' => 400 ) );
}

return self::init_from_array( $array );
Expand All @@ -600,15 +609,11 @@ public static function init_from_json( $json ) {
*/
public static function init_from_array( $data ) {
if ( ! is_array( $data ) ) {
return new WP_Error( 'invalid_array', __( 'Invalid array', 'activitypub' ), array( 'status' => 404 ) );
return new WP_Error( 'invalid_array', __( 'Invalid array', 'activitypub' ), array( 'status' => 400 ) );
}

$object = new static();

foreach ( $data as $key => $value ) {
$key = camel_to_snake_case( $key );
call_user_func( array( $object, 'set_' . $key ), $value );
}
$object->from_array( $data );

return $object;
}
Expand Down
Loading

0 comments on commit 708c592

Please sign in to comment.