pterotype/inc/outbox.php

99 lines
3.0 KiB
PHP
Raw Normal View History

2018-08-21 13:17:07 +00:00
<?php
2018-08-22 12:33:21 +00:00
/*
When an Activity is received (i.e. POSTed) to an Actor's outbox, the server must:
2018-08-21 13:17:07 +00:00
2018-08-22 12:34:28 +00:00
0. Make sure the request is authenticated
2018-08-22 12:33:21 +00:00
1. Add the Activity to the Actor's outbox collection in the DB
2. Deliver the Activity to the appropriate inboxes based on the received Activity
This involves discovering all the inboxes, including nested ones if the target
is a collection, deduplicating inboxes, and the POSTing the Activity to each
target inbox.
3. Perform side effects as necessary
*/
namespace outbox;
2018-08-29 20:38:10 +00:00
require_once plugin_dir_path( __FILE__ ) . '/activities.php';
2018-08-29 21:19:10 +00:00
require_once plugin_dir_path( __FILE__ ) . '/activities/create.php';
require_once plugin_dir_path( __FILE__ ) . '/activities/update.php';
2018-09-01 14:14:11 +00:00
require_once plugin_dir_path( __FILE__ ) . '/activities/delete.php';
require_once plugin_dir_path( __FILE__ ) . '/activities/like.php';
2018-08-25 13:31:20 +00:00
function handle_activity( $actor, $activity ) {
2018-08-29 21:19:00 +00:00
// TODO handle authentication/authorization
2018-08-25 13:31:20 +00:00
if ( !array_key_exists( "type", $activity ) ) {
2018-08-29 20:38:10 +00:00
return new \WP_Error(
2018-08-27 22:36:18 +00:00
'invalid_activity',
__( 'Invalid activity', 'activitypub' ),
array( 'status' => 400 )
2018-08-25 13:31:20 +00:00
);
}
2018-08-29 20:38:10 +00:00
switch ( $activity['type'] ) {
case 'Create':
2018-08-29 21:04:24 +00:00
$activity = \activities\create\handle( $actor, $activity );
2018-08-25 13:31:20 +00:00
break;
2018-08-29 20:38:10 +00:00
case 'Update':
2018-08-29 21:19:10 +00:00
$activity = \activities\update\handle( $actor, $activity );
2018-08-25 13:31:20 +00:00
break;
2018-08-29 20:38:10 +00:00
case 'Delete':
2018-09-01 14:14:11 +00:00
$activity = \activities\delete\handle( $actor, $activity );
2018-08-25 13:31:20 +00:00
break;
2018-08-29 20:38:10 +00:00
case 'Follow':
2018-08-25 13:31:20 +00:00
break;
2018-08-29 20:38:10 +00:00
case 'Add':
2018-08-25 13:31:20 +00:00
break;
2018-08-29 20:38:10 +00:00
case 'Remove':
2018-08-25 13:31:20 +00:00
break;
2018-08-29 20:38:10 +00:00
case 'Like':
$activity = \activities\like\handle( $actor, $activity );
2018-08-25 13:31:20 +00:00
break;
2018-08-29 20:38:10 +00:00
case 'Block':
2018-08-25 13:31:20 +00:00
break;
2018-08-29 20:38:10 +00:00
case 'Undo':
2018-08-25 13:31:20 +00:00
break;
2018-08-27 22:36:18 +00:00
default:
// handle wrapping object in Create activity
break;
2018-08-25 13:31:20 +00:00
}
2018-08-28 22:07:04 +00:00
if ( is_wp_error( $activity ) ) {
return $activity;
} else {
deliver_activity( $activity );
return persist_activity( $actor, $activity );
}
}
function deliver_activity( $activity ) {
// TODO
}
function persist_activity( $actor, $activity ) {
global $wpdb;
2018-08-29 20:38:10 +00:00
$activity = \activities\persist_activity( $activity );
$activity_id = $wpdb->insert_id;
2018-08-28 22:07:04 +00:00
$wpdb->insert( 'activitypub_outbox',
array(
2018-08-29 20:38:10 +00:00
'actor' => $actor,
'activity_id' => $activity_id,
2018-08-28 22:07:04 +00:00
) );
2018-08-29 21:04:24 +00:00
$response = new \WP_REST_Response();
2018-08-28 22:07:04 +00:00
$response->set_status( 201 );
2018-08-29 20:38:10 +00:00
$response->header( 'Location', $activity['id'] );
2018-08-28 22:07:04 +00:00
return $response;
}
function create_outbox_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS activitypub_outbox (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
actor VARCHAR(128) NOT NULL,
2018-08-29 21:04:24 +00:00
activity_id INT UNSIGNED NOT NULL,
FOREIGN KEY activity_fk(activity_id)
REFERENCES activitypub_activities(id)
2018-08-28 22:07:04 +00:00
);
"
);
2018-08-23 02:39:59 +00:00
}
2018-08-21 13:17:07 +00:00
?>