diff --git a/includes/init.php b/includes/init.php index 8858c0d..3df46ce 100644 --- a/includes/init.php +++ b/includes/init.php @@ -3,7 +3,7 @@ namespace init; require_once plugin_dir_path( __FILE__ ) . 'server/api.php'; require_once plugin_dir_path( __FILE__ ) . 'server/actors.php'; -require_once plugin_dir_path( __FILE__ ) . 'migrations.php'; +require_once plugin_dir_path( __FILE__ ) . 'schema.php'; require_once plugin_dir_path( __FILE__ ) . 'server/webfinger.php'; require_once plugin_dir_path( __FILE__ ) . 'client/posts.php'; @@ -21,7 +21,7 @@ add_action( 'pterotype_init', function() { } ); add_action( 'pterotype_load', function() { - \migrations\run_migrations(); + \schema\run_migrations(); } ); add_action( 'generate_rewrite_rules', '\webfinger\generate_rewrite_rules', 111 ); diff --git a/includes/pgp.php b/includes/pgp.php index f18f8ee..2d45190 100644 --- a/includes/pgp.php +++ b/includes/pgp.php @@ -9,7 +9,7 @@ function gen_key( $actor_slug ) { function persist_key( $actor_id, $public_key, $private_key ) { global $wpdb; return $wpdb->replace( - 'pterotype_keys', + $wpdb->prefix . 'pterotype_keys', array( 'actor_id' => $actor_id, 'public_key' => $public_key, @@ -35,14 +35,18 @@ function sign_data( $data, $actor_id ) { function get_public_key( $actor_id ) { global $wpdb; return $wpdb->get_var( $wpdb->prepare( - 'SELECT public_key FROM pterotype_keys WHERE actor_id = %d', $actor_id + "SELECT public_key FROM {$wpdb->prefix}pterotype_keys WHERE actor_id = %d", + $actor_id ) ); } function get_private_key( $actor_id ) { global $wpdb; return $wpdb->get_var( $wpdb->prepare( - 'SELECT private_key FROM pterotype_keys WHERE actor_id = %d', $actor_id + " + SELECT private_key FROM {$wpdb->prefix}pterotype_keys WHERE actor_id = %d + ", + $actor_id ) ); } ?> diff --git a/includes/migrations.php b/includes/schema.php similarity index 53% rename from includes/migrations.php rename to includes/schema.php index 252af0a..6d0eb0d 100644 --- a/includes/migrations.php +++ b/includes/schema.php @@ -1,8 +1,5 @@ =' ) ) { return; } apply_migration( '0.0.1', 'migration_0_0_1' ); - apply_migration( '0.0.2', 'migration_0_0_2' ); - apply_migration( '0.0.3', 'migration_0_0_3' ); - apply_migration( '0.0.4', 'migration_0_0_4' ); - apply_migration( '0.0.5', 'migration_0_0_5' ); update_option( 'pterotype_previously_migrated_version', PTEROTYPE_VERSION ); } @@ -39,51 +29,24 @@ function migration_0_0_1() { global $wpdb; $wpdb->query( " - CREATE TABLE pterotype_activities ( + CREATE TABLE {$wpdb->prefix}pterotype_objects ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, activitypub_id VARCHAR(255) UNIQUE NOT NULL, - activity TEXT NOT NULL + type VARCHAR(50) NOT NULL, + object JSON NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; " ); $wpdb->query( " - CREATE UNIQUE INDEX ACTIVITIES_ACTIVITYPUB_ID_INDEX - ON pterotype_activities (activitypub_id); + CREATE UNIQUE INDEX OBJECTS_ACTIVITYPUB_ID_INDEX + ON {$wpdb->prefix}pterotype_objects (activitypub_id); " ); $wpdb->query( " - CREATE TABLE pterotype_objects ( - id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, - activitypub_id VARCHAR(255) UNIQUE NOT NULL, - object TEXT NOT NULL - ) - ENGINE=InnoDB DEFAULT CHARSET=utf8; - " - ); - $wpdb->query( - " - CREATE UNIQUE INDEX OBJECT_ACTIVITYPUB_ID_INDEX - ON pterotype_objects (activitypub_id); - " - ); - $wpdb->query( - " - CREATE TABLE pterotype_outbox ( - id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, - actor_id INT UNSIGNED NOT NULL, - activity_id INT UNSIGNED NOT NULL, - FOREIGN KEY outbox_activity_fk(activity_id) - REFERENCES pterotype_activities(id) - ) - ENGINE=InnoDB DEFAULT CHARSET=utf8; - " - ); - $wpdb->query( - " - CREATE TABLE pterotype_actors( + CREATE TABLE {$wpdb->prefix}pterotype_actors ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, slug VARCHAR(64) UNIQUE NOT NULL, type VARCHAR(64) NOT NULL @@ -93,150 +56,125 @@ function migration_0_0_1() { ); $wpdb->query( " - CREATE TABLE pterotype_likes ( + CREATE TABLE {$wpdb->prefix}pterotype_outbox ( + id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, actor_id INT UNSIGNED NOT NULL, object_id INT UNSIGNED NOT NULL, - PRIMARY KEY (actor_id, object_id), - FOREIGN KEY likes_actor_fk(actor_id) - REFERENCES pterotype_actors(id), - FOREIGN KEY likes_object_fk(object_id) - REFERENCES pterotype_objects(id) + FOREIGN KEY outbox_object_fk(object_id) + REFERENCES {$wpdb->prefix}pterotype_objects(id), + FOREIGN KEY outbox_actor_fk(actor_id) + REFERENCES {$wpdb->prefix}pterotype_actors(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; " ); $wpdb->query( " - CREATE TABLE pterotype_following( + CREATE TABLE {$wpdb->prefix}pterotype_inbox ( + id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + actor_id INT UNSIGNED NOT NULL, + object_id INT UNSIGNED NOT NULL, + FOREIGN KEY inbox_object_fk(object_id) + REFERENCES {$wpdb->prefix}pterotype_objects(id), + FOREIGN KEY inbox_actor_fk(actor_id) + REFERENCES {$wpdb->prefix}pterotype_actors(id) + ) + ENGINE=InnoDB DEFAULT CHARSET=utf8; + " + ); + $wpdb->query( + " + CREATE TABLE {$wpdb->prefix}pterotype_actor_likes ( + actor_id INT UNSIGNED NOT NULL, + object_id INT UNSIGNED NOT NULL, + PRIMARY KEY (actor_id, object_id), + FOREIGN KEY a_likes_actor_fk(actor_id) + REFERENCES {$wpdb->prefix}pterotype_actors(id), + FOREIGN KEY a_likes_object_fk(object_id) + REFERENCES {$wpdb->prefix}pterotype_objects(id) + ) + ENGINE=InnoDB DEFAULT CHARSET=utf8; + " + ); + $wpdb->query( + " + CREATE TABLE {$wpdb->prefix}pterotype_object_likes ( + object_id INT UNSIGNED NOT NULL, + like_id INT UNSIGNED NOT NULL, + PRIMARY KEY (object_id, like_id), + FOREIGN KEY o_likes_object_fk(object_id) + REFERENCES {$wpdb->prefix}pterotype_objects(id), + FOREIGN KEY o_likes_like_fk(like_id) + REFERENCES {$wpdb->prefix}pterotype_objects(id) + ) + ENGINE=InnoDB DEFAULT CHARSET=utf8; + " + ); + $wpdb->query( + " + CREATE TABLE {$wpdb->prefix}pterotype_following ( actor_id INT UNSIGNED NOT NULL, object_id INT UNSIGNED NOT NULL, state VARCHAR(64) NOT NULL, PRIMARY KEY (actor_id, object_id), FOREIGN KEY following_actor_fk(actor_id) - REFERENCES pterotype_actors(id), + REFERENCES {$wpdb->prefix}pterotype_actors(id), FOREIGN KEY following_object_fk(object_id) - REFERENCES pterotype_objects(id) + REFERENCES {$wpdb->prefix}pterotype_objects(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; " ); $wpdb->query( " - CREATE TABLE pterotype_blocks( - actor_id INT UNSIGNED NOT NULL, - blocked_actor_url TEXT NOT NULL, - FOREIGN KEY blocks_actor_fk(actor_id) - REFERENCES pterotype_actors(id) - ) - ENGINE=InnoDB DEFAULT CHARSET=utf8; - " - ); - $wpdb->query( - " - CREATE TABLE pterotype_inbox ( - id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, - actor_id INT UNSIGNED NOT NULL, - activity_id INT UNSIGNED NOT NULL, - FOREIGN KEY inbox_activity_fk(activity_id) - REFERENCES pterotype_activities(id) - ) - ENGINE=InnoDB DEFAULT CHARSET=utf8; - " - ); -} - -function migration_0_0_2() { - global $wpdb; - $wpdb->query( - " - ALTER TABLE pterotype_objects - MODIFY object JSON NOT NULL, - ADD type VARCHAR(50) NOT NULL; - " - ); - $wpdb->query( - " - ALTER TABLE pterotype_activities - MODIFY activity JSON NOT NULL, - ADD type VARCHAR(50) NOT NULL; - " - ); -} - -function migration_0_0_3() { - global $wpdb; - $wpdb->query( - " - CREATE TABLE pterotype_followers( + CREATE TABLE {$wpdb->prefix}pterotype_followers ( actor_id INT UNSIGNED NOT NULL, object_id INT UNSIGNED NOT NULL, PRIMARY KEY (actor_id, object_id), FOREIGN KEY followers_actor_fk(actor_id) - REFERENCES pterotype_actors(id), + REFERENCES {$wpdb->prefix}pterotype_actors(id), FOREIGN KEY followers_object_fk(object_id) - REFERENCES pterotype_objects(id) + REFERENCES {$wpdb->prefix}pterotype_objects(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; " ); $wpdb->query( " - CREATE TABLE pterotype_shares( + CREATE TABLE {$wpdb->prefix}pterotype_blocks ( + actor_id INT UNSIGNED NOT NULL, + blocked_actor_url TEXT NOT NULL, + FOREIGN KEY blocks_actor_fk(actor_id) + REFERENCES {$wpdb->prefix}pterotype_actors(id) + ) + ENGINE=InnoDB DEFAULT CHARSET=utf8; + " + ); + $wpdb->query( + " + CREATE TABLE {$wpdb->prefix}pterotype_shares ( object_id INT UNSIGNED NOT NULL, announce_id INT UNSIGNED NOT NULL, PRIMARY KEY (object_id, announce_id), FOREIGN KEY shares_object_fk(object_id) - REFERENCES pterotype_objects(id), - FOREIGN KEY shares_activity_fk(announce_id) - REFERENCES pterotype_activities(id) + REFERENCES {$wpdb->prefix}pterotype_objects(id), + FOREIGN KEY shares_announce_fk(announce_id) + REFERENCES {$wpdb->prefix}pterotype_objects(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; " ); -} - -function migration_0_0_4() { - global $wpdb; - /* - pterotype_actor_likes stores things that local actors have liked - */ $wpdb->query( " - ALTER TABLE pterotype_likes RENAME pterotype_actor_likes; - " - ); - /* - pterotype_object_likes stores likes about local objects - */ - $wpdb->query( - " - CREATE TABLE pterotype_object_likes( - object_id INT UNSIGNED NOT NULL, - like_id INT UNSIGNED NOT NULL, - PRIMARY KEY (object_id, like_id), - FOREIGN KEY o_likes_object_fk(object_id) - REFERENCES pterotype_objects(id), - FOREIGN KEY o_likes_activity_fk(like_id) - REFERENCES pterotype_activities(id) - ) - ENGINE=InnoDB DEFAULT CHARSET=utf8; - " - ); -} - -function migration_0_0_5() { - global $wpdb; - $wpdb->query( - ' - CREATE TABLE pterotype_keys( + CREATE TABLE {$wpdb->prefix}pterotype_keys ( actor_id INT UNSIGNED PRIMARY KEY, public_key TEXT NOT NULL, private_key TEXT NOT NULL, FOREIGN KEY keys_actor_fk(actor_id) - REFERENCES pterotype_actors(id) + REFERENCES {$wpdb->prefix}pterotype_actors(id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - ' + " ); } ?> diff --git a/includes/server/activities.php b/includes/server/activities.php deleted file mode 100644 index f7c245e..0000000 --- a/includes/server/activities.php +++ /dev/null @@ -1,140 +0,0 @@ -get_var( $wpdb->prepare( - 'SELECT activity FROM pterotype_activities WHERE id = %d', $id - ) ); - if ( is_null( $activity_json ) ) { - return new \WP_Error( - 'not_found', __( 'Activity not found', 'pterotype' ), array( 'status' => 404 ) - ); - } - $activity = json_decode( $activity_json, true ); - return $activity; -} - -function get_activity_by_activitypub_id( $activitypub_id ) { - global $wpdb; - $activity_json = $wpdb->get_var( $wpdb->prepare( - 'SELECT activity FROM pterotype_activities WHERE id = %s', $activitypub_id - ) ); - if ( is_null( $activity_json ) ) { - return new \WP_Error( - 'not_found', __( 'Activity not found', 'pterotype' ), array( 'status' => 404 ) - ); - } - $activity = json_decode( $activity_json, true ); - return $activity; -} - -function get_activity_id( $activitypub_id ) { - global $wpdb; - return $wpdb->get_var( $wpdb->prepare( - 'SELECT id FROM pterotype_activities WHERE activitypub_id = %s', $activitypub_id - ) ); -} - -function strip_private_fields( $activity ) { - if ( array_key_exists( 'bto', $activity ) ) { - unset( $activity['bto'] ); - } - if ( array_key_exists( 'bcc', $activity ) ) { - unset( $activity['bcc'] ); - } - return $activity; -} - -function persist_activity( $activity ) { - global $wpdb; - $activity = \util\dereference_object( $activity ); - if ( !array_key_exists( 'id', $activity ) ) { - return new \WP_Error( - 'invalid_activity', - __( 'Activity must have an "id" field', 'pterotype' ), - array( 'status' => 400 ) - ); - } - if ( !array_key_exists( 'type', $activity ) ) { - return new \WP_Error( - 'invalid_activity', - __( 'Activity must have a "type" field', 'pterotype' ), - array( 'status' => 400 ) - ); - } - $activitypub_id = $activity['id']; - $type = $activity['type']; - $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; -} - -function create_local_activity( $activity ) { - global $wpdb; - $activity = \util\dereference_object( $activity ); - if ( !array_key_exists( 'type', $activity ) ) { - return new \WP_Error( - 'invalid_activity', - __( 'Activity must have a "type" field', 'pterotype' ), - array( 'status' => 400 ) - ); - } - $type = $activity['type']; - $res = $wpdb->insert( 'pterotype_activities', array( - 'activity' => wp_json_encode( $activity ) - ) ); - if ( !$res ) { - return new \WP_Error( - 'db_error', __( 'Failed to insert activity row', 'pterotype' ) - ); - } - $activity_id = $wpdb->insert_id; - $activity_url = get_rest_url( null, sprintf( '/pterotype/v1/activity/%d', $activity_id ) ); - $activity['id'] = $activity_url; - $res = $wpdb->update( - 'pterotype_activities', - array( - 'activitypub_id' => $activity_url, - 'type' => $type, - 'activity' => wp_json_encode( $activity ), - ), - array( 'id' => $activity_id ), - '%s', - '%d' - ); - if ( !$res ) { - return new \WP_Error( - 'db_error', __( 'Failed to hydrate activity id', 'pterotype' ) - ); - } - return $activity; -} -?> diff --git a/includes/server/actors.php b/includes/server/actors.php index 50e8ae0..29e7b55 100644 --- a/includes/server/actors.php +++ b/includes/server/actors.php @@ -6,7 +6,7 @@ require_once plugin_dir_path( __FILE__ ) . '../pgp.php'; function get_actor( $id ) { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( - 'SELECT * FROM pterotype_actors WHERE id = %d', $id + "SELECT * FROM {$wpdb->prefix}pterotype_actors WHERE id = %d", $id ) ); return get_user_from_row( $row ); } @@ -14,7 +14,7 @@ function get_actor( $id ) { function get_actor_by_slug ( $slug ) { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( - 'SELECT * FROM pterotype_actors WHERE slug = %s', $slug + "SELECT * FROM {$wpdb->prefix}pterotype_actors WHERE slug = %s", $slug ) ); return get_actor_from_row( $row ); } @@ -22,7 +22,7 @@ function get_actor_by_slug ( $slug ) { function get_actor_id( $slug ) { global $wpdb; return $wpdb->get_var( $wpdb->prepare( - 'SELECT id FROM pterotype_actors WHERE slug = %s', $slug + "SELECT id FROM {$wpdb->prefix}pterotype_actors WHERE slug = %s", $slug ) ); } @@ -127,7 +127,7 @@ function get_user_actor( $user ) { function initialize_actors() { global $wpdb; $user_slugs = $wpdb->get_col( - 'SELECT user_nicename FROM wp_users;' + "SELECT user_nicename FROM {$wpdb->users};" ); foreach ( $user_slugs as $user_slug ) { create_actor( $user_slug, 'user' ); @@ -150,7 +150,8 @@ function initialize_actors() { function create_actor( $slug, $type ) { global $wpdb; return $wpdb->query( $wpdb->prepare( - 'INSERT IGNORE INTO pterotype_actors(slug, type) VALUES(%s, %s)', + "INSERT IGNORE INTO {$wpdb->prefix}pterotype_actors(slug, type) + VALUES(%s, %s)", $slug, $type ) ); diff --git a/includes/server/api.php b/includes/server/api.php index d3bc94f..d7fc881 100644 --- a/includes/server/api.php +++ b/includes/server/api.php @@ -5,7 +5,6 @@ require_once plugin_dir_path( __FILE__ ) . 'actors.php'; require_once plugin_dir_path( __FILE__ ) . 'outbox.php'; require_once plugin_dir_path( __FILE__ ) . 'inbox.php'; require_once plugin_dir_path( __FILE__ ) . 'objects.php'; -require_once plugin_dir_path( __FILE__ ) . 'activities.php'; require_once plugin_dir_path( __FILE__ ) . 'following.php'; require_once plugin_dir_path( __FILE__ ) . 'likes.php'; require_once plugin_dir_path( __FILE__ ) . 'shares.php'; @@ -42,11 +41,6 @@ function get_object( $request ) { return \objects\get_object( $id ); } -function get_activity( $request ) { - $id = $request->get_url_params()['id']; - return \activities\get_activity( $id ); -} - function get_following( $request ) { $actor_slug = $request->get_url_params()['actor']; return \following\get_following_collection( $actor_slug ); @@ -97,10 +91,6 @@ function register_routes() { 'methods' => 'GET', 'callback' => __NAMESPACE__ . '\get_object', ) ); - register_rest_route( 'pterotype/v1', '/activity/(?P[0-9]+)', array( - 'methods' => 'GET', - 'callback' => __NAMESPACE__ . '\get_activity', - ) ); register_rest_route( 'pterotype/v1', '/actor/(?P[a-zA-Z0-9-]+)/following', array( 'methods' => 'GET', 'callback' => __NAMESPACE__ . '\get_following', diff --git a/includes/server/blocks.php b/includes/server/blocks.php index a0cadac..0deeeca 100644 --- a/includes/server/blocks.php +++ b/includes/server/blocks.php @@ -10,7 +10,7 @@ get ignored function create_block( $actor_id, $blocked_actor_url ) { global $wpdb; $res = $wpdb->insert( - 'pterotype_blocks', + $wpdb->prefix . 'pterotype_blocks', array( 'actor_id' => $actor_id, 'blocked_actor_url' => $blocked_actor_url ) ); if ( !$res ) { @@ -21,11 +21,11 @@ function create_block( $actor_id, $blocked_actor_url ) { function delete_block( $actor_id, $blocked_actor_url ) { global $wpdb; $res = $wpdb->delete( - 'pterotype_blocks', + $wpdb->prefix . 'pterotype_blocks', array( 'actor_id' => $actor_id, 'blocked_actor_url' => $blocked_actor_url ) ); if ( !$res ) { - return new \WP_Error( 'db_error', __( 'Error inserting block row', 'pterotype' ) ); + return new \WP_Error( 'db_error', __( 'Error deleting block row', 'pterotype' ) ); } } ?> diff --git a/includes/server/deliver.php b/includes/server/deliver.php index b731eca..3013bd9 100644 --- a/includes/server/deliver.php +++ b/includes/server/deliver.php @@ -1,7 +1,6 @@ id; } $wpdb->insert( - 'pterotype_followers', + $wpdb->prefix . 'pterotype_followers', array( 'actor_id' => $actor_id, 'object_id' => $object_id, @@ -64,7 +64,7 @@ function remove_follower( $actor_slug, $follower ) { ); } $wpdb->delete( - 'pterotype_followers', + $wpdb->prefix . 'pterotype_followers', array( 'actor_id' => $actor_id, 'object_id' => $object_id, @@ -84,11 +84,12 @@ function get_followers_collection( $actor_slug ) { } $followers = $wpdb->get_results( $wpdb->prepare( - ' - SELECT object FROM pterotype_followers - JOIN pterotype_objects ON object_id = pterotype_objects.id + " + SELECT object FROM {$wpdb->prefix}pterotype_followers + JOIN {$wpdb->prefix}pterotype_objects + ON object_id = {$wpdb->prefix}pterotype_objects.id WHERE actor_id = %d - ', + ", $actor_id ), ARRAY_A diff --git a/includes/server/following.php b/includes/server/following.php index 0d27f0a..b6266fb 100644 --- a/includes/server/following.php +++ b/includes/server/following.php @@ -9,7 +9,7 @@ define( 'PTEROTYPE_FOLLOW_FOLLOWING', 'FOLLOWING' ); function request_follow( $actor_id, $object_id ) { global $wpdb; return $wpdb->replace( - 'pterotype_following', + $wpdb->prefix . 'pterotype_following', array( 'actor_id' => $actor_id, 'object_id' => $object_id, @@ -22,7 +22,7 @@ function request_follow( $actor_id, $object_id ) { function accept_follow( $actor_id, $object_id ) { global $wpdb; return $wpdb->update( - 'pterotype_following', + $wpdb->prefix . 'pterotype_following', array( 'state' => PTEROTYPE_FOLLOW_FOLLOWING ), array( 'actor_id' => $actor_id, 'object_id' => $object_id ), array( '%s', '%d', '%d' ) @@ -32,7 +32,7 @@ function accept_follow( $actor_id, $object_id ) { function reject_follow( $actor_id, $object_id ) { global $wpdb; return $wpdb->delete( - 'pterotype_following', + $wpdb->prefix . 'pterotype_following', array( 'actor_id' => $actor_id, 'object_id' => $object_id ), '%d' ); @@ -48,12 +48,13 @@ function get_following_collection( $actor_slug ) { } $objects = $wpdb->get_results( $wpdb->prepare( - ' - SELECT object FROM pterotype_following - JOIN pterotype_objects ON pterotype_following.object_id = pterotype_objects.id + " + SELECT object FROM {$wpdb->prefix}pterotype_following + JOIN {$wpdb->prefix}pterotype_objects + ON {$wpdb->prefix}pterotype_following.object_id = {$wpdb->prefix}pterotype_objects.id WHERE actor_id = %d AND state = %s; - ', + ", $actor_id, PTEROTYPE_FOLLOW_FOLLOWING ), ARRAY_A diff --git a/includes/server/inbox.php b/includes/server/inbox.php index 1008578..8e3e53f 100644 --- a/includes/server/inbox.php +++ b/includes/server/inbox.php @@ -9,7 +9,6 @@ When an Activity is received (i.e. POSTed) to an Actor's inbox, the server must: */ namespace inbox; -require_once plugin_dir_path( __FILE__ ) . 'activities.php'; require_once plugin_dir_path( __FILE__ ) . 'objects.php'; require_once plugin_dir_path( __FILE__ ) . 'deliver.php'; require_once plugin_dir_path( __FILE__ ) . 'collections.php'; @@ -75,7 +74,7 @@ function forward_activity( $actor_slug, $activity ) { if ( !array_key_exists( 'id', $activity ) ) { return; } - $seen_before = \activities\get_activity_id( $activity['id'] ); + $seen_before = \objects\get_object_id( $activity['id'] ); if ( $seen_before ) { return; } @@ -123,11 +122,11 @@ function references_local_object( $object, $depth ) { function persist_activity( $actor_slug, $activity ) { global $wpdb; - $activity = \activities\persist_activity( $activity ); + $activity = \objects\upsert_object( $activity ); if ( is_wp_error( $activity ) ) { return $activity; } - $activity_id = \activities\get_activity_id( $activity['id'] ); + $activity_id = \objects\get_object_id( $activity['id'] ); if ( !$activity_id ) { return new \WP_Error( 'db_error', @@ -136,7 +135,8 @@ function persist_activity( $actor_slug, $activity ) { } $actor_id = \actors\get_actor_id( $actor_slug ); $seen_before = $wpdb->get_row( $wpdb->prepare( - 'SELECT * FROM pterotype_inbox WHERE actor_id = %d AND activity_id = %d', + "SELECT * FROM {$wpdb->prefix}pterotype_inbox + WHERE actor_id = %d AND activity_id = %d", $actor_id, $activity_id ) ); @@ -144,7 +144,7 @@ function persist_activity( $actor_slug, $activity ) { return $activity; } $res = $wpdb->insert( - 'pterotype_inbox', + $wpdb->prefix . 'pterotype_inbox', array( 'actor_id' => $actor_id, 'activity_id' => $activity_id, @@ -171,14 +171,15 @@ function get_inbox( $actor_slug ) { ); } $results = $wpdb->get_results( $wpdb->prepare( - ' - SELECT pterotype_activities.activity FROM pterotype_inbox - JOIN pterotype_actors - ON pterotype_actors.id = pterotype_inbox.actor_id - JOIN pterotype_activities - ON pterotype_activities.id = pterotype_inbox.activity_id - WHERE pterotype_inbox.actor_id = %d - ', + " + SELECT {$wpdb->prefix}pterotype_objects.object + FROM {$wpdb->prefix}pterotype_inbox + JOIN {$wpdb->prefix}pterotype_actors + ON {$wpdb->prefix}pterotype_actors.id = {$wpdb->prefix}pterotype_inbox.actor_id + JOIN {$wpdb->prefix}pterotype_objects + ON {$wpdb->prefix}pterotype_objects.id = {$wpdb->prefix}pterotype_inbox.object_id + WHERE {$wpdb->prefix}pterotype_inbox.actor_id = %d + ", $actor_id ), ARRAY_A ); return \collections\make_ordered_collection( array_map( diff --git a/includes/server/likes.php b/includes/server/likes.php index 03ff60c..c0dd31a 100644 --- a/includes/server/likes.php +++ b/includes/server/likes.php @@ -6,7 +6,7 @@ require_once plugin_dir_path( __FILE__ ) . 'collections.php'; function create_local_actor_like( $actor_id, $object_id ) { global $wpdb; return $wpdb->insert( - 'pterotype_actor_likes', + $wpdb->prefix . 'pterotype_actor_likes', array( 'actor_id' => $actor_id, 'object_id' => $object_id ), '%d' ); @@ -15,7 +15,7 @@ function create_local_actor_like( $actor_id, $object_id ) { function delete_local_actor_like( $actor_id, $object_id ) { global $wpdb; return $wpdb->delete( - 'pterotype_actor_likes', + $wpdb->prefix . 'pterotype_actor_likes', array( 'actor_id' => $actor_id, 'object_id' => $object_id ), '%d' ); @@ -24,7 +24,7 @@ function delete_local_actor_like( $actor_id, $object_id ) { function record_like ( $object_id, $like_id ) { global $wpdb; return $wpdb->insert( - 'pterotype_object_likes', + $wpdb->prefix . 'pterotype_object_likes', array( 'object_id' => $object_id, 'like_id' => $like_id @@ -36,7 +36,7 @@ function record_like ( $object_id, $like_id ) { function delete_object_like( $object_id, $like_id ) { global $wpdb; return $wpdb->delete( - 'pterotype_object_likes', + $wpdb->prefix. 'pterotype_object_likes', array( 'object_id' => $object_id, 'like_id' => $like_id @@ -49,11 +49,12 @@ function get_likes_collection( $object_id ) { global $wpdb; $likes = $wpdb->get_results( $wpdb->prepare( - ' - SELECT activity FROM pterotype_object_likes - JOIN pterotype_activities ON like_id = pterotype_activities.id + " + SELECT object FROM {$wpdb->prefix}pterotype_object_likes + JOIN {$wpdb->prefix}pterotype_objects + ON like_id = {$wpdb->prefix}pterotype_objects.id WHERE object_id = %d - ', + ", $object_id ), ARRAY_A diff --git a/includes/server/objects.php b/includes/server/objects.php index 99312cc..3e94b95 100644 --- a/includes/server/objects.php +++ b/includes/server/objects.php @@ -19,7 +19,7 @@ function create_local_object( $object ) { array( 'status' => 400 ) ); } - $res = $wpdb->insert( 'pterotype_objects', array( + $res = $wpdb->insert( $wpdb->prefix . 'pterotype_objects', array( 'object' => wp_json_encode( $object ) ) ); if ( !$res ) { @@ -38,7 +38,7 @@ function create_local_object( $object ) { $object['likes'] = $object_likes; $object['shares'] = $object_shares; $res = $wpdb->update( - 'pterotype_objects', + $wpdb->prefix . 'pterotype_objects', array ( 'activitypub_id' => $object_url, 'type' => $type, @@ -77,12 +77,13 @@ function upsert_object( $object ) { ); } $row = $wpdb->get_row( $wpdb->prepare( - 'SELECT * FROM pterotype_objects WHERE activitypub_id = %s', $object['id'] + "SELECT * FROM {$wpdb->prefix}pterotype_objects WHERE activitypub_id = %s", + $object['id'] ) ); $res = true; if ( $row === null ) { $res = $wpdb->insert( - 'pterotype_objects', + $wpdb->prefix . 'pterotype_objects', array( 'activitypub_id' => $object['id'], 'type' => $object['type'], @@ -93,7 +94,7 @@ function upsert_object( $object ) { $row = new \stdClass(); } else { $res = $wpdb->update( - 'pterotype_objects', + $wpdb->prefix . 'pterotype_objects', array( 'activitypub_id' => $object['id'], 'type' => $object['type'], @@ -129,10 +130,17 @@ function update_object( $object ) { array( 'status' => 400 ) ); } + if ( !array_key_exists( 'type', $object) ) { + return new \WP_Error( + 'invalid_object', + __( 'Object must have a "type" field', 'pterotype' ), + array( 'status' => 400 ) + ); + } $object_json = wp_json_encode( $object ); $res = $wpdb->update( - 'pterotype_objects', - array( 'object' => $object_json ), + $wpdb->prefix . 'pterotype_objects', + array( 'object' => $object_json, 'type' => $object['type'] ), array( 'activitypub_id' => $object['id'] ), '%s', '%d' ); if ( !$res ) { @@ -147,14 +155,16 @@ function update_object( $object ) { function update_referencing_activities( $object ) { global $wpdb; $referencing_activities = $wpdb->get_results( $wpdb->prepare( - 'SELECT * FROM pterotype_activities WHERE activity->"$.object.id" = %s', + " + SELECT * FROM {$wpdb->prefix}pterotype_objects WHERE object->\"$.object.id\" = %s + ", $object['id'] ) ); if ( $referencing_activities ) { foreach ( $referencing_activities as $activity_row ) { $activity = json_decode( $activity_row->activity, true ); $activity['object'] = $object; - \activities\persist_activity( $activity ); + update_object( $activity ); } } } @@ -162,7 +172,7 @@ function update_referencing_activities( $object ) { function get_object( $id ) { global $wpdb; $object_json = $wpdb->get_var( $wpdb->prepare( - 'SELECT object FROM pterotype_objects WHERE id = %d', $id + "SELECT object FROM {$wpdb->prefix}pterotype_objects WHERE id = %d", $id ) ); if ( is_null( $object_json ) ) { return new \WP_Error( @@ -175,7 +185,8 @@ function get_object( $id ) { function get_object_by_activitypub_id( $activitypub_id ) { global $wpdb; $object_json = $wpdb->get_var( $wpdb->prepare( - 'SELECT object FROM pterotype_objects WHERE activitypub_id = %s', $activitypub_id + "SELECT object FROM {$wpdb->prefix}pterotype_objects WHERE activitypub_id = %s", + $activitypub_id ) ); if ( is_null( $object_json ) ) { return new \WP_Error( @@ -188,7 +199,8 @@ function get_object_by_activitypub_id( $activitypub_id ) { function get_object_id( $activitypub_id ) { global $wpdb; return $wpdb->get_var( $wpdb->prepare( - 'SELECT id FROM pterotype_objects WHERE activitypub_id = %s', $activitypub_id + "SELECT id FROM {$wpdb->prefix}pterotype_objects WHERE activitypub_id = %s", + $activitypub_id ) ); } @@ -215,7 +227,7 @@ function delete_object( $object ) { $activitypub_id = $object['id']; $tombstone = make_tombstone( $object ); $res = $wpdb->update( - 'pterotype_objects', + $wpdb->prefix . 'pterotype_objects', array( 'type' => $tombstone['type'], 'object' => wp_json_encode( $tombstone ), @@ -249,4 +261,14 @@ function is_local_object( $object ) { } return \util\is_local_url( $url ); } + +function strip_private_fields( $object ) { + if ( array_key_exists( 'bto', $object ) ) { + unset( $object['bto'] ); + } + if ( array_key_exists( 'bcc', $object ) ) { + unset( $object['bcc'] ); + } + return $object; +} ?> diff --git a/includes/server/outbox.php b/includes/server/outbox.php index 3966330..078f673 100644 --- a/includes/server/outbox.php +++ b/includes/server/outbox.php @@ -12,7 +12,6 @@ When an Activity is received (i.e. POSTed) to an Actor's outbox, the server must */ namespace outbox; -require_once plugin_dir_path( __FILE__ ) . 'activities.php'; require_once plugin_dir_path( __FILE__ ) . 'actors.php'; require_once plugin_dir_path( __FILE__ ) . 'deliver.php'; require_once plugin_dir_path( __FILE__ ) . 'activities/create.php'; @@ -113,7 +112,7 @@ function handle_activity( $actor_slug, $activity ) { return $activity; } // the activity may have changed while processing side effects, so persist the new version - $activity = \activities\persist_activity( $activity ); + $activity = \objects\upsert_object( $activity ); if ( is_wp_error( $activity ) ) { return $activity; } @@ -137,14 +136,15 @@ function get_outbox( $actor_slug ) { ); } $results = $wpdb->get_results( $wpdb->prepare( - ' - SELECT pterotype_activities.activity FROM pterotype_outbox - JOIN pterotype_actors - ON pterotype_actors.id = pterotype_outbox.actor_id - JOIN pterotype_activities - ON pterotype_activities.id = pterotype_outbox.activity_id - WHERE pterotype_outbox.actor_id = %d - ', + " + SELECT {$wpdb->prefix}pterotype_objects.object + FROM {$wpdb->prefix}pterotype_outbox + JOIN {$wpdb->prefix}pterotype_actors + ON {$wpdb->prefix}pterotype_actors.id = {$wpdb->prefix}pterotype_outbox.actor_id + JOIN {$wpdb->prefix}pterotype_objects + ON {$wpdb->prefix}pterotype_objects.id = {$wpdb->prefix}pterotype_outbox.object_id + WHERE {$wpdb->prefix}pterotype_outbox.actor_id = %d + ", $actor_id ), ARRAY_A ); // TODO return PagedCollection if $activites is too big @@ -158,19 +158,19 @@ function get_outbox( $actor_slug ) { function deliver_activity( $actor_slug, $activity ) { \deliver\deliver_activity( $actor_slug, $activity ); - $activity = \activities\strip_private_fields( $activity ); + $activity = \objects\strip_private_fields( $activity ); return $activity; } function persist_activity( $actor_slug, $activity ) { global $wpdb; - $activity = \activities\strip_private_fields( $activity ); - $activity = \activities\create_local_activity( $activity ); + $activity = \objects\strip_private_fields( $activity ); + $activity = \objects\create_local_object( $activity ); $activity_id = $wpdb->insert_id; $actor_id = \actors\get_actor_id( $actor_slug ); - $res = $wpdb->insert( 'pterotype_outbox', array( + $res = $wpdb->insert( $wpdb->prefix . 'pterotype_outbox', array( 'actor_id' => $actor_id, - 'activity_id' => $activity_id, + 'object_id' => $activity_id, ) ); if ( !$res ) { return new \WP_Error( diff --git a/includes/server/shares.php b/includes/server/shares.php index 775ccd9..7bf70cd 100644 --- a/includes/server/shares.php +++ b/includes/server/shares.php @@ -6,10 +6,10 @@ require_once plugin_dir_path( __FILE__ ) . 'collections.php'; function add_share( $object_id, $activity_id ) { global $wpdb; return $wpdb->insert( - 'pterotype_shares', + $wpdb->prefix . 'pterotype_shares', array( 'object_id' => $object_id, - 'activity_id' => $activity_id, + 'announce_id' => $activity_id, ), '%d' ); @@ -19,11 +19,12 @@ function get_shares_collection( $object_id ) { global $wpdb; $shares = $wpdb->get_results( $wpdb->prepare( - ' - SELECT activity FROM pterotype_shares - JOIN pterotype_activities ON announce_id = pterotype_activities.id + " + SELECT object FROM {$wpdb->prefix}pterotype_shares + JOIN {$wpdb->prefix}pterotype_objects + ON announce_id = {$wpdb->prefix}pterotype_objects.id WHERE object_id = %d - ', + ", $object_id ), ARRAY_A diff --git a/includes/util.php b/includes/util.php index 2330281..557fdd0 100644 --- a/includes/util.php +++ b/includes/util.php @@ -1,9 +1,6 @@