Implement object GET url and hydrate object ids

This commit is contained in:
Jeremy Dormitzer 2018-08-28 19:26:18 -04:00
parent d061f8db2e
commit c1c2725b04
4 changed files with 50 additions and 21 deletions

View File

@ -1,6 +1,8 @@
<?php
namespace activities\create;
require_once plugin_dir_path( __FILE__ ) . '/../objects.php';
/*
Create a new post or comment (depending on $activity["object"]["type"]),
copying $activity["actor"] to the object's "attributedTo" and
@ -31,7 +33,7 @@ function handle( $actor, $activity ) {
$object["attributedTo"] = $actor_id;
reconcile_receivers( $object, $activity );
scrub_object( $object );
$object = persist_object( $object );
$object = \objects\persist_object( $object );
$activity["object"] = $object;
return $activity;
}
@ -67,22 +69,4 @@ function scrub_object( &$object ) {
unset( $object["bcc"] );
unset( $object["bto"] );
}
function persist_object( &$object ) {
global $wpdb;
$wpdb->insert( 'activitypub_objects', array( "object" => wp_json_encode( $object ) ) );
// TODO hydrate $object["id"] to URL of object using $wpdb->insert_id
}
function create_object_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS activitypub_objects (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
object TEXT NOT NULL
);
"
);
}
?>

View File

@ -3,6 +3,7 @@ namespace api;
require_once plugin_dir_path( __FILE__ ) . '/actors.php';
require_once plugin_dir_path( __FILE__ ) . '/outbox.php';
require_once plugin_dir_path( __FILE__ ) . '/objects.php';
function get_user_actor( $request ) {
$handle = $request['handle'];
@ -16,6 +17,11 @@ function post_to_outbox( $request ) {
return \outbox\persist_activity( $handle, $activity );
}
function get_object( $request ) {
$id = $request['id'];
return \objects\get_object( $id );
}
function register_routes() {
register_rest_route( 'activitypub/v1', '/actor/(?P<handle>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
@ -25,5 +31,9 @@ function register_routes() {
'methods' => 'POST',
'callback' => __NAMESPACE__ . '\post_to_outbox',
) );
register_rest_route( 'activitypub/v1', '/object/(?P<id>[0-9]+)', array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\get_object',
) );
}
?>

View File

@ -3,7 +3,7 @@ namespace init;
require_once plugin_dir_path( __FILE__ ) . '/outbox.php';
require_once plugin_dir_path( __FILE__ ) . '/api.php';
require_once plugin_dir_path( __FILE__ ) . '/activities/create.php';
require_once plugin_dir_path( __FILE__ ) . '/objects.php';
add_action( 'rest_api_init', function() {
\api\register_routes();
@ -11,6 +11,6 @@ add_action( 'rest_api_init', function() {
add_action( 'activitypub_init', function() {
\outbox\create_outbox_table();
\activities\create\create_object_table();
\objects\create_object_table();
} );
?>

35
inc/objects.php Normal file
View File

@ -0,0 +1,35 @@
<?php
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 );
return $object;
}
function get_object( $id ) {
global $wpdb;
$object = json_decode( $wpdb->get_var( sprintf(
'SELECT object FROM activitypub_objects WHERE id = %d', $id
) ) );
$object["id"] = get_object_url( $id );
return $object;
}
function get_object_url( $id ) {
return get_rest_url( null, sprintf( '/activitypub/v1/object/%d', $id );
}
function create_object_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS activitypub_objects (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
object TEXT NOT NULL
);
"
);
}
?>