diff --git a/inc/api.php b/inc/api.php index 08dac34..2469c8a 100644 --- a/inc/api.php +++ b/inc/api.php @@ -2,17 +2,28 @@ namespace api; require_once plugin_dir_path( __FILE__ ) . '/actors.php'; +require_once plugin_dir_path( __FILE__ ) . '/outbox.php'; -function get_user_actor( $data ) { - $handle = $data["handle"]; +function get_user_actor( $request ) { + $handle = $request['handle']; $id = get_user_by( 'slug', $handle ); return \actors\user_to_actor( $id ); } +function post_to_outbox( $request ) { + $handle = $request['handle'] ; + $activity = json_decode( $request->get_body() ); + return \outbox\create_activity( $handle, $activity ); +} + function register_routes() { register_rest_route( 'activitypub/v1', '/actor/(?P[a-zA-Z0-9-]+)', array( 'methods' => 'GET', 'callback' => __NAMESPACE__ . '\get_user_actor', ) ); + register_rest_route( 'activitypub/v1', '/actor/(?P[a-zA-Z0-9-]+)/outbox', array( + 'methods' => 'POST', + 'callback' => __NAMESPACE__ . '\post_to_outbox', + ) ); } ?> diff --git a/inc/outbox.php b/inc/outbox.php index a9e93c6..1154258 100644 --- a/inc/outbox.php +++ b/inc/outbox.php @@ -14,7 +14,7 @@ namespace outbox; function create_outbox_table() { global $wpdb; - $wpdb->Query( + $wpdb->query( " CREATE TABLE IF NOT EXISTS activitypub_outbox ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, @@ -24,4 +24,18 @@ function create_outbox_table() { " ); } + +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, + ) ); + return json_decode( $wpdb->get_var( sprintf( + "SELECT activity FROM activitypub_outbox WHERE id = %d", $wpdb->insert_id + ) ) ); +} ?>