Implement delete for inbox

This commit is contained in:
Jeremy Dormitzer 2018-09-20 18:48:40 -04:00
parent 73b37d0aec
commit 9669b2c017
No known key found for this signature in database
GPG Key ID: 04F17C0F5A32C320
2 changed files with 53 additions and 2 deletions

View File

@ -7,8 +7,8 @@ function handle_outbox( $actor, $activity ) {
if ( !array_key_exists( 'object', $activity ) ) {
return new \WP_Error(
'invalid_activity',
__( 'Expected an object', 'activitypub' ),
array( 'status' => 40 )
__( 'Expected an object', 'pterotype' ),
array( 'status' => 400 )
);
}
$object = $activity['object'];
@ -18,4 +18,52 @@ 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 )
);
}
if ( !array_key_exists( 'id', $activity ) ) {
return new \WP_Error(
'invalid_activity',
__( 'Expected an id', 'pterotype' ),
array( 'status' => 400 )
);
}
$object = $activity['object'];
if ( !array_key_exists( 'id', $object ) ) {
return new \WP_Error(
'invalid_activity',
__( 'Expected an id', 'pterotype' ),
array( 'status' => 400 )
);
}
$authorized = check_authorization( $activity );
if ( is_wp_error( $authorized ) ) {
return $authorized;
}
$res = \objects\delete_object( $object );
if ( is_wp_error( $res ) ) {
return $res;
}
return $activity;
}
function check_authorization( $activity ) {
$object = $activity['object'];
$activity_origin = parse_url( $activity['id'] )['host'];
$object_origin = parse_url( $object['id'] )['host'];
if ( ( !$activity_origin || !$object_origin ) || $activity_origin !== $object_origin ) {
return new \WP_Error(
'unauthorized',
__( 'Unauthorized Update activity', 'pterotype' ),
array( 'status' => 403 )
);
}
return true;
}
?>

View File

@ -11,6 +11,8 @@ namespace inbox;
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';
function handle_activity( $actor_slug, $activity ) {
if ( !array_key_exists( 'type', $activity ) ) {
@ -29,6 +31,7 @@ function handle_activity( $actor_slug, $activity ) {
$activity = \update\handle_inbox( $actor_slug, $activity );
break;
case 'Delete':
$activity = \delete\handle_inbox( $actor_slug, $activity );
break;
case 'Follow':
break;