2020-04-07 13:03:04 +00:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
use WP_Error;
|
|
|
|
use WP_REST_Server;
|
|
|
|
use WP_REST_Response;
|
|
|
|
use Activitypub\Activity\Activity;
|
|
|
|
use Activitypub\Collection\Users as User_Collection;
|
|
|
|
|
|
|
|
use function Activitypub\get_context;
|
|
|
|
use function Activitypub\url_to_authorid;
|
|
|
|
use function Activitypub\get_rest_url_by_path;
|
|
|
|
use function Activitypub\get_remote_metadata_by_actor;
|
2023-12-08 23:23:11 +00:00
|
|
|
use function Activitypub\extract_recipients_from_activity;
|
2023-10-22 22:20:53 +00:00
|
|
|
|
2020-04-07 13:03:04 +00:00
|
|
|
/**
|
|
|
|
* ActivityPub Inbox REST-Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/#inbox
|
|
|
|
*/
|
|
|
|
class Inbox {
|
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
|
|
|
public static function init() {
|
2023-10-22 22:20:53 +00:00
|
|
|
self::register_routes();
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register routes
|
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
|
|
|
\register_rest_route(
|
2023-10-22 22:20:53 +00:00
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2022-08-19 16:15:51 +00:00
|
|
|
'/inbox',
|
|
|
|
array(
|
2020-04-07 13:03:04 +00:00
|
|
|
array(
|
2023-10-22 22:20:53 +00:00
|
|
|
'methods' => WP_REST_Server::CREATABLE,
|
|
|
|
'callback' => array( self::class, 'shared_inbox_post' ),
|
2023-01-18 16:39:54 +00:00
|
|
|
'args' => self::shared_inbox_post_parameters(),
|
2020-12-25 19:23:08 +00:00
|
|
|
'permission_callback' => '__return_true',
|
2020-04-07 13:03:04 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
\register_rest_route(
|
2023-10-22 22:20:53 +00:00
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
|
|
|
'/users/(?P<user_id>[\w\-\.]+)/inbox',
|
2022-08-19 16:15:51 +00:00
|
|
|
array(
|
2020-04-07 13:03:04 +00:00
|
|
|
array(
|
2023-10-22 22:20:53 +00:00
|
|
|
'methods' => WP_REST_Server::CREATABLE,
|
|
|
|
'callback' => array( self::class, 'user_inbox_post' ),
|
2023-01-18 16:39:54 +00:00
|
|
|
'args' => self::user_inbox_post_parameters(),
|
2020-12-25 19:23:08 +00:00
|
|
|
'permission_callback' => '__return_true',
|
2020-04-07 13:03:04 +00:00
|
|
|
),
|
2021-07-25 23:24:56 +00:00
|
|
|
array(
|
2023-10-22 22:20:53 +00:00
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( self::class, 'user_inbox_get' ),
|
2023-01-18 16:39:54 +00:00
|
|
|
'args' => self::user_inbox_get_parameters(),
|
2021-07-25 23:24:56 +00:00
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
2020-04-07 13:03:04 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the user-inbox
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
2021-07-25 23:24:56 +00:00
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
2021-07-27 21:21:10 +00:00
|
|
|
public static function user_inbox_get( $request ) {
|
|
|
|
$user_id = $request->get_param( 'user_id' );
|
2023-10-22 22:20:53 +00:00
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2021-07-25 23:24:56 +00:00
|
|
|
$page = $request->get_param( 'page', 0 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Action triggerd prior to the ActivityPub profile being created and sent to the client
|
|
|
|
*/
|
2023-10-22 22:20:53 +00:00
|
|
|
\do_action( 'activitypub_rest_inbox_pre' );
|
2021-07-25 23:24:56 +00:00
|
|
|
|
|
|
|
$json = new \stdClass();
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
$json->{'@context'} = get_context();
|
|
|
|
$json->id = get_rest_url_by_path( sprintf( 'users/%d/inbox', $user->get__id() ) );
|
2021-07-25 23:24:56 +00:00
|
|
|
$json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' );
|
|
|
|
$json->type = 'OrderedCollectionPage';
|
2023-10-22 22:20:53 +00:00
|
|
|
$json->partOf = get_rest_url_by_path( sprintf( 'users/%d/inbox', $user->get__id() ) ); // phpcs:ignore
|
2021-07-25 23:24:56 +00:00
|
|
|
|
|
|
|
$json->totalItems = 0; // phpcs:ignore
|
|
|
|
|
|
|
|
$json->orderedItems = array(); // phpcs:ignore
|
|
|
|
|
|
|
|
$json->first = $json->partOf; // phpcs:ignore
|
|
|
|
|
|
|
|
// filter output
|
2023-10-22 22:20:53 +00:00
|
|
|
$json = \apply_filters( 'activitypub_rest_inbox_array', $json );
|
2021-07-25 23:24:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Action triggerd after the ActivityPub profile has been created and sent to the client
|
|
|
|
*/
|
|
|
|
\do_action( 'activitypub_inbox_post' );
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
$rest_response = new WP_REST_Response( $json, 200 );
|
|
|
|
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
|
2021-07-25 23:24:56 +00:00
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
return $rest_response;
|
2021-07-25 23:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles user-inbox requests
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
2020-04-07 13:03:04 +00:00
|
|
|
*
|
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
2021-07-25 23:24:56 +00:00
|
|
|
public static function user_inbox_post( $request ) {
|
2020-04-07 13:03:04 +00:00
|
|
|
$user_id = $request->get_param( 'user_id' );
|
2023-10-22 22:20:53 +00:00
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
|
|
|
}
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
$data = $request->get_json_params();
|
|
|
|
$activity = Activity::init_from_array( $data );
|
|
|
|
$type = $request->get_param( 'type' );
|
|
|
|
$type = \strtolower( $type );
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
\do_action( 'activitypub_inbox', $data, $user->get__id(), $type, $activity );
|
|
|
|
\do_action( "activitypub_inbox_{$type}", $data, $user->get__id(), $activity );
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
$rest_response = new WP_REST_Response( array(), 202 );
|
|
|
|
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
|
|
|
|
|
|
|
|
return $rest_response;
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The shared inbox
|
|
|
|
*
|
2020-12-25 19:23:08 +00:00
|
|
|
* @param WP_REST_Request $request
|
2020-04-07 13:03:04 +00:00
|
|
|
*
|
2020-12-25 19:23:08 +00:00
|
|
|
* @return WP_REST_Response
|
2020-04-07 13:03:04 +00:00
|
|
|
*/
|
2021-07-25 23:24:56 +00:00
|
|
|
public static function shared_inbox_post( $request ) {
|
2023-12-08 23:23:11 +00:00
|
|
|
$data = $request->get_json_params();
|
|
|
|
$activity = Activity::init_from_array( $data );
|
|
|
|
$type = $request->get_param( 'type' );
|
|
|
|
$users = self::get_recipients( $data );
|
2021-07-25 23:24:56 +00:00
|
|
|
|
|
|
|
if ( ! $users ) {
|
2023-10-22 22:20:53 +00:00
|
|
|
return new WP_Error(
|
2022-08-19 16:15:51 +00:00
|
|
|
'rest_invalid_param',
|
|
|
|
\__( 'No recipients found', 'activitypub' ),
|
|
|
|
array(
|
2023-12-08 23:23:11 +00:00
|
|
|
'status' => 400,
|
2022-08-19 16:15:51 +00:00
|
|
|
'params' => array(
|
|
|
|
'to' => \__( 'Please check/validate "to" field', 'activitypub' ),
|
|
|
|
'bto' => \__( 'Please check/validate "bto" field', 'activitypub' ),
|
|
|
|
'cc' => \__( 'Please check/validate "cc" field', 'activitypub' ),
|
|
|
|
'bcc' => \__( 'Please check/validate "bcc" field', 'activitypub' ),
|
|
|
|
'audience' => \__( 'Please check/validate "audience" field', 'activitypub' ),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2021-07-25 23:24:56 +00:00
|
|
|
}
|
2020-12-25 19:23:08 +00:00
|
|
|
|
|
|
|
foreach ( $users as $user ) {
|
2023-10-22 22:20:53 +00:00
|
|
|
$user = User_Collection::get_by_various( $user );
|
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-20 13:41:52 +00:00
|
|
|
$type = \strtolower( $type );
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
\do_action( 'activitypub_inbox', $data, $user->ID, $type, $activity );
|
|
|
|
\do_action( "activitypub_inbox_{$type}", $data, $user->ID, $activity );
|
2020-12-25 19:23:08 +00:00
|
|
|
}
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
$rest_response = new WP_REST_Response( array(), 202 );
|
|
|
|
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
|
|
|
|
|
|
|
|
return $rest_response;
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
2023-01-18 16:39:54 +00:00
|
|
|
public static function user_inbox_get_parameters() {
|
2020-04-07 13:03:04 +00:00
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['page'] = array(
|
|
|
|
'type' => 'integer',
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['user_id'] = array(
|
|
|
|
'required' => true,
|
2023-10-22 22:20:53 +00:00
|
|
|
'type' => 'string',
|
2023-01-18 16:39:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
|
|
|
public static function user_inbox_post_parameters() {
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['page'] = array(
|
|
|
|
'type' => 'integer',
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['user_id'] = array(
|
|
|
|
'required' => true,
|
2023-10-22 22:20:53 +00:00
|
|
|
'type' => 'string',
|
2020-04-07 13:03:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$params['id'] = array(
|
|
|
|
'required' => true,
|
2020-12-25 19:23:08 +00:00
|
|
|
'sanitize_callback' => 'esc_url_raw',
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['actor'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'sanitize_callback' => function( $param, $request, $key ) {
|
2023-12-08 23:23:11 +00:00
|
|
|
if ( \is_array( $param ) ) {
|
|
|
|
if ( isset( $param['id'] ) ) {
|
|
|
|
$param = $param['id'];
|
|
|
|
} else {
|
|
|
|
$param = $param['url'];
|
|
|
|
}
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
2020-12-25 19:23:08 +00:00
|
|
|
return \esc_url_raw( $param );
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['type'] = array(
|
|
|
|
'required' => true,
|
|
|
|
//'type' => 'enum',
|
|
|
|
//'enum' => array( 'Create' ),
|
2021-12-20 13:41:52 +00:00
|
|
|
//'sanitize_callback' => function( $param, $request, $key ) {
|
2023-10-22 22:20:53 +00:00
|
|
|
// return \strtolower( $param );
|
2021-12-20 13:41:52 +00:00
|
|
|
//},
|
2020-12-25 19:23:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$params['object'] = array(
|
|
|
|
'required' => true,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
2023-01-18 16:39:54 +00:00
|
|
|
public static function shared_inbox_post_parameters() {
|
2020-12-25 19:23:08 +00:00
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['page'] = array(
|
|
|
|
'type' => 'integer',
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['id'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'type' => 'string',
|
2020-04-07 13:03:04 +00:00
|
|
|
'sanitize_callback' => 'esc_url_raw',
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['actor'] = array(
|
|
|
|
'required' => true,
|
2020-05-04 15:57:04 +00:00
|
|
|
//'type' => array( 'object', 'string' ),
|
2020-04-07 13:03:04 +00:00
|
|
|
'sanitize_callback' => function( $param, $request, $key ) {
|
|
|
|
if ( ! \is_string( $param ) ) {
|
|
|
|
$param = $param['id'];
|
|
|
|
}
|
|
|
|
return \esc_url_raw( $param );
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['type'] = array(
|
|
|
|
'required' => true,
|
2020-05-04 15:57:04 +00:00
|
|
|
//'type' => 'enum',
|
|
|
|
//'enum' => array( 'Create' ),
|
2021-12-20 13:41:52 +00:00
|
|
|
//'sanitize_callback' => function( $param, $request, $key ) {
|
2023-10-22 22:20:53 +00:00
|
|
|
// return \strtolower( $param );
|
2021-12-20 13:41:52 +00:00
|
|
|
//},
|
2020-04-07 13:03:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$params['object'] = array(
|
|
|
|
'required' => true,
|
2020-05-04 15:57:04 +00:00
|
|
|
//'type' => 'object',
|
2020-04-07 13:03:04 +00:00
|
|
|
);
|
|
|
|
|
2020-12-25 19:23:08 +00:00
|
|
|
$params['to'] = array(
|
2021-07-25 23:24:56 +00:00
|
|
|
'required' => false,
|
2020-12-25 19:23:08 +00:00
|
|
|
'sanitize_callback' => function( $param, $request, $key ) {
|
|
|
|
if ( \is_string( $param ) ) {
|
|
|
|
$param = array( $param );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $param;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['cc'] = array(
|
|
|
|
'sanitize_callback' => function( $param, $request, $key ) {
|
|
|
|
if ( \is_string( $param ) ) {
|
|
|
|
$param = array( $param );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $param;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
$params['bcc'] = array(
|
|
|
|
'sanitize_callback' => function( $param, $request, $key ) {
|
|
|
|
if ( \is_string( $param ) ) {
|
|
|
|
$param = array( $param );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $param;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-04-07 13:03:04 +00:00
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2023-01-18 16:39:54 +00:00
|
|
|
/**
|
|
|
|
* Get local user recipients
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return array The list of local users
|
|
|
|
*/
|
|
|
|
public static function get_recipients( $data ) {
|
2023-12-08 23:23:11 +00:00
|
|
|
$recipients = extract_recipients_from_activity( $data );
|
2023-01-18 16:39:54 +00:00
|
|
|
$users = array();
|
2021-07-25 23:24:56 +00:00
|
|
|
|
|
|
|
foreach ( $recipients as $recipient ) {
|
2023-10-22 22:20:53 +00:00
|
|
|
$user_id = url_to_authorid( $recipient );
|
2021-07-25 23:24:56 +00:00
|
|
|
|
|
|
|
$user = get_user_by( 'id', $user_id );
|
|
|
|
|
|
|
|
if ( $user ) {
|
|
|
|
$users[] = $user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $users;
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
}
|