Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Announce support #747

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions includes/class-handler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Activitypub;

use Activitypub\Handler\Announce;
use Activitypub\Handler\Create;
use Activitypub\Handler\Delete;
use Activitypub\Handler\Follow;
Expand All @@ -22,6 +23,7 @@ public static function init() {
* Register handlers.
*/
public static function register_handlers() {
Announce::init();
Create::init();
Delete::init();
Follow::init();
Expand Down
94 changes: 94 additions & 0 deletions includes/class-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,98 @@ public static function is_tombstone( $url ) {
public static function generate_cache_key( $url ) {
return 'activitypub_http_' . \md5( $url );
}

/**
* Requests the Data from the Object-URL or Object-Array
*
* @param array|string $url_or_object The Object or the Object URL.
* @param bool $cached If the result should be cached.
*
* @return array|WP_Error The Object data as array or WP_Error on failure.
*/
public static function get_remote_object( $url_or_object, $cached = true ) {
if ( is_array( $url_or_object ) ) {
if ( array_key_exists( 'id', $url_or_object ) ) {
$url = $url_or_object['id'];
} elseif ( array_key_exists( 'url', $url_or_object ) ) {
$url = $url_or_object['url'];
} else {
return new WP_Error(
'activitypub_no_valid_actor_identifier',
\__( 'The "actor" identifier is not valid', 'activitypub' ),
array(
'status' => 404,
'object' => $url_or_object,
)
);
}
} else {
$url = $url_or_object;
}

if ( preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $url ) ) {
$url = Webfinger::resolve( $url );
}

if ( ! $url ) {
return new WP_Error(
'activitypub_no_valid_actor_identifier',
\__( 'The "actor" identifier is not valid', 'activitypub' ),
array(
'status' => 404,
'object' => $url,
)
);
}

if ( is_wp_error( $url ) ) {
return $url;
}

$transient_key = self::generate_cache_key( $url );

// only check the cache if needed.
if ( $cached ) {
$data = \get_transient( $transient_key );

if ( $data ) {
return $data;
}
}

if ( ! \wp_http_validate_url( $url ) ) {
return new WP_Error(
'activitypub_no_valid_object_url',
\__( 'The "object" is/has no valid URL', 'activitypub' ),
array(
'status' => 400,
'object' => $url,
)
);
}

$response = self::get( $url );

if ( \is_wp_error( $response ) ) {
return $response;
}

$data = \wp_remote_retrieve_body( $response );
$data = \json_decode( $data, true );

if ( ! $data ) {
return new WP_Error(
'activitypub_invalid_json',
\__( 'No valid JSON data', 'activitypub' ),
array(
'status' => 400,
'object' => $url,
)
);
}

\set_transient( $transient_key, $data, WEEK_IN_SECONDS );

return $data;
}
}
69 changes: 69 additions & 0 deletions includes/handler/class-announce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
namespace Activitypub\Handler;

use Activitypub\Http;

use function Activitypub\is_activity_public;

/**
* Handle Create requests
*/
class Announce {
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_action(
'activitypub_inbox_announce',
array( self::class, 'handle_announce' ),
10,
3
);
}

/**
* Handles "Announce" requests
*
* @param array $array The activity-object
* @param int $user_id The id of the local blog-user
* @param Activitypub\Activity $activity The activity object
*
* @return void
*/
public static function handle_announce( $array, $user_id, $activity = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
return;
}

if ( ! isset( $array['object'] ) ) {
return;
}

// check if Activity is public or not
if ( ! is_activity_public( $array ) ) {
// @todo maybe send email
return;
}

// @todo save the `Announce`-Activity itself

if ( is_string( $array['object'] ) ) {
$object = Http::get_remote_object( $array['object'] );
} else {
$object = $array['object'];
}

if ( ! $object || is_wp_error( $object ) ) {
return;
}

if ( ! isset( $object['type'] ) ) {
return;
}

$type = \strtolower( $object['type'] );

\do_action( 'activitypub_inbox', $object, $user_id, $type, $activity );
\do_action( "activitypub_inbox_{$type}", $object, $user_id, $activity );
}
}