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

329 lines
7.7 KiB
PHP
Raw Permalink Normal View History

2018-09-27 22:28:49 +02:00
<?php
namespace Activitypub\Rest;
2023-05-19 12:00:11 +02:00
use WP_Error;
use WP_REST_Server;
2023-04-21 08:51:38 +02:00
use WP_REST_Response;
2023-07-03 17:59:42 +02:00
use Activitypub\Activity\Activity;
2023-07-03 19:56:06 +02:00
use Activitypub\Collection\Users as User_Collection;
2023-04-20 15:22:11 +02:00
2023-05-19 12:00:11 +02:00
use function Activitypub\get_context;
use function Activitypub\object_to_uri;
2023-05-19 12:00:11 +02:00
use function Activitypub\url_to_authorid;
2023-05-12 22:31:53 +02:00
use function Activitypub\get_rest_url_by_path;
2023-05-19 12:00:11 +02:00
use function Activitypub\get_remote_metadata_by_actor;
use function Activitypub\extract_recipients_from_activity;
2023-04-20 15:22:11 +02:00
2018-09-27 22:28:49 +02:00
/**
2019-02-24 13:01:28 +01:00
* ActivityPub Inbox REST-Class
2018-09-27 22:28:49 +02:00
*
* @author Matthias Pfefferle
2019-02-24 13:01:28 +01:00
*
* @see https://www.w3.org/TR/activitypub/#inbox
2018-09-27 22:28:49 +02:00
*/
class Inbox {
2019-02-24 13:01:28 +01:00
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
self::register_routes();
}
2019-02-28 19:31:55 +01:00
2018-09-27 22:28:49 +02:00
/**
* Register routes
*/
public static function register_routes() {
2019-09-27 10:12:59 +02:00
\register_rest_route(
ACTIVITYPUB_REST_NAMESPACE,
2022-01-27 13:09:11 +01:00
'/inbox',
array(
2018-09-27 22:28:49 +02:00
array(
2023-10-12 11:00:58 +02:00
'methods' => WP_REST_Server::CREATABLE,
2023-04-20 15:22:11 +02:00
'callback' => array( self::class, 'shared_inbox_post' ),
'args' => self::shared_inbox_post_parameters(),
2020-09-18 16:36:09 +02:00
'permission_callback' => '__return_true',
2018-09-27 22:28:49 +02:00
),
)
);
2019-09-27 10:12:59 +02:00
\register_rest_route(
ACTIVITYPUB_REST_NAMESPACE,
2023-05-26 16:08:08 +02:00
'/users/(?P<user_id>[\w\-\.]+)/inbox',
2022-01-27 13:09:11 +01:00
array(
2018-09-27 22:28:49 +02:00
array(
2023-10-12 11:00:58 +02:00
'methods' => WP_REST_Server::CREATABLE,
2023-04-20 15:22:11 +02:00
'callback' => array( self::class, 'user_inbox_post' ),
'args' => self::user_inbox_post_parameters(),
2020-09-18 16:36:09 +02:00
'permission_callback' => '__return_true',
2018-09-27 22:28:49 +02:00
),
array(
2023-05-19 12:00:11 +02:00
'methods' => WP_REST_Server::READABLE,
2023-04-20 15:22:11 +02:00
'callback' => array( self::class, 'user_inbox_get' ),
'args' => self::user_inbox_get_parameters(),
'permission_callback' => '__return_true',
),
2018-09-27 22:28:49 +02:00
)
);
}
/**
* Renders the user-inbox
*
* @param WP_REST_Request $request
* @return WP_REST_Response
*/
public static function user_inbox_get( $request ) {
$user_id = $request->get_param( 'user_id' );
2023-07-03 19:56:06 +02:00
$user = User_Collection::get_by_various( $user_id );
if ( is_wp_error( $user ) ) {
return $user;
}
$page = $request->get_param( 'page', 0 );
/*
* Action triggerd prior to the ActivityPub profile being created and sent to the client
*/
2023-07-25 10:47:59 +02:00
\do_action( 'activitypub_rest_inbox_pre' );
$json = new \stdClass();
2023-05-19 12:00:11 +02:00
$json->{'@context'} = get_context();
2023-07-25 13:47:49 +02:00
$json->id = get_rest_url_by_path( sprintf( 'users/%d/inbox', $user->get__id() ) );
$json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' );
$json->type = 'OrderedCollectionPage';
2023-06-28 14:22:27 +02:00
$json->partOf = get_rest_url_by_path( sprintf( 'users/%d/inbox', $user->get__id() ) ); // phpcs:ignore
$json->totalItems = 0; // phpcs:ignore
$json->orderedItems = array(); // phpcs:ignore
$json->first = $json->partOf; // phpcs:ignore
// filter output
$json = \apply_filters( 'activitypub_rest_inbox_array', $json );
/*
* Action triggerd 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
2019-02-28 19:31:55 +01:00
*
* @return WP_REST_Response
*/
public static function user_inbox_post( $request ) {
$user_id = $request->get_param( 'user_id' );
2023-07-05 15:33:07 +02:00
$user = User_Collection::get_by_various( $user_id );
if ( is_wp_error( $user ) ) {
return $user;
}
2019-11-27 08:25:04 +01:00
$data = $request->get_json_params();
$activity = Activity::init_from_array( $data );
$type = $request->get_param( 'type' );
$type = \strtolower( $type );
2018-10-02 22:16:47 +02:00
\do_action( 'activitypub_inbox', $data, $user->get__id(), $type, $activity );
\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 22:28:49 +02:00
}
2018-09-30 22:51:22 +02:00
/**
* The shared inbox
*
* @param WP_REST_Request $request
2018-09-30 22:51:22 +02:00
*
* @return WP_REST_Response
2018-09-30 22:51:22 +02: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 21:11:34 +01:00
\do_action( 'activitypub_inbox', $data, null, $type, $activity );
\do_action( "activitypub_inbox_{$type}", $data, null, $activity );
2019-11-18 20:57:00 +01: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 22:28:49 +02:00
}
2018-09-30 22:51:22 +02:00
/**
* The supported parameters
*
* @return array list of parameters
*/
public static function user_inbox_get_parameters() {
2018-09-27 22:28:49 +02:00
$params = array();
$params['page'] = array(
'type' => 'integer',
);
$params['user_id'] = array(
2018-09-27 22:28:49 +02: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 22:28:49 +02:00
$params = array();
$params['page'] = array(
'type' => 'integer',
);
$params['user_id'] = array(
2018-09-27 22:28:49 +02:00
'required' => true,
'type' => 'string',
2018-09-27 22:28:49 +02:00
);
$params['id'] = array(
'required' => true,
'sanitize_callback' => 'esc_url_raw',
2023-04-15 07:53:43 +02:00
);
$params['actor'] = array(
'required' => true,
'sanitize_callback' => function ( $param, $request, $key ) {
return object_to_uri( $param );
},
);
$params['type'] = array(
'required' => true,
2020-05-04 00:06:48 +02:00
//'type' => 'enum',
//'enum' => array( 'Create' ),
//'sanitize_callback' => function ( $param, $request, $key ) {
2023-07-20 03:39:58 +02:00
// return \strtolower( $param );
2021-11-17 21:11:34 +01:00
//},
);
$params['object'] = array(
'required' => true,
);
return $params;
}
/**
* The supported parameters
*
* @return array list of parameters
*/
public static function shared_inbox_post_parameters() {
$params = array();
$params['page'] = array(
'type' => 'integer',
);
$params['id'] = array(
'required' => true,
'type' => 'string',
'sanitize_callback' => 'esc_url_raw',
);
$params['actor'] = array(
'required' => true,
//'type' => array( 'object', 'string' ),
'sanitize_callback' => function ( $param, $request, $key ) {
return object_to_uri( $param );
},
);
$params['type'] = array(
'required' => true,
//'type' => 'enum',
//'enum' => array( 'Create' ),
//'sanitize_callback' => function ( $param, $request, $key ) {
2023-07-20 03:39:58 +02:00
// return \strtolower( $param );
2021-11-17 21:11:34 +01:00
//},
);
$params['object'] = array(
'required' => true,
2020-05-04 00:06:48 +02:00
//'type' => 'object',
);
$params['to'] = array(
2021-01-09 01:26:26 +01:00
'required' => false,
'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;
},
);
2018-09-27 22:28:49 +02:00
return $params;
}
/**
* Get local user recipients
*
* @param array $data
*
* @return array The list of local users
*/
public static function get_recipients( $data ) {
$recipients = extract_recipients_from_activity( $data );
$users = array();
2021-01-09 01:26:26 +01:00
foreach ( $recipients as $recipient ) {
2023-05-19 12:00:11 +02:00
$user_id = url_to_authorid( $recipient );
2021-01-09 01:26:26 +01:00
$user = get_user_by( 'id', $user_id );
if ( $user ) {
$users[] = $user;
}
}
return $users;
}
2018-09-27 22:28:49 +02:00
}