updated plugin ActivityPub version 2.2.0

This commit is contained in:
2024-03-28 09:39:50 +00:00
committed by Gitium
parent fd53533f59
commit 00c5db12cc
50 changed files with 1726 additions and 374 deletions

View File

@ -5,7 +5,8 @@ use WP_Error;
use WP_REST_Server;
use WP_REST_Response;
use Activitypub\Transformer\Post;
use Activitypub\Activity\Activity;
use Activitypub\Activity\Actor;
use Activitypub\Activity\Base_Object;
use Activitypub\Collection\Users as User_Collection;
use function Activitypub\esc_hashtag;
@ -102,7 +103,7 @@ class Collection {
}
$response = array(
'@context' => Activity::CONTEXT,
'@context' => Base_Object::JSON_LD_CONTEXT,
'id' => get_rest_url_by_path( sprintf( 'users/%d/collections/tags', $user->get__id() ) ),
'type' => 'Collection',
'totalItems' => is_countable( $tags ) ? count( $tags ) : 0,
@ -160,7 +161,7 @@ class Collection {
}
$response = array(
'@context' => Activity::CONTEXT,
'@context' => Base_Object::JSON_LD_CONTEXT,
'id' => get_rest_url_by_path( sprintf( 'users/%d/collections/featured', $user_id ) ),
'type' => 'OrderedCollection',
'totalItems' => is_countable( $posts ) ? count( $posts ) : 0,
@ -168,7 +169,7 @@ class Collection {
);
foreach ( $posts as $post ) {
$response['orderedItems'][] = Post::transform( $post )->to_object()->to_array();
$response['orderedItems'][] = Post::transform( $post )->to_object()->to_array( false );
}
$rest_response = new WP_REST_Response( $response, 200 );
@ -186,7 +187,7 @@ class Collection {
*/
public static function moderators_get( $request ) {
$response = array(
'@context' => Activity::CONTEXT,
'@context' => Actor::JSON_LD_CONTEXT,
'id' => get_rest_url_by_path( 'collections/moderators' ),
'type' => 'OrderedCollection',
'orderedItems' => array(),

View File

@ -0,0 +1,91 @@
<?php
namespace Activitypub\Rest;
use WP_Error;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use Activitypub\Comment as Comment_Utils;
use Activitypub\Webfinger as Webfinger_Utils;
/**
* ActivityPub Followers REST-Class
*
* @author Matthias Pfefferle
*
* @see https://www.w3.org/TR/activitypub/#followers
*/
class Comment {
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
self::register_routes();
}
/**
* Register routes
*/
public static function register_routes() {
\register_rest_route(
ACTIVITYPUB_REST_NAMESPACE,
'/comments/(?P<comment_id>\d+)/remote-reply',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( self::class, 'remote_reply_get' ),
'permission_callback' => '__return_true',
'args' => array(
'resource' => array(
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
),
),
)
);
}
/**
* Endpoint for remote follow UI/Block
*
* @param WP_REST_Request $request The request object.
*
* @return void|string The URL to the remote follow page
*/
public static function remote_reply_get( WP_REST_Request $request ) {
$resource = $request->get_param( 'resource' );
$comment_id = $request->get_param( 'comment_id' );
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return new WP_Error( 'activitypub_comment_not_found', __( 'Comment not found', 'activitypub' ), array( 'status' => 404 ) );
}
$is_local = Comment_Utils::is_local( $comment );
if ( $is_local ) {
return new WP_Error( 'activitypub_local_only_comment', __( 'Comment is local only', 'activitypub' ), array( 'status' => 403 ) );
}
$template = Webfinger_Utils::get_remote_follow_endpoint( $resource );
if ( is_wp_error( $template ) ) {
return $template;
}
$resource = \get_comment_meta( $comment_id, 'source_id', true );
if ( ! $resource ) {
$resource = Comment_Utils::generate_id( $comment );
}
$url = str_replace( '{uri}', $resource, $template );
return new WP_REST_Response(
array( 'url' => $url ),
200
);
}
}

View File

@ -96,7 +96,7 @@ class Followers {
$json->orderedItems = array_map(
function ( $item ) use ( $context ) {
if ( 'full' === $context ) {
return $item->to_array();
return $item->to_array( false );
}
return $item->get_url();
},

View File

@ -11,7 +11,6 @@ use function Activitypub\get_context;
use function Activitypub\object_to_uri;
use function Activitypub\url_to_authorid;
use function Activitypub\get_rest_url_by_path;
use function Activitypub\get_remote_metadata_by_actor;
use function Activitypub\extract_recipients_from_activity;
/**

View File

@ -129,7 +129,7 @@ class Nodeinfo {
$nodeinfo = array();
$nodeinfo['version'] = '1.0';
$nodeinfo['version'] = '2.0';
$nodeinfo['server'] = array(
'baseUrl' => \home_url( '/' ),
'name' => \get_bloginfo( 'name' ),

View File

@ -59,7 +59,7 @@ class Outbox {
return $user;
}
$post_types = \get_option( 'activitypub_support_post_types', array( 'post', 'page' ) );
$post_types = \get_option( 'activitypub_support_post_types', array( 'post' ) );
$page = $request->get_param( 'page', 1 );
@ -108,10 +108,8 @@ class Outbox {
$post = Post::transform( $post )->to_object();
$activity = new Activity();
$activity->set_type( 'Create' );
$activity->set_context( null );
$activity->set_object( $post );
$json->orderedItems[] = $activity->to_array(); // phpcs:ignore
$json->orderedItems[] = $activity->to_array( false ); // phpcs:ignore
}
}

View File

@ -49,10 +49,6 @@ class Server {
public static function application_actor() {
$user = new Application_User();
$user->set_context(
\Activitypub\Activity\Activity::CONTEXT
);
$json = $user->to_array();
$rest_response = new WP_REST_Response( $json, 200 );

View File

@ -88,10 +88,6 @@ class Users {
*/
\do_action( 'activitypub_rest_users_pre' );
$user->set_context(
Activity::CONTEXT
);
$json = $user->to_array();
$rest_response = new WP_REST_Response( $json, 200 );