Skip to content

Commit 708c592

Browse files
authored
Outbox Collection (#593)
1 parent a6fd182 commit 708c592

File tree

64 files changed

+5979
-2319
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+5979
-2319
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [Untitled]
8+
## [Unreleased]
99

1010
### Changed
1111

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

3434
* Support for WPML post locale
3535

36+
### Added
37+
38+
* Outbox queue
39+
40+
### Changed
41+
42+
* Rewrite the current dispatcher system, to use the Outbox instead of the Scheduler.
43+
3644
### Removed
3745

3846
* Built-in support for nodeinfo2. Use the [NodeInfo plugin](https://wordpress.org/plugins/nodeinfo/) instead.

activitypub.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@
4040
*/
4141
function rest_init() {
4242
Rest\Actors::init();
43-
Rest\Outbox::init();
4443
Rest\Inbox::init();
4544
Rest\Followers::init();
4645
Rest\Following::init();
4746
Rest\Comment::init();
4847
Rest\Server::init();
4948
Rest\Collection::init();
5049
Rest\Post::init();
51-
( new Rest\Interaction_Controller() )->register_routes();
5250
( new Rest\Application_Controller() )->register_routes();
51+
( new Rest\Interaction_Controller() )->register_routes();
52+
( new Rest\Outbox_Controller() )->register_routes();
5353
( new Rest\Webfinger_Controller() )->register_routes();
5454

5555
// Load NodeInfo endpoints only if blog is public.
@@ -65,7 +65,7 @@ function rest_init() {
6565
function plugin_init() {
6666
\add_action( 'init', array( __NAMESPACE__ . '\Migration', 'init' ) );
6767
\add_action( 'init', array( __NAMESPACE__ . '\Activitypub', 'init' ) );
68-
\add_action( 'init', array( __NAMESPACE__ . '\Activity_Dispatcher', 'init' ) );
68+
\add_action( 'init', array( __NAMESPACE__ . '\Dispatcher', 'init' ) );
6969
\add_action( 'init', array( __NAMESPACE__ . '\Handler', 'init' ) );
7070
\add_action( 'init', array( __NAMESPACE__ . '\Admin', 'init' ) );
7171
\add_action( 'init', array( __NAMESPACE__ . '\Hashtag', 'init' ) );

includes/activity/class-activity.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
use Activitypub\Link;
1313

14+
use function Activitypub\is_actor;
15+
use function Activitypub\is_activity;
16+
1417
/**
1518
* \Activitypub\Activity\Activity implements the common
1619
* attributes of an Activity.
@@ -154,7 +157,14 @@ class Activity extends Base_Object {
154157
public function set_object( $data ) {
155158
// Convert array to object.
156159
if ( is_array( $data ) ) {
157-
$data = self::init_from_array( $data );
160+
// Check if the item is an Activity or an Object.
161+
if ( is_activity( $data ) ) {
162+
$data = self::init_from_array( $data );
163+
} elseif ( is_actor( $data ) ) {
164+
$data = Actor::init_from_array( $data );
165+
} else {
166+
$data = Base_Object::init_from_array( $data );
167+
}
158168
}
159169

160170
// Set object.

includes/activity/class-base-object.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ public function __call( $method, $params ) {
483483
}
484484

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

@@ -566,10 +566,19 @@ public function add( $key, $value ) {
566566
$this->$key = array();
567567
}
568568

569-
$attributes = $this->$key;
570-
$attributes[] = $value;
569+
if ( is_string( $this->$key ) ) {
570+
$this->$key = array( $this->$key );
571+
}
572+
573+
$attributes = $this->$key;
574+
575+
if ( is_array( $value ) ) {
576+
$attributes = array_merge( $attributes, $value );
577+
} else {
578+
$attributes[] = $value;
579+
}
571580

572-
$this->$key = $attributes;
581+
$this->$key = array_unique( $attributes );
573582

574583
return $this->$key;
575584
}
@@ -579,13 +588,13 @@ public function add( $key, $value ) {
579588
*
580589
* @param string $json The JSON string.
581590
*
582-
* @return Base_Object An Object built from the JSON string.
591+
* @return Base_Object|WP_Error An Object built from the JSON string or WP_Error when it's not a JSON string.
583592
*/
584593
public static function init_from_json( $json ) {
585594
$array = \json_decode( $json, true );
586595

587596
if ( ! is_array( $array ) ) {
588-
$array = array();
597+
return new WP_Error( 'invalid_json', __( 'Invalid JSON', 'activitypub' ), array( 'status' => 400 ) );
589598
}
590599

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

606615
$object = new static();
607-
608-
foreach ( $data as $key => $value ) {
609-
$key = camel_to_snake_case( $key );
610-
call_user_func( array( $object, 'set_' . $key ), $value );
611-
}
616+
$object->from_array( $data );
612617

613618
return $object;
614619
}

0 commit comments

Comments
 (0)