2018-08-28 23:26:18 +00:00
|
|
|
<?php
|
|
|
|
namespace objects;
|
|
|
|
|
2018-09-01 14:14:11 +00:00
|
|
|
// TODO for 'post' objects, store a post id instead of the full post text,
|
|
|
|
// then hydrate the text on read
|
|
|
|
|
2018-09-01 13:37:01 +00:00
|
|
|
function create_object( $object ) {
|
2018-08-28 23:26:18 +00:00
|
|
|
global $wpdb;
|
2018-09-01 13:37:01 +00:00
|
|
|
$res = $wpdb->insert(
|
|
|
|
'activitypub_objects', array( 'object' => wp_json_encode( $object ) )
|
|
|
|
);
|
|
|
|
if ( !$res ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'db_error', __( 'Failed to insert object row', 'activitypub' )
|
|
|
|
);
|
|
|
|
}
|
2018-08-29 20:38:10 +00:00
|
|
|
$object['id'] = get_object_url( $wpdb->insert_id );
|
2018-08-28 23:26:18 +00:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2018-09-01 13:37:01 +00:00
|
|
|
function update_object( $object ) {
|
|
|
|
global $wpdb;
|
|
|
|
if ( !array_key_exists( 'id', $object ) ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'invalid_object',
|
|
|
|
__( 'Object must have an "id" parameter', 'activitypub' ),
|
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$id = get_id_from_url( $object['id'] );
|
|
|
|
$object_json = wp_json_encode( $object );
|
|
|
|
$res = $wpdb->update(
|
|
|
|
'activitypub_object',
|
|
|
|
array( 'object' => $object_json ),
|
|
|
|
array( 'id' => $id ),
|
|
|
|
'%s', '%d' );
|
|
|
|
if ( !$res ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'db_error', __( 'Failed to update object row', 'activitypub' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:26:18 +00:00
|
|
|
function get_object( $id ) {
|
|
|
|
global $wpdb;
|
2018-08-29 20:38:10 +00:00
|
|
|
$object_json = $wpdb->get_var( sprintf(
|
2018-08-28 23:26:18 +00:00
|
|
|
'SELECT object FROM activitypub_objects WHERE id = %d', $id
|
2018-08-29 20:38:10 +00:00
|
|
|
) );
|
|
|
|
if ( is_null( $object_json ) ) {
|
|
|
|
return new \WP_Error(
|
2018-09-01 14:14:11 +00:00
|
|
|
'not_found', __( 'Object not found', 'activitypub' ), array( 'status' => 404 )
|
2018-08-29 20:38:10 +00:00
|
|
|
);
|
|
|
|
}
|
2018-08-29 21:04:24 +00:00
|
|
|
$object = json_decode( $object_json, true );
|
2018-08-29 20:38:10 +00:00
|
|
|
$object['id'] = get_object_url( $id );
|
2018-08-28 23:26:18 +00:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2018-09-01 14:14:11 +00:00
|
|
|
function delete_object( $object ) {
|
|
|
|
global $wpdb;
|
|
|
|
if ( !array_key_exists( 'id', $object ) ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'invalid_object',
|
|
|
|
__( 'Object must have an "id" parameter', 'activitypub' ),
|
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$id = get_id_from_url( $object['id'] );
|
|
|
|
$res = $wpdb->delete( 'activitypub_objects', array( 'id' => $id ), '%d' );
|
|
|
|
if ( !$res ) {
|
|
|
|
return new \WP_Error( 'db_error', __( 'Error deleting object', 'activitypub' ) );
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2018-09-01 13:37:01 +00:00
|
|
|
function get_id_from_url( $url ) {
|
|
|
|
global $wpdb;
|
|
|
|
$matches = array();
|
|
|
|
$found = preg_match(
|
|
|
|
get_rest_url( null, '/activitypub/v1/object/(.+)' ), $url, $matches );
|
|
|
|
if ( $found === 0 || count( $matches ) != 2 ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'invalid_url',
|
|
|
|
sprintf( '%s %s', $url, __( 'is not a valid object url', 'activitypub' ) ),
|
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$id = $matches[1];
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_object_from_url( $url ) {
|
|
|
|
return get_object( get_id_from_url( $url ) );
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:26:18 +00:00
|
|
|
function get_object_url( $id ) {
|
2018-08-29 20:38:10 +00:00
|
|
|
return get_rest_url( null, sprintf( '/activitypub/v1/object/%d', $id ) );
|
2018-08-28 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
?>
|