2018-08-21 13:17:07 +00:00
|
|
|
<?php
|
2018-08-22 12:33:21 +00:00
|
|
|
/*
|
|
|
|
When an Activity is received (i.e. POSTed) to an Actor's outbox, the server must:
|
2018-08-21 13:17:07 +00:00
|
|
|
|
2018-08-22 12:34:28 +00:00
|
|
|
0. Make sure the request is authenticated
|
2018-08-22 12:33:21 +00:00
|
|
|
1. Add the Activity to the Actor's outbox collection in the DB
|
|
|
|
2. Deliver the Activity to the appropriate inboxes based on the received Activity
|
|
|
|
This involves discovering all the inboxes, including nested ones if the target
|
|
|
|
is a collection, deduplicating inboxes, and the POSTing the Activity to each
|
|
|
|
target inbox.
|
|
|
|
3. Perform side effects as necessary
|
|
|
|
*/
|
2018-08-23 02:07:06 +00:00
|
|
|
namespace outbox;
|
|
|
|
|
|
|
|
function create_outbox_table() {
|
|
|
|
global $wpdb;
|
2018-08-23 02:39:59 +00:00
|
|
|
$wpdb->query(
|
2018-08-23 02:07:06 +00:00
|
|
|
"
|
|
|
|
CREATE TABLE IF NOT EXISTS activitypub_outbox (
|
|
|
|
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
|
|
|
actor VARCHAR(128) NOT NULL,
|
|
|
|
activity TEXT NOT NULL
|
|
|
|
);
|
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
2018-08-23 02:39:59 +00:00
|
|
|
|
|
|
|
function create_activity( $actor, $activity ) {
|
|
|
|
// TODO validate activity and actor; handle errors
|
|
|
|
global $wpdb;
|
|
|
|
$activity_json = wp_json_encode($activity);
|
|
|
|
$wpdb->insert( 'activitypub_outbox',
|
|
|
|
array(
|
|
|
|
"actor" => $actor,
|
|
|
|
"activity" => $activity_json,
|
|
|
|
) );
|
2018-08-25 13:31:03 +00:00
|
|
|
$persisted = json_decode( $wpdb->get_var( sprintf(
|
2018-08-23 02:39:59 +00:00
|
|
|
"SELECT activity FROM activitypub_outbox WHERE id = %d", $wpdb->insert_id
|
|
|
|
) ) );
|
2018-08-25 13:31:03 +00:00
|
|
|
$response = new WP_REST_Response( $persisted );
|
|
|
|
$response->set_status( 201 );
|
|
|
|
// TODO set location header of response to created object URL
|
|
|
|
return $response;
|
|
|
|
}
|
2018-08-25 13:31:20 +00:00
|
|
|
|
|
|
|
function handle_activity( $actor, $activity ) {
|
|
|
|
if ( !array_key_exists( "type", $activity ) ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'invalid_activity', 'Invalid activity', array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
switch ( $activity["type"] ) {
|
|
|
|
case "Create":
|
|
|
|
break;
|
|
|
|
case "Update":
|
|
|
|
break;
|
|
|
|
case "Delete":
|
|
|
|
break;
|
|
|
|
case "Follow":
|
|
|
|
break;
|
|
|
|
case "Add":
|
|
|
|
break;
|
|
|
|
case "Remove":
|
|
|
|
break;
|
|
|
|
case "Like":
|
|
|
|
break;
|
|
|
|
case "Block":
|
|
|
|
break;
|
|
|
|
case "Undo":
|
|
|
|
break;
|
|
|
|
}
|
2018-08-23 02:39:59 +00:00
|
|
|
}
|
2018-08-21 13:17:07 +00:00
|
|
|
?>
|