Implement posting to outbox

This commit is contained in:
Jeremy Dormitzer 2018-08-22 22:39:59 -04:00
parent d790c425ef
commit 7c7957b82b
2 changed files with 28 additions and 3 deletions

View File

@ -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<handle>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\get_user_actor',
) );
register_rest_route( 'activitypub/v1', '/actor/(?P<handle>[a-zA-Z0-9-]+)/outbox', array(
'methods' => 'POST',
'callback' => __NAMESPACE__ . '\post_to_outbox',
) );
}
?>

View File

@ -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
) ) );
}
?>