Implement delete

This commit is contained in:
Jeremy Dormitzer 2018-09-01 10:14:11 -04:00
parent 8258d7af5e
commit 63a0328648
3 changed files with 42 additions and 1 deletions

19
inc/activities/delete.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace delete;
require_once plugin_dir_path( __FILE__ ) . '/../objects.php';
function handle( $actor, $activity ) {
if ( !array_key_exists( 'object', $activity ) ) {
return new \WP_Error(
'invalid_activity', __( 'Expected an object', 'activitypub' )
);
}
$object = $activity['object'];
$res = \objects\delete_object( $object );
if ( is_wp_error( $res ) ) {
return $res;
}
return $activity;
}
?>

View File

@ -1,6 +1,9 @@
<?php
namespace objects;
// TODO for 'post' objects, store a post id instead of the full post text,
// then hydrate the text on read
function create_object( $object ) {
global $wpdb;
$res = $wpdb->insert(
@ -46,7 +49,7 @@ function get_object( $id ) {
) );
if ( is_null( $object_json ) ) {
return new \WP_Error(
'not_found', __( 'Object not found', 'activitypub' ), array ( 'status' => 404 )
'not_found', __( 'Object not found', 'activitypub' ), array( 'status' => 404 )
);
}
$object = json_decode( $object_json, true );
@ -54,6 +57,23 @@ function get_object( $id ) {
return $object;
}
function delete_object( $object ) {
global $wpdb;
if ( !array_key_exists( 'id', $object ) ) {
return new \WP_Error(
'invalid_object',
__( 'Object must have an "id" parameter', 'activitypub' ),
array( 'status' => 400 )
);
}
$id = get_id_from_url( $object['id'] );
$res = $wpdb->delete( 'activitypub_objects', array( 'id' => $id ), '%d' );
if ( !$res ) {
return new \WP_Error( 'db_error', __( 'Error deleting object', 'activitypub' ) );
}
return $res;
}
function get_id_from_url( $url ) {
global $wpdb;
$matches = array();

View File

@ -15,6 +15,7 @@ namespace outbox;
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, $activity ) {
// TODO handle authentication/authorization
@ -33,6 +34,7 @@ function handle_activity( $actor, $activity ) {
$activity = \activities\update\handle( $actor, $activity );
break;
case 'Delete':
$activity = \activities\delete\handle( $actor, $activity );
break;
case 'Follow':
break;