diff --git a/inc/activities/delete.php b/inc/activities/delete.php index 21a7fb4..6046a7a 100644 --- a/inc/activities/delete.php +++ b/inc/activities/delete.php @@ -6,7 +6,9 @@ require_once plugin_dir_path( __FILE__ ) . '/../objects.php'; function handle( $actor, $activity ) { if ( !array_key_exists( 'object', $activity ) ) { return new \WP_Error( - 'invalid_activity', __( 'Expected an object', 'activitypub' ) + 'invalid_activity', + __( 'Expected an object', 'activitypub' ), + array( 'status' => 40 ) ); } $object = $activity['object']; diff --git a/inc/activities/like.php b/inc/activities/like.php index 300034b..e6db718 100644 --- a/inc/activities/like.php +++ b/inc/activities/like.php @@ -1,4 +1,31 @@ 400 ) + ); + } + $object = $activity['object']; + if ( !array_key_exists( 'id', $object ) ) { + return new \WP_Error( + 'invalid_object', + __( 'Expected an object id', 'activitypub' ), + array( 'status' => 400 ) + ); + } + $object_id = $object['id']; + $actor_id = \actors\get_actor_id( $actor ); + $res = \likes\create_like( $actor_id, $object_id ); + if ( is_wp_error( $res ) ) { + return $res; + } + return $activity; +} ?> diff --git a/inc/actors.php b/inc/actors.php index 7e660c3..1edaade 100644 --- a/inc/actors.php +++ b/inc/actors.php @@ -17,6 +17,13 @@ function get_actor_by_slug ( $slug ) { return get_user_from_row( $row ); } +function get_actor_id( $slug ) { + global $wpdb; + return $wpdb->get_var( $wpdb->prepare( + "SELECT slug FROM activitypub_actors WHERE slug = %s", $slug + ) ); +} + function get_user_from_row( $row ) { switch ( $row->type ) { case "user": diff --git a/inc/init.php b/inc/init.php index 4f6decf..9acf95c 100644 --- a/inc/init.php +++ b/inc/init.php @@ -6,6 +6,7 @@ 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'; +require_once plugin_dir_path( __FILE__ ) . '/likes.php'; add_action( 'rest_api_init', function() { \api\register_routes(); @@ -22,5 +23,6 @@ add_action( 'activitypub_init', function() { \outbox\create_outbox_table(); \actors\create_actors_table(); \actors\initialize_user_actors(); + \likes\create_likes_table(); } ); ?> diff --git a/inc/likes.php b/inc/likes.php index 09ef9cc..ceeb752 100644 --- a/inc/likes.php +++ b/inc/likes.php @@ -1,6 +1,13 @@ insert( + 'activitypub_likes', array( 'actor_id' => $actor_id, 'object_url' => $object_id ) + ); +} + function create_likes_table() { global $wpdb; $wpdb->query( @@ -8,7 +15,8 @@ function create_likes_table() { CREATE TABLE IF NOT EXISTS activitypub_likes ( actor_id INT NOT NULL, object_url TEXT NOT NULL, - + FOREIGN KEY actor_fk(actor_id) + REFERENCES activitypub_actors(id) ); " );