Hydrate object/activity ids on create

This commit is contained in:
Jeremy Dormitzer 2018-09-19 07:56:57 -04:00
parent 1e808f3eca
commit e8899425f7
4 changed files with 50 additions and 12 deletions

View File

@ -56,6 +56,36 @@ function persist_activity( $activity ) {
return $activity;
}
function create_local_activity( $activity ) {
global $wpdb;
$res = $wpdb->insert( 'activitypub_activities', array(
'activity' => wp_json_encode( $activity )
) );
if ( !$res ) {
return new \WP_Error(
'db_error', __( 'Failed to insert activity row', 'activitypub' )
);
}
$activity_id = $wpdb->insert_id;
$activity_url = get_rest_url( null, sprintf( '/activitypub/v1/activity/%d', $id ) );
$activity['id'] = $activity_url;
$res = $wpdb->replace(
'activitypub_activities',
array(
'id' => $activity_id,
'activitypub_id' => $activity_url,
'activity' => $activity
),
array( '%d', '%s', '%s' )
);
if ( !$res ) {
return new \WP_Error(
'db_error', __( 'Failed to hydrate activity id', 'activitypub' )
);
}
return $activity;
}
function create_activities_table() {
global $wpdb;
$wpdb->query(

View File

@ -23,7 +23,6 @@ function handle_outbox( $actor, $activity ) {
);
}
if ( !array_key_exists( 'actor', $activity ) ) {
// TODO validate that $activity['actor'] is the URL of the $actor
return new \WP_Error(
'invalid_actor', __( 'Expecting a valid actor', 'activitypub' )
);
@ -33,7 +32,7 @@ function handle_outbox( $actor, $activity ) {
$object['attributedTo'] = $attributed_actor;
reconcile_receivers( $object, $activity );
$object = scrub_object( $object );
$object = \objects\create_object( $object );
$object = \objects\create_local_object( $object );
if ( is_wp_error( $object ) ) {
return $object;
}

View File

@ -4,17 +4,9 @@ namespace objects;
// TODO for 'post' objects, store a post id instead of the full post text,
// then hydrate the text on read
function create_object( $object ) {
function create_local_object( $object ) {
global $wpdb;
if ( !array_key_exists( 'id', $object ) ) {
return new \WP_Error(
'invalid_object',
__( 'Object must have an "id" field', 'activitypub' ),
array( 'status' => 400 )
);
}
$res = $wpdb->insert( 'activitypub_objects', array(
'activitypub_id' => $object['id'],
'object' => wp_json_encode( $object )
) );
if ( !$res ) {
@ -22,6 +14,23 @@ function create_object( $object ) {
'db_error', __( 'Failed to insert object row', 'activitypub' )
);
}
$object_id = $wpdb->insert_id;
$object_url = get_rest_url( null, sprintf( '/activitypub/v1/object/%d', $object_id ) );
$object['id'] = $object_url;
$res = $wpdb->replace(
'activitypub_objects',
array (
'id' => $object_id,
'activitypub_id' => $object_url,
'object' => wp_json_encode( $object )
),
array( '%d', '%s', '%s' )
);
if ( !$res ) {
return new \WP_Error(
'db_error', __( 'Failed to hydrate object id', 'activitypub' )
);
}
return $object;
}

View File

@ -117,7 +117,7 @@ function deliver_activity( $activity ) {
function persist_activity( $actor_slug, $activity ) {
global $wpdb;
$activity = \activities\persist_activity( $activity );
$activity = \activities\create_local_activity( $activity );
$activity_id = $wpdb->insert_id;
$actor_id = \actors\get_actor_id( $actor_slug );
$wpdb->insert( 'activitypub_outbox', array(