pterotype/inc/inbox.php

80 lines
2.4 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 inbox, the server must:
2018-08-21 13:17:07 +00:00
2018-08-22 12:33:21 +00:00
1. Forward the Activity as necessary
See (https://www.w3.org/TR/activitypub/#x7-1-2-forwarding-from-inbox).
2. Perform the side effects of receiving the Activity
2018-09-15 21:47:13 +00:00
3. Persist the activity in the actor's inbox (and the attached object, if necessary)
2018-08-22 12:33:21 +00:00
*/
2018-09-15 21:47:13 +00:00
namespace inbox;
2018-09-19 22:08:16 +00:00
require_once plugin_dir_path( __FILE__ ) . '/activities.php';
require_once plugin_dir_path( __FILE__ ) . '/activities/create.php';
2018-09-20 22:48:40 +00:00
require_once plugin_dir_path( __FILE__ ) . '/activities/update.php';
require_once plugin_dir_path( __FILE__ ) . '/activities/delete.php';
2018-09-20 23:10:15 +00:00
require_once plugin_dir_path( __FILE__ ) . '/activities/follow.php';
2018-09-19 22:08:16 +00:00
function handle_activity( $actor_slug, $activity ) {
2018-09-15 21:47:13 +00:00
if ( !array_key_exists( 'type', $activity ) ) {
return new \WP_Error(
'invalid_activity',
2018-09-19 15:16:41 +00:00
__( 'Activity must have a type', 'pterotype' ),
2018-09-15 21:47:13 +00:00
array( 'status' => 400 )
);
}
forward_activity( $activity );
switch ( $activity['type'] ) {
case 'Create':
2018-09-20 23:10:15 +00:00
$activity = \activities\create\handle_inbox( $actor_slug, $activity );
2018-09-15 21:47:13 +00:00
break;
case 'Update':
2018-09-20 23:10:15 +00:00
$activity = \activities\update\handle_inbox( $actor_slug, $activity );
2018-09-15 21:47:13 +00:00
break;
case 'Delete':
2018-09-20 23:10:15 +00:00
$activity = \activities\delete\handle_inbox( $actor_slug, $activity );
2018-09-15 21:47:13 +00:00
break;
case 'Follow':
2018-09-20 23:10:15 +00:00
$activity = \activities\follow\handle_inbox( $actor_slug, $activity );
2018-09-15 21:47:13 +00:00
break;
case 'Accept':
$activity = \activities\accept\handle_inbox( $actor_slug, $activity );
2018-09-15 21:47:13 +00:00
break;
case 'Reject':
break;
case 'Add':
break;
case 'Remove':
break;
case 'Announce':
break;
case 'Undo':
break;
}
if ( is_wp_error( $activity ) ) {
return $activity;
}
2018-09-19 22:08:16 +00:00
return persist_activity( $actor_slug, $activity );
2018-09-15 21:47:13 +00:00
}
function forward_activity( $activity ) {
2018-09-19 22:08:16 +00:00
// TODO
2018-09-15 21:47:13 +00:00
}
2018-09-19 22:08:16 +00:00
function persist_activity( $actory_slug, $activity ) {
2018-09-15 21:47:13 +00:00
global $wpdb;
2018-09-19 22:08:16 +00:00
$activity = \activities\persist_activity( $activity );
if ( is_wp_error( $activity ) ) {
return $activity;
}
$activity_id = $wpdb->insert_id;
$actor_id = \actors\get_actor_id( $actor_slug );
$wpdb->insert( 'pterotype_inbox', array(
'actor_id' => $actor_id,
'activity_id' => $activity_id,
) );
$response = new \WP_Rest_Response();
return $response;
2018-09-15 21:47:13 +00:00
}
2018-08-21 13:17:07 +00:00
?>