wordpress-activitypub/includes/rest/class-inbox.php

337 lines
8.5 KiB
PHP
Raw Normal View History

2018-09-27 20:28:49 +00:00
<?php
/**
* Inbox REST-Class file.
*
* @package Activitypub
*/
namespace Activitypub\Rest;
2023-05-19 10:00:11 +00:00
use WP_REST_Server;
2023-04-21 06:51:38 +00:00
use WP_REST_Response;
2023-07-03 15:59:42 +00:00
use Activitypub\Activity\Activity;
2023-07-03 17:56:06 +00:00
use Activitypub\Collection\Users as User_Collection;
2023-04-20 13:22:11 +00:00
2023-05-19 10:00:11 +00:00
use function Activitypub\get_context;
use function Activitypub\url_to_authorid;
2023-05-12 20:31:53 +00:00
use function Activitypub\get_rest_url_by_path;
2024-04-05 20:49:38 +00:00
use function Activitypub\get_masked_wp_version;
use function Activitypub\extract_recipients_from_activity;
2023-04-20 13:22:11 +00:00
2018-09-27 20:28:49 +00:00
/**
* ActivityPub Inbox REST-Class.
2018-09-27 20:28:49 +00:00
*
* @author Matthias Pfefferle
2019-02-24 12:01:28 +00:00
*
* @see https://www.w3.org/TR/activitypub/#inbox
2018-09-27 20:28:49 +00:00
*/
class Inbox {
2019-02-24 12:01:28 +00:00
/**
* Initialize the class, registering WordPress hooks.
2019-02-24 12:01:28 +00:00
*/
public static function init() {
self::register_routes();
}
2019-02-28 18:31:55 +00:00
2018-09-27 20:28:49 +00:00
/**
* Register routes.
2018-09-27 20:28:49 +00:00
*/
public static function register_routes() {
2019-09-27 08:12:59 +00:00
\register_rest_route(
ACTIVITYPUB_REST_NAMESPACE,
2022-01-27 12:09:11 +00:00
'/inbox',
array(
2018-09-27 20:28:49 +00:00
array(
2023-10-12 09:00:58 +00:00
'methods' => WP_REST_Server::CREATABLE,
2023-04-20 13:22:11 +00:00
'callback' => array( self::class, 'shared_inbox_post' ),
'args' => self::shared_inbox_post_parameters(),
2020-09-18 14:36:09 +00:00
'permission_callback' => '__return_true',
2018-09-27 20:28:49 +00:00
),
)
);
2019-09-27 08:12:59 +00:00
\register_rest_route(
ACTIVITYPUB_REST_NAMESPACE,
'/(users|actors)/(?P<user_id>[\w\-\.]+)/inbox',
2022-01-27 12:09:11 +00:00
array(
2018-09-27 20:28:49 +00:00
array(
2023-10-12 09:00:58 +00:00
'methods' => WP_REST_Server::CREATABLE,
2023-04-20 13:22:11 +00:00
'callback' => array( self::class, 'user_inbox_post' ),
'args' => self::user_inbox_post_parameters(),
2020-09-18 14:36:09 +00:00
'permission_callback' => '__return_true',
2018-09-27 20:28:49 +00:00
),
array(
2023-05-19 10:00:11 +00:00
'methods' => WP_REST_Server::READABLE,
2023-04-20 13:22:11 +00:00
'callback' => array( self::class, 'user_inbox_get' ),
'args' => self::user_inbox_get_parameters(),
'permission_callback' => '__return_true',
),
2018-09-27 20:28:49 +00:00
)
);
}
/**
* Renders the user-inbox.
*
* @param \WP_REST_Request $request The request object.
* @return WP_REST_Response|\WP_Error The response object or WP_Error.
*/
public static function user_inbox_get( $request ) {
$user_id = $request->get_param( 'user_id' );
2023-07-03 17:56:06 +00:00
$user = User_Collection::get_by_various( $user_id );
if ( is_wp_error( $user ) ) {
return $user;
}
/**
* Action triggered prior to the ActivityPub profile being created and sent to the client.
*/
2023-07-25 08:47:59 +00:00
\do_action( 'activitypub_rest_inbox_pre' );
$json = new \stdClass();
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
2023-05-19 10:00:11 +00:00
$json->{'@context'} = get_context();
$json->id = get_rest_url_by_path( sprintf( 'actors/%d/inbox', $user->get__id() ) );
$json->generator = 'http://wordpress.org/?v=' . get_masked_wp_version();
$json->type = 'OrderedCollectionPage';
$json->partOf = get_rest_url_by_path( sprintf( 'actors/%d/inbox', $user->get__id() ) );
$json->totalItems = 0;
$json->orderedItems = array();
$json->first = $json->partOf;
// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
/**
* Filter the ActivityPub inbox array.
*
* @param array $json The ActivityPub inbox array.
*/
$json = \apply_filters( 'activitypub_rest_inbox_array', $json );
/**
* Action triggered after the ActivityPub profile has been created and sent to the client.
*/
\do_action( 'activitypub_inbox_post' );
$rest_response = new WP_REST_Response( $json, 200 );
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
return $rest_response;
}
/**
* Handles user-inbox requests.
*
* @param \WP_REST_Request $request The request object.
2019-02-28 18:31:55 +00:00
*
* @return WP_REST_Response|\WP_Error The response object or WP_Error.
*/
public static function user_inbox_post( $request ) {
$user_id = $request->get_param( 'user_id' );
2023-07-05 13:33:07 +00:00
$user = User_Collection::get_by_various( $user_id );
if ( is_wp_error( $user ) ) {
return $user;
}
2019-11-27 07:25:04 +00:00
$data = $request->get_json_params();
$activity = Activity::init_from_array( $data );
$type = $request->get_param( 'type' );
$type = \strtolower( $type );
2018-10-02 20:16:47 +00:00
/**
* ActivityPub inbox action.
*
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param string $type The type of the activity.
* @param Activity $activity The Activity object.
*/
\do_action( 'activitypub_inbox', $data, $user->get__id(), $type, $activity );
/**
* ActivityPub inbox action for specific activity types.
*
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param Activity $activity The Activity object.
*/
\do_action( "activitypub_inbox_{$type}", $data, $user->get__id(), $activity );
$rest_response = new WP_REST_Response( array(), 202 );
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
return $rest_response;
2018-09-27 20:28:49 +00:00
}
2018-09-30 20:51:22 +00:00
/**
* The shared inbox.
2018-09-30 20:51:22 +00:00
*
* @param \WP_REST_Request $request The request object.
2018-09-30 20:51:22 +00:00
*
* @return WP_REST_Response
2018-09-30 20:51:22 +00:00
*/
public static function shared_inbox_post( $request ) {
$data = $request->get_json_params();
$activity = Activity::init_from_array( $data );
$type = $request->get_param( 'type' );
$type = \strtolower( $type );
2021-11-17 20:11:34 +00:00
/**
* ActivityPub inbox action.
*
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param string $type The type of the activity.
* @param Activity $activity The Activity object.
*/
\do_action( 'activitypub_inbox', $data, null, $type, $activity );
/**
* ActivityPub inbox action for specific activity types.
*
* @param array $data The data array.
* @param int|null $user_id The user ID.
* @param Activity $activity The Activity object.
*/
\do_action( "activitypub_inbox_{$type}", $data, null, $activity );
2019-11-18 19:57:00 +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;
2018-09-27 20:28:49 +00:00
}
2018-09-30 20:51:22 +00:00
/**
* The supported parameters.
2018-09-30 20:51:22 +00:00
*
* @return array List of parameters.
2018-09-30 20:51:22 +00:00
*/
public static function user_inbox_get_parameters() {
2018-09-27 20:28:49 +00:00
$params = array();
$params['page'] = array(
'type' => 'integer',
);
$params['user_id'] = array(
2018-09-27 20:28:49 +00:00
'required' => true,
'type' => 'string',
);
return $params;
}
/**
* The supported parameters.
*
* @return array List of parameters.
*/
public static function user_inbox_post_parameters() {
2018-09-27 20:28:49 +00:00
$params = array();
$params['user_id'] = array(
2018-09-27 20:28:49 +00:00
'required' => true,
'type' => 'string',
2018-09-27 20:28:49 +00:00
);
$params['id'] = array(
'required' => true,
'sanitize_callback' => 'esc_url_raw',
2023-04-15 05:53:43 +00:00
);
$params['actor'] = array(
'required' => true,
2024-10-09 10:47:09 +00:00
'sanitize_callback' => '\Activitypub\object_to_uri',
);
$params['type'] = array(
'required' => true,
);
$params['object'] = array(
'required' => true,
'validate_callback' => function ( $param, $request, $key ) {
/**
* Filter the ActivityPub object validation.
*
* @param bool $validate The validation result.
* @param array $param The object data.
* @param object $request The request object.
* @param string $key The key.
*/
return apply_filters( 'activitypub_validate_object', true, $param, $request, $key );
},
);
return $params;
}
/**
* The supported parameters.
*
* @return array List of parameters.
*/
public static function shared_inbox_post_parameters() {
$params = self::user_inbox_post_parameters();
$params['to'] = array(
'required' => false,
'sanitize_callback' => function ( $param ) {
if ( \is_string( $param ) ) {
$param = array( $param );
}
return $param;
},
);
$params['cc'] = array(
'sanitize_callback' => function ( $param ) {
if ( \is_string( $param ) ) {
$param = array( $param );
}
return $param;
},
);
$params['bcc'] = array(
'sanitize_callback' => function ( $param ) {
if ( \is_string( $param ) ) {
$param = array( $param );
}
return $param;
},
);
2018-09-27 20:28:49 +00:00
return $params;
}
/**
* Get local user recipients.
*
* @param array $data The data array.
*
* @return array The list of local users.
*/
public static function get_recipients( $data ) {
$recipients = extract_recipients_from_activity( $data );
$users = array();
2021-01-09 00:26:26 +00:00
foreach ( $recipients as $recipient ) {
2023-05-19 10:00:11 +00:00
$user_id = url_to_authorid( $recipient );
2021-01-09 00:26:26 +00:00
$user = get_user_by( 'id', $user_id );
if ( $user ) {
$users[] = $user;
}
}
return $users;
}
2018-09-27 20:28:49 +00:00
}