consistent API response, GET inbox and filterable follower list

This commit is contained in:
Matthias Pfefferle 2021-01-05 21:56:38 +01:00
parent c8d341ba1f
commit c797109fd7
7 changed files with 80 additions and 11 deletions

View file

@ -39,9 +39,9 @@ class Activitypub {
} }
if ( \is_author() ) { if ( \is_author() ) {
$json_template = \dirname( __FILE__ ) . '/../templates/json-author.php'; $json_template = \dirname( __FILE__ ) . '/../templates/author-json.php';
} elseif ( \is_singular() ) { } elseif ( \is_singular() ) {
$json_template = \dirname( __FILE__ ) . '/../templates/json-post.php'; $json_template = \dirname( __FILE__ ) . '/../templates/post-json.php';
} }
global $wp_query; global $wp_query;

View file

@ -61,6 +61,11 @@ class Followers {
$json->{'@context'} = \Activitypub\get_context(); $json->{'@context'} = \Activitypub\get_context();
$json->id = \home_url( \add_query_arg( null, null ) );
$json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' );
$json->actor = \get_author_posts_url( $user_id );
$json->type = 'OrderedCollectionPage';
$json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/followers" ); // phpcs:ignore $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/followers" ); // phpcs:ignore
$json->totalItems = \Activitypub\count_followers( $user_id ); // phpcs:ignore $json->totalItems = \Activitypub\count_followers( $user_id ); // phpcs:ignore
$json->orderedItems = \Activitypub\Peer\Followers::get_followers( $user_id ); // phpcs:ignore $json->orderedItems = \Activitypub\Peer\Followers::get_followers( $user_id ); // phpcs:ignore

View file

@ -61,14 +61,17 @@ class Following {
$json->{'@context'} = \Activitypub\get_context(); $json->{'@context'} = \Activitypub\get_context();
$json->id = \home_url( \add_query_arg( null, null ) );
$json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' );
$json->actor = \get_author_posts_url( $user_id );
$json->type = 'OrderedCollectionPage';
$json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/following" ); // phpcs:ignore $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/following" ); // phpcs:ignore
$json->totalItems = 0; // phpcs:ignore $json->totalItems = 0; // phpcs:ignore
$json->orderedItems = array(); // phpcs:ignore $json->orderedItems = apply_filters( 'activitypub_following', array(), $user ); // phpcs:ignore
$json->first = $json->partOf; // phpcs:ignore $json->first = $json->partOf; // phpcs:ignore
$json->first = \get_rest_url( null, "/activitypub/1.0/users/$user_id/following" );
$response = new \WP_REST_Response( $json, 200 ); $response = new \WP_REST_Response( $json, 200 );
$response->header( 'Content-Type', 'application/activity+json' ); $response->header( 'Content-Type', 'application/activity+json' );

View file

@ -30,7 +30,7 @@ class Inbox {
'activitypub/1.0', '/inbox', array( 'activitypub/1.0', '/inbox', array(
array( array(
'methods' => \WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( '\Activitypub\Rest\Inbox', 'shared_inbox' ), 'callback' => array( '\Activitypub\Rest\Inbox', 'shared_inbox_post' ),
'args' => self::shared_inbox_request_parameters(), 'args' => self::shared_inbox_request_parameters(),
'permission_callback' => '__return_true', 'permission_callback' => '__return_true',
), ),
@ -41,10 +41,15 @@ class Inbox {
'activitypub/1.0', '/users/(?P<user_id>\d+)/inbox', array( 'activitypub/1.0', '/users/(?P<user_id>\d+)/inbox', array(
array( array(
'methods' => \WP_REST_Server::EDITABLE, 'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( '\Activitypub\Rest\Inbox', 'user_inbox' ), 'callback' => array( '\Activitypub\Rest\Inbox', 'user_inbox_post' ),
'args' => self::user_inbox_request_parameters(), 'args' => self::user_inbox_request_parameters(),
'permission_callback' => '__return_true', 'permission_callback' => '__return_true',
), ),
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( '\Activitypub\Rest\Inbox', 'user_inbox_get' ),
'permission_callback' => '__return_true',
),
) )
); );
} }
@ -82,10 +87,66 @@ class Inbox {
* Renders the user-inbox * Renders the user-inbox
* *
* @param WP_REST_Request $request * @param WP_REST_Request $request
* @return WP_REST_Response
*/
public static function user_inbox_get( $request ) {
$user_id = $request->get_param( 'user_id' );
$author = \get_user_by( 'ID', $user_id );
if ( ! $author ) {
return new \WP_Error( 'rest_invalid_param', \__( 'User not found', 'activitypub' ), array(
'status' => 404,
'params' => array(
'user_id' => \__( 'User not found', 'activitypub' ),
),
) );
}
$page = $request->get_param( 'page', 0 );
/*
* Action triggerd prior to the ActivityPub profile being created and sent to the client
*/
\do_action( 'activitypub_inbox_pre' );
$json = new \stdClass();
$json->{'@context'} = \Activitypub\get_context();
$json->id = \home_url( \add_query_arg( null, null ) );
$json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' );
$json->actor = \get_author_posts_url( $user_id );
$json->type = 'OrderedCollectionPage';
$json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/inbox" ); // phpcs:ignore
$json->totalItems = 0; // phpcs:ignore
$json->orderedItems = array(); // phpcs:ignore
$json->first = $json->partOf; // phpcs:ignore
// filter output
$json = \apply_filters( 'activitypub_inbox_array', $json );
/*
* Action triggerd after the ActivityPub profile has been created and sent to the client
*/
\do_action( 'activitypub_inbox_post' );
$response = new \WP_REST_Response( $json, 200 );
$response->header( 'Content-Type', 'application/activity+json' );
return $response;
}
/**
* Handles user-inbox requests
*
* @param WP_REST_Request $request
* *
* @return WP_REST_Response * @return WP_REST_Response
*/ */
public static function user_inbox( $request ) { public static function user_inbox_post( $request ) {
$user_id = $request->get_param( 'user_id' ); $user_id = $request->get_param( 'user_id' );
$data = $request->get_params(); $data = $request->get_params();
@ -104,7 +165,7 @@ class Inbox {
* *
* @return WP_REST_Response * @return WP_REST_Response
*/ */
public static function shared_inbox( $request ) { public static function shared_inbox_post( $request ) {
$data = $request->get_params(); $data = $request->get_params();
$type = \strtoloer( $request->get_param( 'type' ) ); $type = \strtoloer( $request->get_param( 'type' ) );

View file

@ -24,7 +24,7 @@ class Outbox {
'activitypub/1.0', '/users/(?P<user_id>\d+)/outbox', array( 'activitypub/1.0', '/users/(?P<user_id>\d+)/outbox', array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => \WP_REST_Server::READABLE,
'callback' => array( '\Activitypub\Rest\Outbox', 'user_outbox' ), 'callback' => array( '\Activitypub\Rest\Outbox', 'user_outbox_get' ),
'args' => self::request_parameters(), 'args' => self::request_parameters(),
'permission_callback' => '__return_true', 'permission_callback' => '__return_true',
), ),
@ -38,7 +38,7 @@ class Outbox {
* @param WP_REST_Request $request * @param WP_REST_Request $request
* @return WP_REST_Response * @return WP_REST_Response
*/ */
public static function user_outbox( $request ) { public static function user_outbox_get( $request ) {
$user_id = $request->get_param( 'user_id' ); $user_id = $request->get_param( 'user_id' );
$author = \get_user_by( 'ID', $user_id ); $author = \get_user_by( 'ID', $user_id );