diff --git a/includes/server/activities.php b/includes/server/activities.php index 189a5b8..f7c245e 100644 --- a/includes/server/activities.php +++ b/includes/server/activities.php @@ -67,11 +67,33 @@ function persist_activity( $activity ) { } $activitypub_id = $activity['id']; $type = $activity['type']; - $wpdb->replace( 'pterotype_activities', array( - 'activitypub_id' => $activitypub_id, - 'type' => $type, - 'activity' => wp_json_encode( $activity ) + $row = $wpdb->get_row( $wpdb->prepare( + 'SELECT * FROM pterotype_activities WHERE activitypub_id = %s', $activitypub_id ) ); + $res = true; + if ( $row === null ) { + $res = $wpdb->insert( + 'pterotype_activities', + array( + 'activitypub_id' => $activitypub_id, + 'type' => $type, + 'activity' => wp_json_encode( $activity ) + ), + '%s' + ); + } else { + $res = $wpdb->update( + 'pterotype_activities', + array( + 'activitypub_id' => $activitypub_id, + 'type' => $type, + 'activity' => wp_json_encode( $activity ) + ), + array( 'id' => $row->id ), + '%s', + '%d' + ); + } return $activity; } @@ -97,15 +119,16 @@ function create_local_activity( $activity ) { $activity_id = $wpdb->insert_id; $activity_url = get_rest_url( null, sprintf( '/pterotype/v1/activity/%d', $activity_id ) ); $activity['id'] = $activity_url; - $res = $wpdb->replace( + $res = $wpdb->update( 'pterotype_activities', array( - 'id' => $activity_id, 'activitypub_id' => $activity_url, 'type' => $type, 'activity' => wp_json_encode( $activity ), ), - array( '%d', '%s', '%s', '%s' ) + array( 'id' => $activity_id ), + '%s', + '%d' ); if ( !$res ) { return new \WP_Error( diff --git a/includes/server/actors.php b/includes/server/actors.php index aa573b3..17558f7 100644 --- a/includes/server/actors.php +++ b/includes/server/actors.php @@ -115,10 +115,10 @@ function initialize_actors() { function create_actor( $slug, $type ) { global $wpdb; - $wpdb->replace( - 'pterotype_actors', - array( 'slug' => $slug, 'type' => $type ), - '%s' - ); + return $wpdb->query( $wpdb->prepare( + 'INSERT IGNORE INTO pterotype_actors(slug, type) VALUES(%s, %s)', + $slug, + $type + ) ); } ?> diff --git a/includes/server/following.php b/includes/server/following.php index 4f18220..0d27f0a 100644 --- a/includes/server/following.php +++ b/includes/server/following.php @@ -14,7 +14,8 @@ function request_follow( $actor_id, $object_id ) { 'actor_id' => $actor_id, 'object_id' => $object_id, 'state' => PTEROTYPE_FOLLOW_PENDING - ) + ), + array( '%d', '%d', '%s' ) ); } diff --git a/includes/server/objects.php b/includes/server/objects.php index c0e48ff..66c59c2 100644 --- a/includes/server/objects.php +++ b/includes/server/objects.php @@ -37,15 +37,16 @@ function create_local_object( $object ) { $object['id'] = $object_url; $object['likes'] = $object_likes; $object['shares'] = $object_shares; - $res = $wpdb->replace( + $res = $wpdb->update( 'pterotype_objects', array ( - 'id' => $object_id, 'activitypub_id' => $object_url, 'type' => $type, 'object' => wp_json_encode( $object ) ), - array( '%d', '%s', '%s', '%s' ) + array( 'id' => $object_id ), + '%s', + '%d' ); if ( !$res ) { return new \WP_Error( @@ -76,7 +77,7 @@ function upsert_object( $object ) { ); } $row = $wpdb->get_row( $wpdb->prepare( - 'SELECT * FROM pterotype_objects WHERE activitypub_url = %s', $object['id'] + 'SELECT * FROM pterotype_objects WHERE activitypub_id = %s', $object['id'] ) ); $res = true; if ( $row === null ) { @@ -86,18 +87,20 @@ function upsert_object( $object ) { 'activitypub_id' => $object['id'], 'type' => $object['type'], 'object' => wp_json_encode( $object ) - ) + ), + '%s' ); } else { - $res = $wpdb->replace( + $res = $wpdb->update( 'pterotype_objects', array( - 'id' => $row->id, 'activitypub_id' => $object['id'], 'type' => $object['type'], 'object' => wp_json_encode( $object ) ), - array( '%d', '%s', '%s', '%s' ) + array( 'id' => $row->id ), + '%s', + '%d' ); $row = new stdClass(); $row->id = $wpdb->insert_id; @@ -219,14 +222,15 @@ function delete_object( $object ) { } $activitypub_id = $object['id']; $tombstone = make_tombstone( $object ); - $res = $wpdb->replace( + $res = $wpdb->update( 'pterotype_objects', array( - 'activitypub_id' => $activitypub_id, 'type' => $tombstone['type'], 'object' => wp_json_encode( $tombstone ), ), - array( '%s', '%s', '%s' ) + array( 'activitypub_id' => $activitypub_id ), + '%s', + '%s' ); if ( !$res ) { return new \WP_Error( 'db_error', __( 'Error deleting object', 'pterotype' ) ); diff --git a/includes/server/outbox.php b/includes/server/outbox.php index 4b6a792..1f6c812 100644 --- a/includes/server/outbox.php +++ b/includes/server/outbox.php @@ -25,6 +25,7 @@ require_once plugin_dir_path( __FILE__ ) . 'activities/undo.php'; require_once plugin_dir_path( __FILE__ ) . '../util.php'; function handle_activity( $actor_slug, $activity ) { + xdebug_break(); // TODO handle authentication/authorization $activity = \util\dereference_object( $activity ); if ( is_wp_error( $activity ) ) { @@ -113,7 +114,6 @@ function handle_activity( $actor_slug, $activity ) { return $activity; } // the activity may have changed while processing side effects, so persist the new version - // TODO why was 'id' missing from the activity here? $activity = \activities\persist_activity( $activity ); if ( is_wp_error( $activity ) ) { return $activity;