Implement Create activity

This commit is contained in:
Jeremy Dormitzer 2018-08-28 18:07:04 -04:00
parent 8f8f08c303
commit be908690d4
2 changed files with 62 additions and 34 deletions

View File

@ -6,6 +6,8 @@ Create a new post or comment (depending on $activity["object"]["type"]),
copying $activity["actor"] to the object's "attributedTo" and
copying any recipients of the activity that aren't on the object
to the object and vice-versa.
Returns either the modified $activity or a WP_Error.
*/
function handle( $actor, $activity ) {
if ( !(array_key_exists( "type", $activity ) && $activity["type"] === "Create") ) {
@ -27,13 +29,27 @@ function handle( $actor, $activity ) {
$object = $activity["object"];
$actor_id = $activity["actor"];
$object["attributedTo"] = $actor_id;
reconcile_receivers( $object, $activity );
$activity["object"] = $object;
return $activity;
}
function reconcile_receivers( $object, $activity ) {
// TODO copy "audience", "to" "bto", "cc", "bcc"
// to both object and activity from each other
function reconcile_receivers( &$object, &$activity ) {
copy_field_value( "audience", $object, $activity );
copy_field_value( "audience", $activity, $object );
copy_field_value( "to", $object, $activity );
copy_field_value( "to", $activity, $object );
copy_field_value( "bto", $object, $activity );
copy_field_value( "bto", $activity, $object );
copy_field_value( "cc", $object, $activity );
copy_field_value( "cc", $activity, $object );
copy_field_value( "bcc", $object, $activity );
copy_field_value( "bcc", $activity, $object );
}
function copy_field_value( $field, $from, &$to ) {

View File

@ -12,36 +12,7 @@ When an Activity is received (i.e. POSTed) to an Actor's outbox, the server must
*/
namespace outbox;
function create_outbox_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS activitypub_outbox (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
actor VARCHAR(128) NOT NULL,
activity TEXT NOT NULL
);
"
);
}
function persist_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,
) );
$persisted = json_decode( $wpdb->get_var( sprintf(
"SELECT activity FROM activitypub_outbox WHERE id = %d", $wpdb->insert_id
) ) );
$response = new WP_REST_Response( $persisted );
$response->set_status( 201 );
// TODO set location header of response to created object URL
return $response;
}
require_once plugin_dir_path( __FILE__ ) . '/activities/create.php';
function handle_activity( $actor, $activity ) {
if ( !array_key_exists( "type", $activity ) ) {
@ -53,6 +24,7 @@ function handle_activity( $actor, $activity ) {
}
switch ( $activity["type"] ) {
case "Create":
$activity = \activites\create\handle( $actor, $activity );
break;
case "Update":
break;
@ -74,5 +46,45 @@ function handle_activity( $actor, $activity ) {
// handle wrapping object in Create activity
break;
}
if ( is_wp_error( $activity ) ) {
return $activity;
} else {
deliver_activity( $activity );
return persist_activity( $actor, $activity );
}
}
function deliver_activity( $activity ) {
// TODO
}
function persist_activity( $actor, $activity ) {
global $wpdb;
$activity_json = wp_json_encode($activity);
$wpdb->insert( 'activitypub_outbox',
array(
"actor" => $actor,
"activity" => $activity_json,
) );
$persisted = json_decode( $wpdb->get_var( sprintf(
"SELECT activity FROM activitypub_outbox WHERE id = %d", $wpdb->insert_id
) ) );
$response = new WP_REST_Response( $persisted );
$response->set_status( 201 );
// TODO set location header of response to created object URL
return $response;
}
function create_outbox_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS activitypub_outbox (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
actor VARCHAR(128) NOT NULL,
activity TEXT NOT NULL
);
"
);
}
?>