Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function rest_init() {
Rest\Server::init();
( new Rest\Actors_Controller() )->register_routes();
( new Rest\Actors_Inbox_Controller() )->register_routes();
( new Rest\Admin\Actions_Controller() )->register_routes();
( new Rest\Application_Controller() )->register_routes();
( new Rest\Collections_Controller() )->register_routes();
( new Rest\Comments_Controller() )->register_routes();
Expand Down
11 changes: 11 additions & 0 deletions build/admin-followers/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "activitypub/admin-followers",
"title": "ActivityPub Admin Followers DataViews",
"category": "widgets",
"description": "Admin interface for managing ActivityPub followers using DataViews.",
"textdomain": "activitypub",
"editorScript": "file:./index.js",
"style": "file:./style.scss"
}
1 change: 1 addition & 0 deletions build/admin-followers/index.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'react-jsx-runtime', 'wp-api-fetch', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-date', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-keycodes', 'wp-notices', 'wp-preferences', 'wp-primitives', 'wp-private-apis', 'wp-warning'), 'version' => '41ad72ce53e39198439f');
54 changes: 54 additions & 0 deletions build/admin-followers/index.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions build/admin-followers/style-index-rtl.css

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions build/admin-followers/style-index.css

Large diffs are not rendered by default.

99 changes: 98 additions & 1 deletion includes/class-post-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
use Activitypub\Activity\Activity;
use Activitypub\Collection\Extra_Fields;
use Activitypub\Collection\Followers;
use Activitypub\Collection\Following;
use Activitypub\Collection\Inbox;
use Activitypub\Collection\Outbox;
use Activitypub\Collection\Posts;
use Activitypub\Collection\Remote_Actors;

use function Activitypub\object_to_uri;

/**
* Post Types class.
*/
Expand Down Expand Up @@ -61,7 +64,7 @@ public static function register_remote_actors_post_type() {
'query_var' => false,
'delete_with_user' => false,
'can_export' => true,
'supports' => array(),
'supports' => array( 'custom-fields' ),
)
);

Expand Down Expand Up @@ -92,6 +95,7 @@ public static function register_remote_actors_post_type() {
array(
'type' => 'string',
'single' => false,
'show_in_rest' => true,
'sanitize_callback' => 'sanitize_text_field',
)
);
Expand Down Expand Up @@ -572,6 +576,99 @@ public static function register_ap_actor_rest_field() {
),
)
);

// Add formatted actor data field.
\register_rest_field(
Remote_Actors::POST_TYPE,
'actor_info',
array(
'get_callback' => function ( $response ) {
$actor = Remote_Actors::get_actor( $response['id'] );
if ( \is_wp_error( $actor ) ) {
return null;
}
return array(
'username' => $actor->get_preferred_username(),
'name' => $actor->get_name() ?? $actor->get_preferred_username(),
'icon' => object_to_uri( $actor->get_icon() ),
'url' => object_to_uri( $actor->get_url() ?? $actor->get_id() ),
'webfinger' => Remote_Actors::get_acct( $response['id'] ),
'identifier' => $actor->get_id(),
);
},
'schema' => array(
'description' => 'Parsed ActivityPub actor information',
'type' => 'object',
'context' => array( 'view', 'edit' ),
),
)
);

// Add follow status field.
\register_rest_field(
Remote_Actors::POST_TYPE,
'follow_status',
array(
'get_callback' => function ( $response ) {
$current_user_id = \get_current_user_id();
if ( ! $current_user_id ) {
return array( 'follows_back' => false );
}
return array(
'follows_back' => Following::check_status( $current_user_id, $response['id'] ),
);
},
'schema' => array(
'description' => 'Follow relationship status',
'type' => 'object',
'context' => array( 'view', 'edit' ),
),
)
);

// Add custom query parameter for filtering by follower relationships.
\add_filter( 'rest_ap_actor_query', array( self::class, 'filter_ap_actor_query_by_follower' ), 10, 2 );
}

/**
* Add custom collection parameters for filtering ap_actor posts.
*
* @param array $query_params JSON Schema-formatted collection parameters.
* @return array Modified collection parameters.
*/
public static function add_follower_collection_params( $query_params ) {
$query_params['follower_of'] = array(
'description' => __( 'Limit result set to actors that follow a specific user ID.', 'activitypub' ),
'type' => 'integer',
'minimum' => 0,
);

return $query_params;
}

/**
* Filter WP_Query args to support follower_of parameter.
*
* @param array $args Array of arguments for WP_Query.
* @param \WP_REST_Request $request The REST API request.
* @return array Modified query arguments.
*/
public static function filter_ap_actor_query_by_follower( $args, $request ) {
if ( ! empty( $request['follower_of'] ) ) {
$user_id = (int) $request['follower_of'];

// Add meta_query to filter by _activitypub_following.
if ( ! isset( $args['meta_query'] ) ) {
$args['meta_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
}

$args['meta_query'][] = array(
'key' => Followers::FOLLOWER_META_KEY,
'value' => (string) $user_id,
);
}

return $args;
}

/**
Expand Down
Loading