[WIP] Fix posting bugs

There is still something funky with deletion, I think
This commit is contained in:
Jeremy Dormitzer 2018-10-07 21:18:49 -04:00
parent 26dbfe6d2c
commit 384bcba338
7 changed files with 30 additions and 43 deletions

View File

@ -4,22 +4,22 @@ namespace posts;
require_once plugin_dir_path( __FILE__ ) . '../server/activities/create.php';
function handle_post_status_change( $new_status, $old_status, $post ) {
// TODO find a way to get the id of an existing object for the post, if it exists
$actor_slug = PTEROTYPE_BLOG_ACTOR_SLUG;
$actor_outbox = get_rest_url(
null, sprintf( 'pterotype/v1/actor/%s/outbox', $actor_slug )
);
$post_object = post_to_object( $post );
$activity = null;
if ( $new_status == 'publish' && $old_status != 'publish' ) {
// Create
$post_object = post_to_object( $post );
$activity = \activities\create\make_create( $actor_slug, $post_object );
} else if ( $new_status == 'publish' && $old_status == 'publish' ) {
// Update
$post_object = post_to_object( $post );
$activity = \activities\update\make_update( $actor_slug, $post_object );
} else if ( $new_status != 'publish' && $old_status == 'publish' ) {
// Delete
// TODO delete isn't working
$post_object = post_to_object( $post, true );
$activity = \activities\delete\make_delete( $actor_slug, $post_object );
}
if ( $activity && ! is_wp_error( $activity ) ) {
@ -35,7 +35,11 @@ function handle_post_status_change( $new_status, $old_status, $post ) {
/**
Return an object of type Article
*/
function post_to_object( $post ) {
function post_to_object( $post, $deleted = false ) {
$permalink = get_permalink( $post );
if ( $deleted ) {
$permalink = substr( $permalink, 0, -10 ) . '/';
}
$object = array(
'@context' => array( 'https://www.w3.org/ns/activitystreams' ),
'type' => 'Article',
@ -44,20 +48,20 @@ function post_to_object( $post ) {
'attributedTo' => get_rest_url(
null, sprintf( '/pterotype/v1/actor/%s', PTEROTYPE_BLOG_ACTOR_SLUG )
),
'url' => get_permalink( $post ),
'url' => $permalink,
'summary' => $post->post_excerpt,
);
$existing = get_existing_object( $post );
$existing = get_existing_object( $permalink );
if ( $existing ) {
$object['id'] = $existing->activitypub_id;
}
return $object;
}
function get_existing_object( $post ) {
function get_existing_object( $permalink ) {
global $wpdb;
return $wpdb->get_row( $wpdb->prepare(
'SELECT * FROM pterotype_objects WHERE object->"$.url" = %s', get_permalink( $post )
'SELECT * FROM pterotype_objects WHERE object->"$.url" = %s', $permalink
) );
}
?>

View File

@ -96,7 +96,7 @@ function make_create( $actor_slug, $object ) {
return $actor;
}
$activity = array(
'@context' => 'https://www.w3.org/ns/activitystreams',
'@context' => array( 'https://www.w3.org/ns/activitystreams' ),
'type' => 'Create',
'actor' => $actor,
'object' => $object

View File

@ -13,10 +13,11 @@ function handle_outbox( $actor, $activity ) {
);
}
$object = $activity['object'];
$res = \objects\delete_object( $object );
if ( is_wp_error( $res ) ) {
return $res;
$tombstone = \objects\delete_object( $object );
if ( is_wp_error( $tombstone ) ) {
return $tombstone;
}
$activity['object'] = $tombstone;
return $activity;
}
@ -74,7 +75,7 @@ function make_delete( $actor_slug, $object ) {
return $actor;
}
return array(
'@context' => 'https://www.w3.org/ns/activitystreams',
'@context' => array( 'https://www.w3.org/ns/activitystreams' ),
'type' => 'Delete',
'actor' => $actor,
'object' => $object

View File

@ -82,7 +82,7 @@ function make_accept( $actor_slug, $follow ) {
);
}
$accept = array(
'@context' => 'https://www.w3.org/ns/activitystreams',
'@context' => array( 'https://www.w3.org/ns/activitystreams' ),
'type' => 'Accept',
'actor' => \actors\get_actor_by_slug( $actor_slug ),
'object' => $follow,

View File

@ -99,7 +99,7 @@ function make_update( $actor_slug, $object ) {
return $actor;
}
return array(
'@context' => 'https://www.w3.org/ns/activitystreams',
'@context' => array( 'https://www.w3.org/ns/activitystreams' ),
'type' => 'Update',
'actor' => $actor,
'object' => $object

View File

@ -3,7 +3,7 @@ namespace collections;
function make_ordered_collection( $objects ) {
$ordered_collection = array(
'@context' => 'https://www.w3.org/ns/activitystreams',
'@context' => array( 'https://www.w3.org/ns/activitystreams' ),
'type' => 'OrderedCollection',
'totalItems' => count( $objects ),
'orderedItems' => $objects

View File

@ -105,19 +105,7 @@ function upsert_object( $object ) {
);
$row = new \stdClass();
$row->id = $wpdb->insert_id;
$activites_res = $wpdb->query( $wpdb->prepare(
'
UPDATE pterotype_activities
SET activity = JSON_SET(activity, "$.object", %s)
WHERE activity->"$.object.id" = %s;
',
wp_json_encode( $object ), $object['id']
) );
if ( $activities_res === false ) {
return new \WP_Error(
'db_error', __( 'Failed to update associated activities', 'pterotype' )
);
}
update_referencing_activities( $object );
}
if ( !$res ) {
return new \WP_Error(
@ -152,6 +140,12 @@ function update_object( $object ) {
'db_error', __( 'Failed to update object row', 'pterotype' )
);
}
update_referencing_activities( $object );
return $object;
}
function update_referencing_activities( $object ) {
global $wpdb;
$referencing_activities = $wpdb->get_results( $wpdb->prepare(
'SELECT * FROM pterotype_activities WHERE activity->"$.object.id" = %s',
$object['id']
@ -163,7 +157,6 @@ function update_object( $object ) {
\activities\persist_activity( $activity );
}
}
return $object;
}
function get_object( $id ) {
@ -234,24 +227,13 @@ function delete_object( $object ) {
if ( !$res ) {
return new \WP_Error( 'db_error', __( 'Error deleting object', 'pterotype' ) );
}
$res = $wpdb->query( $wpdb->prepare(
'
UPDATE pterotype_activities
SET activity = JSON_SET(activity, "$.object", %s)
WHERE activity->"$.object.id" = %s;
',
wp_json_encode( $tombstone ), $object['id']
) );
if ( $res === false ) {
return new \WP_Error(
'db_error', __( 'Failed to update associated activities', 'pterotype' )
);
}
update_referencing_activities( $tombstone );
return $tombstone;
}
function make_tombstone( $object ) {
$tombstone = array(
'@context' => array( 'https://www.w3.org/ns/activitystreams' ),
'type' => 'Tombstone',
'formerType' => $object['type'],
'id' => $object['id'],