From 3fc6fd546c7a70ee1cdb25c0fb5382556f1275b9 Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Mon, 3 Sep 2018 12:47:08 -0400 Subject: [PATCH] [WIP] Start moving actors to their own table; start Like activity --- inc/activities.php | 2 +- inc/activities/delete.php | 2 +- inc/activities/like.php | 4 ++++ inc/actors.php | 43 ++++++++++++++++++++++++++++++++++++++- inc/init.php | 2 ++ inc/likes.php | 16 +++++++++++++++ inc/objects.php | 2 +- inc/outbox.php | 2 ++ 8 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 inc/activities/like.php create mode 100644 inc/likes.php diff --git a/inc/activities.php b/inc/activities.php index 767ee6c..7eb0dc4 100644 --- a/inc/activities.php +++ b/inc/activities.php @@ -3,7 +3,7 @@ namespace activities; function get_activity( $id ) { global $wpdb; - $activity_json = $wpdb->get_var( sprintf( + $activity_json = $wpdb->get_var( $wpdb->prepare( 'SELECT activity FROM activitypub_activities WHERE id = %d', $id ) ); if ( is_null( $activity_json ) ) { diff --git a/inc/activities/delete.php b/inc/activities/delete.php index 40d9367..21a7fb4 100644 --- a/inc/activities/delete.php +++ b/inc/activities/delete.php @@ -1,5 +1,5 @@ diff --git a/inc/actors.php b/inc/actors.php index 31482bc..603b2df 100644 --- a/inc/actors.php +++ b/inc/actors.php @@ -1,7 +1,35 @@ get_row( $wpdb->prepare( + 'SELECT * FROM activitypub_actors WHERE id = %d', $id + ) ); + return get_user_from_row( $row ); +} + +function get_actor_by_slug ( $slug ) { + global $wpdb; + $row = $wpdb->get_row( $wpdb->prepare( + 'SELECT * FROM activitypub_actors WHERE slug = %s', $slug + ) ); + return get_user_from_row( $row ); +} + +function get_user_from_row( $row ) { + switch ( $row->type ) { + case "user": + $user = get_user_by( 'slug', $row->slug ); + return get_user_actor( $user ); + case "commenter": + return new \WP_Error( + 'not_implemented', __( 'Commenter actors not yet implemented', 'activitypub' ) + ); + } +} + +function get_user_actor( $user ) { $handle = get_the_author_meta( 'user_nicename', $user->get('ID')); $actor = array( "@context" => array( "https://www.w3.org/ns/activitystreams" ), @@ -25,4 +53,17 @@ function get_actor( $user ) { ); return $actor; } + +function create_actors_table() { + global $wpdb; + $wpdb->query( + " + CREATE TABLE IF NOT EXISTS activitypub_actors( + id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + slug VARCHAR(255) UNIQUE NOT NULL, + type VARCHAR(64) + ); + " + ); +} ?> diff --git a/inc/init.php b/inc/init.php index dcabbb9..006b5eb 100644 --- a/inc/init.php +++ b/inc/init.php @@ -5,6 +5,7 @@ require_once plugin_dir_path( __FILE__ ) . '/outbox.php'; require_once plugin_dir_path( __FILE__ ) . '/api.php'; require_once plugin_dir_path( __FILE__ ) . '/objects.php'; require_once plugin_dir_path( __FILE__ ) . '/activities.php'; +require_once plugin_dir_path( __FILE__ ) . '/actors.php'; add_action( 'rest_api_init', function() { \api\register_routes(); @@ -14,5 +15,6 @@ add_action( 'activitypub_init', function() { \activities\create_activities_table(); \objects\create_object_table(); \outbox\create_outbox_table(); + \actors\create_actors_table(); } ); ?> diff --git a/inc/likes.php b/inc/likes.php new file mode 100644 index 0000000..09ef9cc --- /dev/null +++ b/inc/likes.php @@ -0,0 +1,16 @@ +query( + " + CREATE TABLE IF NOT EXISTS activitypub_likes ( + actor_id INT NOT NULL, + object_url TEXT NOT NULL, + + ); + " + ); +} +?> diff --git a/inc/objects.php b/inc/objects.php index aa5095c..70f6c52 100644 --- a/inc/objects.php +++ b/inc/objects.php @@ -44,7 +44,7 @@ function update_object( $object ) { function get_object( $id ) { global $wpdb; - $object_json = $wpdb->get_var( sprintf( + $object_json = $wpdb->get_var( $wpdb->prepare( 'SELECT object FROM activitypub_objects WHERE id = %d', $id ) ); if ( is_null( $object_json ) ) { diff --git a/inc/outbox.php b/inc/outbox.php index 540a40d..dc112aa 100644 --- a/inc/outbox.php +++ b/inc/outbox.php @@ -16,6 +16,7 @@ require_once plugin_dir_path( __FILE__ ) . '/activities.php'; require_once plugin_dir_path( __FILE__ ) . '/activities/create.php'; require_once plugin_dir_path( __FILE__ ) . '/activities/update.php'; require_once plugin_dir_path( __FILE__ ) . '/activities/delete.php'; +require_once plugin_dir_path( __FILE__ ) . '/activities/like.php'; function handle_activity( $actor, $activity ) { // TODO handle authentication/authorization @@ -43,6 +44,7 @@ function handle_activity( $actor, $activity ) { case 'Remove': break; case 'Like': + $activity = \activities\like\handle( $actor, $activity ); break; case 'Block': break;