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-19 11:56:57 +00:00
|
|
|
function create_local_object( $object ) {
|
2018-08-28 23:26:18 +00:00
|
|
|
global $wpdb;
|
2018-09-19 15:16:41 +00:00
|
|
|
$res = $wpdb->insert( 'pterotype_activitypub_objects', array(
|
2018-09-19 02:55:47 +00:00
|
|
|
'object' => wp_json_encode( $object )
|
|
|
|
) );
|
2018-09-01 13:37:01 +00:00
|
|
|
if ( !$res ) {
|
|
|
|
return new \WP_Error(
|
2018-09-19 15:16:41 +00:00
|
|
|
'db_error', __( 'Failed to insert object row', 'pterotype' )
|
2018-09-01 13:37:01 +00:00
|
|
|
);
|
|
|
|
}
|
2018-09-19 11:56:57 +00:00
|
|
|
$object_id = $wpdb->insert_id;
|
2018-09-19 15:16:41 +00:00
|
|
|
$object_url = get_rest_url( null, sprintf( '/pterotype/v1/object/%d', $object_id ) );
|
2018-09-19 11:56:57 +00:00
|
|
|
$object['id'] = $object_url;
|
|
|
|
$res = $wpdb->replace(
|
2018-09-19 15:16:41 +00:00
|
|
|
'pterotype_activitypub_objects',
|
2018-09-19 11:56:57 +00:00
|
|
|
array (
|
|
|
|
'id' => $object_id,
|
|
|
|
'activitypub_id' => $object_url,
|
|
|
|
'object' => wp_json_encode( $object )
|
|
|
|
),
|
|
|
|
array( '%d', '%s', '%s' )
|
|
|
|
);
|
|
|
|
if ( !$res ) {
|
|
|
|
return new \WP_Error(
|
2018-09-19 15:16:41 +00:00
|
|
|
'db_error', __( 'Failed to hydrate object id', 'pterotype' )
|
2018-09-19 11:56:57 +00:00
|
|
|
);
|
|
|
|
}
|
2018-08-28 23:26:18 +00:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2018-09-19 12:54:07 +00:00
|
|
|
function upsert_object( $object ) {
|
|
|
|
global $wpdb;
|
|
|
|
if ( !array_key_exists( 'id', $object ) ) {
|
|
|
|
return new \WP_Error(
|
|
|
|
'invalid_object',
|
2018-09-19 15:16:41 +00:00
|
|
|
__( 'Objects must have an "id" field', 'pterotype' ),
|
2018-09-19 12:54:07 +00:00
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$row = $wpdb->get_row( $wpdb->prepare(
|
2018-09-19 15:16:41 +00:00
|
|
|
'SELECT * FROM pterotype_activitypub_objects WHERE activitypub_url = %s', $object['id']
|
2018-09-19 12:54:07 +00:00
|
|
|
) );
|
|
|
|
$res = true;
|
|
|
|
if ( $row === null ) {
|
|
|
|
$res = $wpdb->insert(
|
2018-09-19 15:16:41 +00:00
|
|
|
'pterotype_activitypub_objects',
|
2018-09-19 12:54:07 +00:00
|
|
|
array(
|
|
|
|
'activitypub_id' => $object['id'],
|
|
|
|
'object' => wp_json_encode( $object )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$res = $wpdb->replace(
|
2018-09-19 15:16:41 +00:00
|
|
|
'pterotype_activitypub_objects',
|
2018-09-19 12:54:07 +00:00
|
|
|
array(
|
|
|
|
'id' => $row->id,
|
|
|
|
'activitypub_id' => $object['id'],
|
|
|
|
'object' => wp_json_encode( $object )
|
|
|
|
),
|
|
|
|
array( '%d', '%s', '%s' )
|
|
|
|
);
|
|
|
|
$row = new stdClass();
|
|
|
|
$row->id = $wpdb->insert_id;
|
|
|
|
}
|
|
|
|
if ( !$res ) {
|
|
|
|
return new \WP_Error(
|
2018-09-19 15:16:41 +00:00
|
|
|
'db_error', __( 'Failed to upsert object row', 'pterotype' )
|
2018-09-19 12:54:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
$row->object = $object;
|
|
|
|
return $row;
|
|
|
|
}
|
|
|
|
|
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',
|
2018-09-19 15:16:41 +00:00
|
|
|
__( 'Object must have an "id" field', 'pterotype' ),
|
2018-09-01 13:37:01 +00:00
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$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(
|
2018-09-19 15:16:41 +00:00
|
|
|
'db_error', __( 'Failed to update object row', 'pterotype' )
|
2018-09-01 13:37:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:26:18 +00:00
|
|
|
function get_object( $id ) {
|
|
|
|
global $wpdb;
|
2018-09-03 16:47:08 +00:00
|
|
|
$object_json = $wpdb->get_var( $wpdb->prepare(
|
2018-09-19 15:16:41 +00:00
|
|
|
'SELECT object FROM pterotype_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-19 15:16:41 +00:00
|
|
|
'not_found', __( 'Object not found', 'pterotype' ), array( 'status' => 404 )
|
2018-08-29 20:38:10 +00:00
|
|
|
);
|
|
|
|
}
|
2018-09-19 02:55:47 +00:00
|
|
|
return json_decode( $object_json, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_object_by_activitypub_id( $activitypub_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
$object_json = $wpdb->get_var( $wpdb->prepare(
|
2018-09-19 15:16:41 +00:00
|
|
|
'SELECT object FROM pterotype_activitypub_objects WHERE activitypub_id = %s', $activitypub_id
|
2018-09-19 02:55:47 +00:00
|
|
|
) );
|
|
|
|
if ( is_null( $object_json ) ) {
|
|
|
|
return new \WP_Error(
|
2018-09-19 15:16:41 +00:00
|
|
|
'not_found', __( 'Object not found', 'pterotype' ), array( 'status' => 404 )
|
2018-09-19 02:55:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return json_decode( $object_json, true );
|
2018-08-28 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
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',
|
2018-09-19 15:16:41 +00:00
|
|
|
__( 'Object must have an "id" field', 'pterotype' ),
|
2018-09-01 14:14:11 +00:00
|
|
|
array( 'status' => 400 )
|
|
|
|
);
|
|
|
|
}
|
2018-09-19 02:55:47 +00:00
|
|
|
$activitypub_id = $object['id'];
|
2018-09-19 15:16:41 +00:00
|
|
|
$res = $wpdb->delete( 'pterotype_activitypub_objects', array( 'activitypub_id' => $id ), '%s' );
|
2018-09-01 14:14:11 +00:00
|
|
|
if ( !$res ) {
|
2018-09-19 15:16:41 +00:00
|
|
|
return new \WP_Error( 'db_error', __( 'Error deleting object', 'pterotype' ) );
|
2018-09-01 14:14:11 +00:00
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:26:18 +00:00
|
|
|
function create_object_table() {
|
|
|
|
global $wpdb;
|
|
|
|
$wpdb->query(
|
|
|
|
"
|
2018-09-19 15:16:41 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS pterotype_activitypub_objects (
|
2018-08-28 23:26:18 +00:00
|
|
|
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
2018-09-19 12:54:07 +00:00
|
|
|
activitypub_id VARCHAR(255) UNIQUE NOT NULL,
|
2018-08-28 23:26:18 +00:00
|
|
|
object TEXT NOT NULL
|
2018-09-19 12:54:07 +00:00
|
|
|
)
|
|
|
|
ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
2018-08-28 23:26:18 +00:00
|
|
|
"
|
|
|
|
);
|
2018-09-19 02:55:47 +00:00
|
|
|
$wpdb->query(
|
|
|
|
"
|
2018-09-19 15:16:41 +00:00
|
|
|
CREATE UNIQUE INDEX OBJECT_ACTIVITYPUB_ID_INDEX
|
|
|
|
ON pterotype_activitypub_objects (activitypub_id);
|
2018-09-19 02:55:47 +00:00
|
|
|
"
|
|
|
|
);
|
2018-08-28 23:26:18 +00:00
|
|
|
}
|
|
|
|
?>
|