Implement announce for inbox

This commit is contained in:
Jeremy Dormitzer 2018-09-23 22:22:30 -04:00
parent 124bf8b4ac
commit a29e86ee16
7 changed files with 102 additions and 1 deletions

View File

@ -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'] );

View File

@ -0,0 +1,45 @@
<?php
namespace activities\announce;
require_once plugin_dir_path( __FILE__ ) . '/../objects.php';
require_once plugin_dir_path( __FILE__ ) . '/../shares.php';
function handle_inbox( $actor_slug, $activity ) {
if ( !array_key_exists( 'object', $activity ) ) {
return new \WP_Error(
'invalid_activity',
__( 'Expected an "object" field', 'pterotype' ),
array( 'status' => 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;
}
?>

View File

@ -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 ) {

View File

@ -1,6 +1,9 @@
<?php
namespace likes;
// TODO implement a 'likes' collection for objects -
// implemented similar to/same as 'shares' collection
function create_like( $actor_id, $object_id ) {
global $wpdb;
return $wpdb->insert(

View File

@ -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;
"
);
}
?>

View File

@ -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;
}
?>

15
inc/shares.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace shares;
function add_share( $object_id, $activity_id ) {
global $wpdb;
return $wpdb->insert(
'pterotype_shares',
array(
'object_id' => $object_id,
'activity_id' => $activity_id,
),
'%d'
);
}
?>