Store activities in a table

This commit is contained in:
Jeremy Dormitzer 2018-08-29 16:38:10 -04:00
parent c1c2725b04
commit cfc1f87b71
4 changed files with 76 additions and 24 deletions

43
inc/activities.php Normal file
View File

@ -0,0 +1,43 @@
<?php
namespace activities;
function get_activity( $id ) {
global $wpdb;
$activity_json = $wpdb->get_var( sprintf(
'SELECT activity FROM activitypub_activities WHERE id = %d', $id
) );
if ( is_null( $activity_json ) ) {
return new \WP_Error(
'not_found', __( 'Activity not found', 'activitypub' ), array( 'status' => 404 )
);
}
$activity = json_decode( $activity_json );
$activity['id'] = get_activity_url( $id );
return $activity;
}
function persist_activity( $activity ) {
global $wpdb;
$wpdb->insert(
'activitypub_activities', array( 'activity' => wp_json_encode( $activity ) )
);
$activity["id"] = get_activity_url( $wpdb->insert_id );
return $activity;
}
function get_activity_url( $id ) {
return get_rest_url( null, sprintf( '/activitypub/v1/activity/%d', $id ) );
}
function create_activities_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS activitypub_activities (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
activity TEXT NOT NULL
);
"
);
}
?>

View File

@ -4,13 +4,15 @@ namespace init;
require_once plugin_dir_path( __FILE__ ) . '/outbox.php';
require_once plugin_dir_path( __FILE__ ) . '/api.php';
require_once plugin_dir_path( __FILE__ ) . '/objects.php';
require_once plugin_dir_path( __FILE__ ) . '/activities.php';
add_action( 'rest_api_init', function() {
\api\register_routes();
} );
add_action( 'activitypub_init', function() {
\outbox\create_outbox_table();
\activities\create_activities_table();
\objects\create_object_table();
\outbox\create_outbox_table();
} );
?>

View File

@ -4,21 +4,27 @@ namespace objects;
function persist_object( $object ) {
global $wpdb;
$wpdb->insert( 'activitypub_objects', array( 'object' => wp_json_encode( $object ) ) );
$object["id"] = get_object_url( $wpdb->insert_id );
$object['id'] = get_object_url( $wpdb->insert_id );
return $object;
}
function get_object( $id ) {
global $wpdb;
$object = json_decode( $wpdb->get_var( sprintf(
$object_json = $wpdb->get_var( sprintf(
'SELECT object FROM activitypub_objects WHERE id = %d', $id
) ) );
$object["id"] = get_object_url( $id );
) );
if ( is_null( $object_json ) ) {
return new \WP_Error(
404, __( 'Object not found', 'activitypub' ), array ( 'status' => 404 )
);
}
$object = json_decode( $object_json );
$object['id'] = get_object_url( $id );
return $object;
}
function get_object_url( $id ) {
return get_rest_url( null, sprintf( '/activitypub/v1/object/%d', $id );
return get_rest_url( null, sprintf( '/activitypub/v1/object/%d', $id ) );
}
function create_object_table() {

View File

@ -13,34 +13,35 @@ When an Activity is received (i.e. POSTed) to an Actor's outbox, the server must
namespace outbox;
require_once plugin_dir_path( __FILE__ ) . '/activities/create.php';
require_once plugin_dir_path( __FILE__ ) . '/activities.php';
function handle_activity( $actor, $activity ) {
if ( !array_key_exists( "type", $activity ) ) {
return new WP_Error(
return new \WP_Error(
'invalid_activity',
__( 'Invalid activity', 'activitypub' ),
array( 'status' => 400 )
);
}
switch ( $activity["type"] ) {
case "Create":
switch ( $activity['type'] ) {
case 'Create':
$activity = \activites\create\handle( $actor, $activity );
break;
case "Update":
case 'Update':
break;
case "Delete":
case 'Delete':
break;
case "Follow":
case 'Follow':
break;
case "Add":
case 'Add':
break;
case "Remove":
case 'Remove':
break;
case "Like":
case 'Like':
break;
case "Block":
case 'Block':
break;
case "Undo":
case 'Undo':
break;
default:
// handle wrapping object in Create activity
@ -60,16 +61,16 @@ function deliver_activity( $activity ) {
function persist_activity( $actor, $activity ) {
global $wpdb;
$activity_json = wp_json_encode( $activity );
$activity = \activities\persist_activity( $activity );
$activity_id = $wpdb->insert_id;
$wpdb->insert( 'activitypub_outbox',
array(
"actor" => $actor,
"activity" => $activity_json,
'actor' => $actor,
'activity_id' => $activity_id,
) );
// TODO hydrate $activity["id"] with URL to activity using $wpdb->insert_id
$response = new WP_REST_Response( $activity );
$response = new WP_REST_Response();
$response->set_status( 201 );
// TODO set location header of response to created activity URL
$response->header( 'Location', $activity['id'] );
return $response;
}
@ -80,7 +81,7 @@ function create_outbox_table() {
CREATE TABLE IF NOT EXISTS activitypub_outbox (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
actor VARCHAR(128) NOT NULL,
activity TEXT NOT NULL
activity_id INT UNSIGNED FOREIGN KEY REFERENCES activitypub_activities(id)
);
"
);