| 1 |
<?php |
| 2 |
/** |
| 3 |
* Following_Controller file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Rest; |
| 9 |
|
| 10 |
use Activitypub\Collection\Actors; |
| 11 |
|
| 12 |
use function Activitypub\get_context; |
| 13 |
use function Activitypub\is_single_user; |
| 14 |
use function Activitypub\get_rest_url_by_path; |
| 15 |
use function Activitypub\get_masked_wp_version; |
| 16 |
|
| 17 |
/** |
| 18 |
* Following_Controller class. |
| 19 |
* |
| 20 |
* @author Matthias Pfefferle |
| 21 |
* |
| 22 |
* @see https://www.w3.org/TR/activitypub/#following |
| 23 |
*/ |
| 24 |
class Following_Controller extends Actors_Controller { |
| 25 |
use Collection; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initialize the class, registering WordPress hooks. |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
\add_filter( 'activitypub_rest_following', array( self::class, 'default_following' ), 10, 2 ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Register routes. |
| 36 |
*/ |
| 37 |
public function register_routes() { |
| 38 |
\register_rest_route( |
| 39 |
$this->namespace, |
| 40 |
'/' . $this->rest_base . '/following', |
| 41 |
array( |
| 42 |
'args' => array( |
| 43 |
'user_id' => array( |
| 44 |
'description' => 'The ID or username of the actor.', |
| 45 |
'type' => 'string', |
| 46 |
'required' => true, |
| 47 |
'pattern' => '[\w\-\.]+', |
| 48 |
), |
| 49 |
), |
| 50 |
array( |
| 51 |
'methods' => \WP_REST_Server::READABLE, |
| 52 |
'callback' => array( $this, 'get_items' ), |
| 53 |
'permission_callback' => array( 'Activitypub\Rest\Server', 'verify_signature' ), |
| 54 |
'args' => array( |
| 55 |
'page' => array( |
| 56 |
'description' => 'Current page of the collection.', |
| 57 |
'type' => 'integer', |
| 58 |
'minimum' => 1, |
| 59 |
// No default so we can differentiate between Collection and CollectionPage requests. |
| 60 |
), |
| 61 |
'per_page' => array( |
| 62 |
'description' => 'Maximum number of items to be returned in result set.', |
| 63 |
'type' => 'integer', |
| 64 |
'default' => 10, |
| 65 |
'minimum' => 1, |
| 66 |
'maximum' => 100, |
| 67 |
), |
| 68 |
), |
| 69 |
), |
| 70 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 71 |
) |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Retrieves following list. |
| 77 |
* |
| 78 |
* @param \WP_REST_Request $request Full details about the request. |
| 79 |
* @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure. |
| 80 |
*/ |
| 81 |
public function get_items( $request ) { |
| 82 |
$user_id = $request->get_param( 'user_id' ); |
| 83 |
$user = Actors::get_by_various( $user_id ); |
| 84 |
|
| 85 |
if ( \is_wp_error( $user ) ) { |
| 86 |
return $user; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Action triggered prior to the ActivityPub profile being created and sent to the client. |
| 91 |
*/ |
| 92 |
\do_action( 'activitypub_rest_following_pre' ); |
| 93 |
|
| 94 |
$response = array( |
| 95 |
'@context' => get_context(), |
| 96 |
'id' => get_rest_url_by_path( \sprintf( 'actors/%d/following', $user->get__id() ) ), |
| 97 |
'generator' => 'https://wordpress.org/?v=' . get_masked_wp_version(), |
| 98 |
'actor' => $user->get_id(), |
| 99 |
'type' => 'OrderedCollection', |
| 100 |
); |
| 101 |
|
| 102 |
/** |
| 103 |
* Filter the list of following urls. |
| 104 |
* |
| 105 |
* @param array $items The array of following urls. |
| 106 |
* @param \Activitypub\Model\User $user The user object. |
| 107 |
*/ |
| 108 |
$items = \apply_filters( 'activitypub_rest_following', array(), $user ); |
| 109 |
|
| 110 |
$response['totalItems'] = \is_countable( $items ) ? \count( $items ) : 0; |
| 111 |
$response['orderedItems'] = $items; |
| 112 |
|
| 113 |
$response = $this->prepare_collection_response( $response, $request ); |
| 114 |
if ( is_wp_error( $response ) ) { |
| 115 |
return $response; |
| 116 |
} |
| 117 |
|
| 118 |
$response = \rest_ensure_response( $response ); |
| 119 |
$response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) ); |
| 120 |
|
| 121 |
return $response; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Add the Blog Authors to the following list of the Blog Actor |
| 126 |
* if Blog not in single mode. |
| 127 |
* |
| 128 |
* @param array $follow_list The array of following urls. |
| 129 |
* @param \Activitypub\Model\User $user The user object. |
| 130 |
* |
| 131 |
* @return array The array of following urls. |
| 132 |
*/ |
| 133 |
public static function default_following( $follow_list, $user ) { |
| 134 |
if ( 0 !== $user->get__id() || is_single_user() ) { |
| 135 |
return $follow_list; |
| 136 |
} |
| 137 |
|
| 138 |
$users = Actors::get_collection(); |
| 139 |
|
| 140 |
foreach ( $users as $user ) { |
| 141 |
$follow_list[] = $user->get_id(); |
| 142 |
} |
| 143 |
|
| 144 |
return $follow_list; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Retrieves the following schema, conforming to JSON Schema. |
| 149 |
* |
| 150 |
* @return array Item schema data. |
| 151 |
*/ |
| 152 |
public function get_item_schema() { |
| 153 |
if ( $this->schema ) { |
| 154 |
return $this->add_additional_fields_schema( $this->schema ); |
| 155 |
} |
| 156 |
|
| 157 |
$item_schema = array( |
| 158 |
'type' => 'string', |
| 159 |
'format' => 'uri', |
| 160 |
); |
| 161 |
|
| 162 |
$schema = $this->get_collection_schema( $item_schema ); |
| 163 |
|
| 164 |
// Add following-specific properties. |
| 165 |
$schema['title'] = 'following'; |
| 166 |
$schema['properties']['actor'] = array( |
| 167 |
'description' => 'The actor who owns the following collection.', |
| 168 |
'type' => 'string', |
| 169 |
'format' => 'uri', |
| 170 |
'readonly' => true, |
| 171 |
); |
| 172 |
$schema['properties']['generator'] = array( |
| 173 |
'description' => 'The generator of the following collection.', |
| 174 |
'type' => 'string', |
| 175 |
'format' => 'uri', |
| 176 |
'readonly' => true, |
| 177 |
); |
| 178 |
|
| 179 |
$this->schema = $schema; |
| 180 |
|
| 181 |
return $this->add_additional_fields_schema( $this->schema ); |
| 182 |
} |
| 183 |
} |
| 184 |
|