diff --git a/inc/activities.php b/inc/activities.php index 108e358..f43f6ad 100644 --- a/inc/activities.php +++ b/inc/activities.php @@ -29,6 +29,13 @@ function get_activity_by_activitypub_id( $activitypub_id ) { 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'] ); diff --git a/inc/activities/announce.php b/inc/activities/announce.php new file mode 100644 index 0000000..07d59b2 --- /dev/null +++ b/inc/activities/announce.php @@ -0,0 +1,45 @@ + 400 ) + ); + } + $object = $activity['object']; + if ( !array_key_exists( 'id', $object ) ) { + return new \WP_Error( + 'invalid_activity', + __( 'Expected an "id" field', 'pterotype' ), + array( 'status' => 400 ) + ); + } + if ( !\objects\is_local_object( $object ) ) { + return $activity; + } + $object_id = \objects\get_object_id( $object['id'] ); + if ( !$object_id ) { + return new \WP_Error( + 'not_found', + __( 'Object not found', 'pterotype' ), + array( 'status' => 404 ) + ); + } + $activity_id = \activities\get_activity_id( $activity['id'] ); + if ( !$activity_id ) { + return new \WP_Error( + 'not_found', + __( 'Activity not found', 'pterotype' ), + array( 'status' => 404 ) + ); + } + \shares\add_share( $object_id, $activity_id ); + return $activity; +} +?> diff --git a/inc/inbox.php b/inc/inbox.php index 5cedbb1..ab09937 100644 --- a/inc/inbox.php +++ b/inc/inbox.php @@ -16,6 +16,7 @@ require_once plugin_dir_path( __FILE__ ) . '/activities/delete.php'; require_once plugin_dir_path( __FILE__ ) . '/activities/follow.php'; require_once plugin_dir_path( __FILE__ ) . '/activities/accept.php'; require_once plugin_dir_path( __FILE__ ) . '/activities/reject.php'; +require_once plugin_dir_path( __FILE__ ) . '/activities/announce.php'; function handle_activity( $actor_slug, $activity ) { if ( !array_key_exists( 'type', $activity ) ) { @@ -26,6 +27,10 @@ function handle_activity( $actor_slug, $activity ) { ); } forward_activity( $activity ); + $res = persist_activity( $actor_slug, $activity ); + if ( is_wp_error( $res ) ) { + return $res; + } switch ( $activity['type'] ) { case 'Create': $activity = \activities\create\handle_inbox( $actor_slug, $activity ); @@ -60,6 +65,7 @@ function handle_activity( $actor_slug, $activity ) { ); break; case 'Announce': + $activity = \activities\announce\handle_inbox( $actor_slug, $activity ); break; case 'Undo': // TODO @@ -68,7 +74,7 @@ function handle_activity( $actor_slug, $activity ) { if ( is_wp_error( $activity ) ) { return $activity; } - return persist_activity( $actor_slug, $activity ); + return $res; } function forward_activity( $activity ) { diff --git a/inc/likes.php b/inc/likes.php index 92358d1..607f7d0 100644 --- a/inc/likes.php +++ b/inc/likes.php @@ -1,6 +1,9 @@ insert( diff --git a/inc/migrations.php b/inc/migrations.php index 924c6b5..5430d89 100644 --- a/inc/migrations.php +++ b/inc/migrations.php @@ -177,5 +177,19 @@ function migration_0_0_3() { ENGINE=InnoDB DEFAULT CHARSET=utf8; " ); + $wpdb->query( + " + CREATE TABLE pterotype_shares( + object_id INT UNSIGNED NOT NULL, + announce_id INT UNSIGNED NOT NULL, + PRIMARY KEY (object_id, shared_by), + FOREIGN KEY shares_object_fk(object_id) + REFERENCES pterotype_objects(id), + FOREIGN KEY shares_activity_fk(object_id) + REFERENCES pterotype_activities(id) + ) + ENGINE=InnoDB DEFAULT CHARSET=utf8; + " + ); } ?> diff --git a/inc/objects.php b/inc/objects.php index aae7f19..7126ec2 100644 --- a/inc/objects.php +++ b/inc/objects.php @@ -232,4 +232,15 @@ function make_tombstone( $object ) { ); return $tombstone; } + +function is_local_object( $object ) { + if ( array_key_exists( 'id', $object ) ) { + $parsed = parse_url( $object['id'] ); + if ( $parsed ) { + $site_host = parse_url( get_site_url() )['host']; + return $parsed['host'] === $site_host; + } + } + return false; +} ?> diff --git a/inc/shares.php b/inc/shares.php new file mode 100644 index 0000000..fce2530 --- /dev/null +++ b/inc/shares.php @@ -0,0 +1,15 @@ +insert( + 'pterotype_shares', + array( + 'object_id' => $object_id, + 'activity_id' => $activity_id, + ), + '%d' + ); +} +?>