some code cleanups

This commit is contained in:
Matthias Pfefferle 2023-05-19 12:00:11 +02:00
parent ec3f8454c1
commit dd486e552f
2 changed files with 31 additions and 18 deletions

View file

@ -1,9 +1,15 @@
<?php <?php
namespace Activitypub\Rest; namespace Activitypub\Rest;
use WP_Error;
use WP_REST_Server;
use WP_REST_Response;
use Activitypub\Model\Activity; use Activitypub\Model\Activity;
use function Activitypub\get_context;
use function Activitypub\url_to_authorid;
use function Activitypub\get_rest_url_by_path; use function Activitypub\get_rest_url_by_path;
use function Activitypub\get_remote_metadata_by_actor;
/** /**
* ActivityPub Inbox REST-Class * ActivityPub Inbox REST-Class
@ -32,7 +38,7 @@ class Inbox {
'/inbox', '/inbox',
array( array(
array( array(
'methods' => \WP_REST_Server::EDITABLE, 'methods' => WP_REST_Server::EDITABLE,
'callback' => array( self::class, 'shared_inbox_post' ), 'callback' => array( self::class, 'shared_inbox_post' ),
'args' => self::shared_inbox_post_parameters(), 'args' => self::shared_inbox_post_parameters(),
'permission_callback' => '__return_true', 'permission_callback' => '__return_true',
@ -45,13 +51,13 @@ class Inbox {
'/users/(?P<user_id>\d+)/inbox', '/users/(?P<user_id>\d+)/inbox',
array( array(
array( array(
'methods' => \WP_REST_Server::EDITABLE, 'methods' => WP_REST_Server::EDITABLE,
'callback' => array( self::class, 'user_inbox_post' ), 'callback' => array( self::class, 'user_inbox_post' ),
'args' => self::user_inbox_post_parameters(), 'args' => self::user_inbox_post_parameters(),
'permission_callback' => '__return_true', 'permission_callback' => '__return_true',
), ),
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => array( self::class, 'user_inbox_get' ), 'callback' => array( self::class, 'user_inbox_get' ),
'args' => self::user_inbox_get_parameters(), 'args' => self::user_inbox_get_parameters(),
'permission_callback' => '__return_true', 'permission_callback' => '__return_true',
@ -106,7 +112,7 @@ class Inbox {
$json = new \stdClass(); $json = new \stdClass();
$json->{'@context'} = \Activitypub\get_context(); $json->{'@context'} = get_context();
$json->id = \home_url( \add_query_arg( null, null ) ); $json->id = \home_url( \add_query_arg( null, null ) );
$json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' ); $json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' );
$json->type = 'OrderedCollectionPage'; $json->type = 'OrderedCollectionPage';
@ -126,7 +132,7 @@ class Inbox {
*/ */
\do_action( 'activitypub_inbox_post' ); \do_action( 'activitypub_inbox_post' );
$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' );
@ -150,7 +156,7 @@ class Inbox {
\do_action( 'activitypub_inbox', $data, $user_id, $type ); \do_action( 'activitypub_inbox', $data, $user_id, $type );
\do_action( "activitypub_inbox_{$type}", $data, $user_id ); \do_action( "activitypub_inbox_{$type}", $data, $user_id );
return new \WP_REST_Response( array(), 202 ); return new WP_REST_Response( array(), 202 );
} }
/** /**
@ -166,7 +172,7 @@ class Inbox {
$users = self::extract_recipients( $data ); $users = self::extract_recipients( $data );
if ( ! $users ) { if ( ! $users ) {
return new \WP_Error( return new WP_Error(
'rest_invalid_param', 'rest_invalid_param',
\__( 'No recipients found', 'activitypub' ), \__( 'No recipients found', 'activitypub' ),
array( array(
@ -189,7 +195,7 @@ class Inbox {
\do_action( "activitypub_inbox_{$type}", $data, $user->ID ); \do_action( "activitypub_inbox_{$type}", $data, $user->ID );
} }
return new \WP_REST_Response( array(), 202 ); return new WP_REST_Response( array(), 202 );
} }
/** /**
@ -350,7 +356,7 @@ class Inbox {
* @param int $user_id The id of the local blog-user * @param int $user_id The id of the local blog-user
*/ */
public static function handle_reaction( $object, $user_id ) { public static function handle_reaction( $object, $user_id ) {
$meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] ); $meta = get_remote_metadata_by_actor( $object['actor'] );
$comment_post_id = \url_to_postid( $object['object'] ); $comment_post_id = \url_to_postid( $object['object'] );
@ -395,7 +401,7 @@ class Inbox {
* @param int $user_id The id of the local blog-user * @param int $user_id The id of the local blog-user
*/ */
public static function handle_create( $object, $user_id ) { public static function handle_create( $object, $user_id ) {
$meta = \Activitypub\get_remote_metadata_by_actor( $object['actor'] ); $meta = get_remote_metadata_by_actor( $object['actor'] );
if ( ! isset( $object['object']['inReplyTo'] ) ) { if ( ! isset( $object['object']['inReplyTo'] ) ) {
return; return;
@ -502,7 +508,7 @@ class Inbox {
$users = array(); $users = array();
foreach ( $recipients as $recipient ) { foreach ( $recipients as $recipient ) {
$user_id = \Activitypub\url_to_authorid( $recipient ); $user_id = url_to_authorid( $recipient );
$user = get_user_by( 'id', $user_id ); $user = get_user_by( 'id', $user_id );

View file

@ -1,6 +1,13 @@
<?php <?php
namespace Activitypub\Rest; namespace Activitypub\Rest;
use stdClass;
use WP_Error;
use WP_REST_Server;
use WP_REST_Response;
use Activitypub\Model\Post;
use Activitypub\Model\Activity;
use function Activitypub\get_rest_url_by_path; use function Activitypub\get_rest_url_by_path;
/** /**
@ -27,7 +34,7 @@ class Outbox {
'/users/(?P<user_id>\d+)/outbox', '/users/(?P<user_id>\d+)/outbox',
array( array(
array( array(
'methods' => \WP_REST_Server::READABLE, 'methods' => WP_REST_Server::READABLE,
'callback' => array( self::class, 'user_outbox_get' ), 'callback' => array( self::class, 'user_outbox_get' ),
'args' => self::request_parameters(), 'args' => self::request_parameters(),
'permission_callback' => '__return_true', 'permission_callback' => '__return_true',
@ -48,7 +55,7 @@ class Outbox {
$post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) ); $post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) );
if ( ! $author ) { if ( ! $author ) {
return new \WP_Error( return new WP_Error(
'rest_invalid_param', 'rest_invalid_param',
\__( 'User not found', 'activitypub' ), \__( 'User not found', 'activitypub' ),
array( array(
@ -67,9 +74,9 @@ class Outbox {
*/ */
\do_action( 'activitypub_outbox_pre' ); \do_action( 'activitypub_outbox_pre' );
$json = new \stdClass(); $json = new stdClass();
$json->{'@context'} = \Activitypub\get_context(); $json->{'@context'} = get_context();
$json->id = \home_url( \add_query_arg( null, null ) ); $json->id = \home_url( \add_query_arg( null, null ) );
$json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' ); $json->generator = 'http://wordpress.org/?v=' . \get_bloginfo_rss( 'version' );
$json->actor = \get_author_posts_url( $user_id ); $json->actor = \get_author_posts_url( $user_id );
@ -103,8 +110,8 @@ class Outbox {
); );
foreach ( $posts as $post ) { foreach ( $posts as $post ) {
$activitypub_post = new \Activitypub\Model\Post( $post ); $activitypub_post = new Post( $post );
$activitypub_activity = new \Activitypub\Model\Activity( 'Create', false ); $activitypub_activity = new Activity( 'Create', false );
$activitypub_activity->from_post( $activitypub_post ); $activitypub_activity->from_post( $activitypub_post );
$json->orderedItems[] = $activitypub_activity->to_array(); // phpcs:ignore $json->orderedItems[] = $activitypub_activity->to_array(); // phpcs:ignore
@ -119,7 +126,7 @@ class Outbox {
*/ */
\do_action( 'activitypub_outbox_post' ); \do_action( 'activitypub_outbox_post' );
$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' );