Implement Follow (outbox) activity

This commit is contained in:
Jeremy Dormitzer 2018-09-04 00:01:34 -06:00
parent 21e6622fe5
commit 4de5612b8c
4 changed files with 56 additions and 0 deletions

29
following.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace following;
$PENDING = 'PENDING';
$FOLLOWING = 'FOLLOWING';
function request_follow( $actor_id, $object ) {
global $wpdb;
return $wpdb->insert(
'activitypub_following',
array( 'actor_id' => $actor_id, 'object' => wp_json_encode( $object ) )
);
}
function create_following_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS activitypub_following(
actor_id INT UNSIGNED NOT NULL,
object TEXT NOT NULL,
state VARCHAR(64) NOT NULL,
FOREIGN KEY actor_fk(actor_id)
REFERENCES activitypub_actors(id)
);
"
);
}
?>

23
inc/activities/follow.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace activities\follow;
require_once plugin_dir_path( __FILE__ ) . '/../following.php';
require_once plugin_dir_path( __FILE__ ) . '/../actors.php';
function handle( $actor, $activity ) {
if ( !array_key_exists( 'object', $activity ) ) {
return new \WP_Error(
'invalid_activity',
__( 'Expected an object', 'activitypub' ),
array( 'status' => 400 )
);
}
$object = $activity['object'];
$actor_id = \actors\get_actor_id( $actor );
$res = \following\request_follow( $actor_id, $object );
if ( is_wp_error( $res ) ) {
return $res;
}
return $activity;
}
?>

View File

@ -7,6 +7,7 @@ require_once plugin_dir_path( __FILE__ ) . '/objects.php';
require_once plugin_dir_path( __FILE__ ) . '/activities.php';
require_once plugin_dir_path( __FILE__ ) . '/actors.php';
require_once plugin_dir_path( __FILE__ ) . '/likes.php';
require_once plugin_dir_path( __FILE__ ) . '/following.php';
add_action( 'rest_api_init', function() {
\api\register_routes();
@ -24,5 +25,6 @@ add_action( 'activitypub_init', function() {
\actors\create_actors_table();
\actors\initialize_user_actors();
\likes\create_likes_table();
\following\create_following_table();
} );
?>

View File

@ -17,6 +17,7 @@ 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/like.php';
require_once plugin_dir_path( __FILE__ ) . '/activities/follow.php';
function handle_activity( $actor, $activity ) {
// TODO handle authentication/authorization
@ -38,6 +39,7 @@ function handle_activity( $actor, $activity ) {
$activity = \activities\delete\handle( $actor, $activity );
break;
case 'Follow':
$activity = \activities\follow\handle( $actor, $activity );
break;
case 'Add':
break;