Implement update for inbox

This commit is contained in:
Jeremy Dormitzer 2018-09-20 18:42:41 -04:00
parent 02be20b2b3
commit 73b37d0aec
No known key found for this signature in database
GPG Key ID: 04F17C0F5A32C320
2 changed files with 60 additions and 4 deletions

View File

@ -3,18 +3,18 @@ namespace activities\update;
require_once plugin_dir_path( __FILE__ ) . '/../objects.php';
function handle_outbox( $actor, $activity ) {
function handle_outbox( $actor_slug, $activity ) {
if ( !(array_key_exists( 'type', $activity ) && $activity['type'] === 'Update') ) {
return new \WP_Error(
'invalid_activity',
__( 'Expecting an Update activity', 'activitypub' ),
__( 'Expecting an Update activity', 'pterotype' ),
array( 'status' => 400 )
);
}
if ( !array_key_exists( 'object', $activity ) ) {
return new \WP_Error(
'invalid_activity',
__( 'Expecting an object', 'activitypub' ),
__( 'Expecting an object', 'pterotype' ),
array( 'status' => 400 )
);
}
@ -22,7 +22,7 @@ function handle_outbox( $actor, $activity ) {
if ( !array_key_exists( 'id', $update_object ) ) {
return new \WP_Error(
'invalid_object',
__( 'Object must have an "id" parameter', 'activitypub' ),
__( 'Object must have an "id" parameter', 'pterotype' ),
array( 'status' => 400 )
);
}
@ -37,4 +37,59 @@ function handle_outbox( $actor, $activity ) {
}
return $activity;
}
function handle_inbox( $actor_slug, $activity ) {
if ( !(array_key_exists( 'type', $activity ) && $activity['type'] === 'Update') ) {
return new \WP_Error(
'invalid_activity',
__( 'Expecting an Update activity', 'pterotype' ),
array( 'status' => 400 )
);
}
if ( !array_key_exists( 'id', $activity ) ) {
return new \WP_Error(
'invalid_activity',
__( 'Activities must have an "id" field', 'pterotype' ),
array( 'status' => 400 )
);
}
if ( !array_key_exists( 'object', $activity ) ) {
return new \WP_Error(
'invalid_activity',
__( 'Expecting an object', 'pterotype' ),
array( 'status' => 400 )
);
}
$object = $activity['object'];
if ( !array_key_exists( 'id', $object ) ) {
return new \WP_Error(
'invalid_activity',
__( 'Objects must have an "id" field', 'pterotype' ),
array( 'status' => 400 )
);
}
$authorized = check_authorization( $activity );
if ( is_wp_error( $authorized ) ) {
return $authorized;
}
$object = \objects\upsert_object( $object );
if ( is_wp_error( $object ) ) {
return $object;
}
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

@ -26,6 +26,7 @@ function handle_activity( $actor_slug, $activity ) {
$activity = \create\handle_inbox( $actor_slug, $activity );
break;
case 'Update':
$activity = \update\handle_inbox( $actor_slug, $activity );
break;
case 'Delete':
break;