diff --git a/inc/activities.php b/inc/activities.php index 5102101..8d12fb6 100644 --- a/inc/activities.php +++ b/inc/activities.php @@ -49,7 +49,7 @@ function persist_activity( $activity ) { ); } $activitypub_id = $activity['id']; - $wpdb->insert( 'pterotype_activities', array( + $wpdb->replace( 'pterotype_activities', array( 'activitypub_id' => $activitypub_id, 'activity' => wp_json_encode( $activity ) ) ); diff --git a/inc/activities/create.php b/inc/activities/create.php index 5299cea..7a0f02b 100644 --- a/inc/activities/create.php +++ b/inc/activities/create.php @@ -11,7 +11,7 @@ to the object and vice-versa. Returns either the modified $activity or a WP_Error. */ -function handle_outbox( $actor, $activity ) { +function handle_outbox( $actor_slug, $activity ) { if ( !(array_key_exists( 'type', $activity ) && $activity['type'] === 'Create') ) { return new \WP_Error( 'invalid_activity', __( 'Expecting a Create activity', 'activitypub' ) @@ -40,6 +40,21 @@ function handle_outbox( $actor, $activity ) { return $activity; } +function handle_inbox( $actor_slug, $activity ) { + if ( !array_key_exists( 'object', $activity ) ) { + return new \WP_Error( + 'invalid_activity', + __( 'Expected an object', 'pterotype' ), + array( 'status' => 400 ) + ); + } + $object = \objects\upsert_object( $object ); + if ( is_wp_error( $object ) ) { + return $object; + } + return $activity; +} + function reconcile_receivers( &$object, &$activity ) { copy_field_value( 'audience', $object, $activity ); copy_field_value( 'audience', $activity, $object ); diff --git a/inc/inbox.php b/inc/inbox.php index 96780ec..3f9e513 100644 --- a/inc/inbox.php +++ b/inc/inbox.php @@ -6,15 +6,13 @@ When an Activity is received (i.e. POSTed) to an Actor's inbox, the server must: See (https://www.w3.org/TR/activitypub/#x7-1-2-forwarding-from-inbox). 2. Perform the side effects of receiving the Activity 3. Persist the activity in the actor's inbox (and the attached object, if necessary) - -To persist an activity or object: - 1. Check if the activity or object already exists in the DB - 2. If yes, do nothing - 3. If no, add it to the DB */ namespace inbox; -function handle_activity( $actor, $activity ) { +require_once plugin_dir_path( __FILE__ ) . '/activities.php'; +require_once plugin_dir_path( __FILE__ ) . '/activities/create.php'; + +function handle_activity( $actor_slug, $activity ) { if ( !array_key_exists( 'type', $activity ) ) { return new \WP_Error( 'invalid_activity', @@ -25,6 +23,7 @@ function handle_activity( $actor, $activity ) { forward_activity( $activity ); switch ( $activity['type'] ) { case 'Create': + $activity = \create\handle_inbox( $actor_slug, $activity ); break; case 'Update': break; @@ -48,32 +47,26 @@ function handle_activity( $actor, $activity ) { if ( is_wp_error( $activity ) ) { return $activity; } - persist_activity( $activity ); - return new \WP_REST_Response(); + return persist_activity( $actor_slug, $activity ); } function forward_activity( $activity ) { - + // TODO } -function persist_activity( $activity ) { +function persist_activity( $actory_slug, $activity ) { global $wpdb; - -} - -function create_inbox_table() { - global $wpdb; - $wpdb->query( - " - CREATE TABLE IF NOT EXISTS pterotype_inbox( - id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, - actor_id UNSIGNED INT NOT NULL, - activity_id INT UNSIGNED NOT NULL, - FOREIGN KEY inbox_activity_fk(activity_id) - REFERENCES pterotype_activities(id) - ) - ENGINE=InnoDB DEFAULT CHARSET=utf8; - " - ); + $activity = \activities\persist_activity( $activity ); + if ( is_wp_error( $activity ) ) { + return $activity; + } + $activity_id = $wpdb->insert_id; + $actor_id = \actors\get_actor_id( $actor_slug ); + $wpdb->insert( 'pterotype_inbox', array( + 'actor_id' => $actor_id, + 'activity_id' => $activity_id, + ) ); + $response = new \WP_Rest_Response(); + return $response; } ?>