Implement follow for inbox

This commit is contained in:
Jeremy Dormitzer 2018-09-20 19:10:15 -04:00
parent 9669b2c017
commit 1ee21d54dc
No known key found for this signature in database
GPG Key ID: 04F17C0F5A32C320
2 changed files with 42 additions and 6 deletions

View File

@ -4,22 +4,56 @@ namespace activities\follow;
require_once plugin_dir_path( __FILE__ ) . '/../following.php';
require_once plugin_dir_path( __FILE__ ) . '/../actors.php';
require_once plugin_dir_path( __FILE__ ) . '/../objects.php';
require_once plugin_dir_path( __FILE__ ) . '/../outbox.php';
function handle_outbox( $actor, $activity ) {
function handle_outbox( $actor_slug, $activity ) {
if ( !array_key_exists( 'object', $activity ) ) {
return new \WP_Error(
'invalid_activity',
__( 'Expected an object', 'activitypub' ),
__( 'Expected an object', 'pterotype' ),
array( 'status' => 400 )
);
}
$object = $activity['object'];
$object_row = \objects\upsert_object( $object );
$actor_id = \actors\get_actor_id( $actor );
$actor_id = \actors\get_actor_id( $actor_slug );
$res = \following\request_follow( $actor_id, $object_row->id );
if ( is_wp_error( $res ) ) {
return $res;
}
return $activity;
}
function handle_inbox( $actor_slug, $activity ) {
// For now, always Accept follow requests
// in the future, implement a UI to either accept or reject
// (or automatically accept if the user chooses to enable that setting)
$accept = make_accept( $actor_slug, $activity );
if ( is_wp_error( $accept ) ) {
return $accept;
}
$res = \outbox\handle_activity( $actor_slug, $accept );
if ( is_wp_error( $res ) ) {
return $res;
}
return $activity;
}
function make_accept( $actor_slug, $follow ) {
if ( !array_key_exists( 'actor', $follow ) ) {
return new \WP_Error(
'invalid_activity',
__( 'Activity must have an actor', 'pterotype' ),
array( 'status' => 400 )
);
}
$accept = array(
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Accept',
'actor' => \actors\get_actor_by_slug( $actor_slug ),
'object' => $follow,
'to' => $follow['actor'],
);
return $accept;
}
?>

View File

@ -13,6 +13,7 @@ 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';
require_once plugin_dir_path( __FILE__ ) . '/activities/follow.php';
function handle_activity( $actor_slug, $activity ) {
if ( !array_key_exists( 'type', $activity ) ) {
@ -25,15 +26,16 @@ function handle_activity( $actor_slug, $activity ) {
forward_activity( $activity );
switch ( $activity['type'] ) {
case 'Create':
$activity = \create\handle_inbox( $actor_slug, $activity );
$activity = \activities\create\handle_inbox( $actor_slug, $activity );
break;
case 'Update':
$activity = \update\handle_inbox( $actor_slug, $activity );
$activity = \activities\update\handle_inbox( $actor_slug, $activity );
break;
case 'Delete':
$activity = \delete\handle_inbox( $actor_slug, $activity );
$activity = \activities\delete\handle_inbox( $actor_slug, $activity );
break;
case 'Follow':
$activity = \activities\follow\handle_inbox( $actor_slug, $activity );
break;
case 'Accept':
break;