Skip to content

Commit

Permalink
Merge pull request #110 from akirk/break-out/instance
Browse files Browse the repository at this point in the history
Hookify: Instance endpoints.
  • Loading branch information
obenland authored Mar 17, 2024
2 parents dcaa6ee + 051e6a4 commit 0924972
Show file tree
Hide file tree
Showing 7 changed files with 814 additions and 36 deletions.
263 changes: 227 additions & 36 deletions includes/class-mastodon-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function __construct() {

// Register Handlers.
new Handler\Account();
new Handler\Instance();
new Handler\Media_Attachment();
new Handler\Notification();
new Handler\Relationship();
Expand Down Expand Up @@ -166,9 +167,13 @@ public function rewrite_rules() {
'api/v1/filters',
'api/v1/follow_requests',
'api/v1/followed_tags',
'api/v1/instance/peers',
'api/v2/instance',
'api/v1/instance',
'api/v2/instance',
'api/v1/instance/peers',
'api/v1/instance/rules',
'api/v1/instance/domain_blocks',
'api/v1/instance/extended_description',
'api/v1/instance/translation_languages',
'api/v1/lists',
'api/v1/markers',
'api/v1/mutes',
Expand Down Expand Up @@ -248,34 +253,71 @@ public function add_rest_routes() {
'permission_callback' => $this->required_scope( 'read:announcements' ),
)
);

register_rest_route(
self::PREFIX,
'api/v1/instance',
array(
'methods' => 'GET',
'callback' => array( $this, 'api_instance' ),
'permission_callback' => array( $this, 'public_api_permission' ),
)
);
register_rest_route(
self::PREFIX,
'api/v2/instance',
array(
'methods' => 'GET',
'callback' => array( $this, 'api_instance_v2' ),
'permission_callback' => array( $this, 'public_api_permission' ),
)
);
register_rest_route(
self::PREFIX,
'api/v1/instance/peers',
array(
'methods' => 'GET',
'callback' => '__return_empty_array',
'callback' => array( $this, 'api_instance_peers' ),
'permission_callback' => array( $this, 'public_api_permission' ),
)
);
register_rest_route(
self::PREFIX,
'api/v1/instance',
'api/v1/instance/rules',
array(
'methods' => 'GET',
'callback' => array( $this, 'api_instance' ),
'callback' => array( $this, 'api_instance_rules' ),
'permission_callback' => array( $this, 'public_api_permission' ),
)
);

register_rest_route(
self::PREFIX,
'api/v2/instance',
'api/v1/instance/domain_blocks',
array(
'methods' => 'GET',
'callback' => array( $this, 'api_instance_v2' ),
'callback' => array( $this, 'api_instance_domain_blocks' ),
'permission_callback' => array( $this, 'public_api_permission' ),
)
);
register_rest_route(
self::PREFIX,
'api/v1/instance/extended_description',
array(
'methods' => 'GET',
'callback' => array( $this, 'api_instance_extended_description' ),
'permission_callback' => array( $this, 'public_api_permission' ),
)
);
register_rest_route(
self::PREFIX,
'api/v1/instance/translation_languages',
array(
'methods' => 'GET',
'callback' => array( $this, 'api_instance_translation_languages' ),
'permission_callback' => array( $this, 'public_api_permission' ),
)
);

register_rest_route(
self::PREFIX,
'api/nodeinfo/2.0.json',
Expand Down Expand Up @@ -2786,39 +2828,188 @@ public function api_announcements() {
return $ret;
}

public function api_instance() {
$ret = array(
'title' => html_entity_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
'description' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES ),
'short_description' => html_entity_decode( get_bloginfo( 'description' ), ENT_QUOTES ),
'email' => 'not@public.example',
'version' => $this->software_string(),
'stats' => array(
'user_count' => 1,
'status_count' => 1,
'domain_count' => 1,
),
/**
* Returns the software instance of Mastodon running on this domain.
*
* @return WP_REST_Response The instance data.
*/
public function api_instance(): WP_REST_Response {
/**
* Modify the instance data returned for `/api/instance` requests.
*
* @param array $ret The instance data.
*
* @return array The modified instance data.
*
* Example:
* ```php
* add_filter( 'mastodon_api_instance_v2', function( $instance_data ) {
* return new Entity\Instance();
* } );
* ```
*/
$instance = apply_filters( 'mastodon_api_instance_v1', null );

'account_domain' => \wp_parse_url( \home_url(), \PHP_URL_HOST ),
'registrations' => false,
'approval_required' => false,
'uri' => \wp_parse_url( \home_url(), \PHP_URL_HOST ),
);
return rest_ensure_response( $instance );
}

/**
* Returns the software instance of Mastodon running on this domain.
*
* @return WP_REST_Response The instance data.
*/
public function api_instance_v2(): WP_REST_Response {
/**
* Modify the instance data returned for `/api/instance` requests.
*
* @param null $instance_data The instance data.
* @return Entity\Instance The instance object.
*
* Example:
* ```php
* add_filter( 'mastodon_api_instance_v2', function( $instance_data ) {
* return new Entity\Instance();
* } );
* ```
*/
$instance = apply_filters( 'mastodon_api_instance_v2', null );

return apply_filters( 'mastodon_api_instance_v1', $ret );
return rest_ensure_response( $instance );
}

public function api_instance_v2() {
$api_instance = $this->api_instance();
$ret = array_merge(
array(
'domain' => $api_instance['account_domain'],
),
$api_instance
);
/**
* Returns the list of connected domains.
*
* @param WP_REST_Request $request The full request object.
* @return WP_REST_Response
*/
public function api_instance_peers( WP_REST_Request $request ): WP_REST_Response {
$peers = get_bookmarks();
$peers = wp_list_pluck( $peers, 'link_url' );

/**
* Modify the instance peers returned for `/api/instance/peers` requests.
*
* @param array $peers The instance peers.
* @return array The modified instance peers.
*
* Example:
* ```php
* add_filter( 'mastodon_api_instance_peers', function( $peers ) {
* $peers[] = 'https://example.com';
*
* return $peers;
* } );
* ```
*/
$peers = apply_filters( 'mastodon_api_instance_peers', $peers );

return rest_ensure_response( $peers );
}

/**
* Rules that the users of this service should follow.
*
* @param WP_REST_Request $request The full request object.
* @return WP_REST_Response
*/
public function api_instance_rules( WP_REST_Request $request ): WP_REST_Response {
/**
* Modify the instance rules returned for `/api/instance/rules` requests.
*
* @param array $rules The instance rules.
* @return array The modified instance rules.
*
* Example:
* ```php
* add_filter( 'mastodon_api_instance_rules', function( $rules ) {
* $rules[] = 'https://example.com';
*
* return $rules;
* } );
* ```
*/
$rules = apply_filters( 'mastodon_api_instance_rules', array() );

return rest_ensure_response( $rules );
}

/**
* Obtain a list of domains that have been blocked.
*
* @param WP_REST_Request $request The full request object.
* @return WP_REST_Response
*/
public function api_instance_domain_blocks( WP_REST_Request $request ): WP_REST_Response {
/**
* Modify the instance domain_blocks returned for `/api/instance/domain_blocks` requests.
*
* @param array $domain_blocks The instance domain_blocks.
* @return Entity\Domain_Block[] The list of blocked domains.
*
* Example:
* ```php
* add_filter( 'mastodon_api_instance_domain_blocks', function( $domain_blocks ) {
* $domain_blocks[] = new Entity\Domain_Block();
*
* return $domain_blocks;
* } );
* ```
*/
$domain_blocks = apply_filters( 'mastodon_api_instance_domain_blocks', array() );

return rest_ensure_response( $domain_blocks );
}

/**
* Obtain an extended description of this server.
*
* @param WP_REST_Request $request The full request object.
* @return WP_REST_Response
*/
public function api_instance_extended_description( WP_REST_Request $request ): WP_REST_Response {
/**
* Modify the instance extended_description returned for `/api/instance/extended_description` requests.
*
* @param null $extended_description The instance extended_description.
* @return Entity\Extended_Description The extended description of this server.
*
* Example:
* ```php
* add_filter( 'mastodon_api_instance_extended_description', function( $description ) {
* return new Entity\Extended_Description();
* } );
* ```
*/
$extended_description = apply_filters( 'mastodon_api_instance_extended_description', null );

unset( $ret['account_domain'] );
return rest_ensure_response( $extended_description );
}

/**
* Translation language pairs supported by the translation engine used by the server.
*
* @param WP_REST_Request $request The full request object.
* @return WP_REST_Response
*/
public function api_instance_translation_languages( WP_REST_Request $request ): WP_REST_Response {
/**
* Modify the translation languages returned for `/api/instance/translation_languages` requests.
*
* @param array $translation_languages The instance translation_languages.
* @return array The modified instance translation_languages.
*
* Example:
* ```php
* add_filter( 'mastodon_api_instance_translation_languages', function( $translation_languages ) {
* $translation_languages['en'] = array( 'de', 'es', 'fr', 'it', 'ja', 'nl', 'pl', 'pt', 'ru', 'zh' );
*
* return $translation_languages;
* } );
* ```
*/
$translation_languages = apply_filters( 'mastodon_api_instance_translation_languages', array() );

return apply_filters( 'mastodon_api_instance_v2', $ret );
return rest_ensure_response( $translation_languages );
}
}
59 changes: 59 additions & 0 deletions includes/entity/class-domain-block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Domain Block entity.
*
* This contains the Domain Block entity.
*
* @package Enable_Mastodon_Apps
*/

namespace Enable_Mastodon_Apps\Entity;

/**
* Represents a domain that is blocked by the instance.
*
* @since 0.7.0
*
* @package Enable_Mastodon_Apps
*/
class Domain_Block extends Entity {
/**
* The types of the properties.
*
* @var array
*/
protected $_types = array(
'domain' => 'string',
'digest' => 'string',
'severity' => 'string',
'comment' => 'string',
);

/**
* The domain that is blocked.
*
* @var string
*/
public string $domain = '';

/**
* The SHA256 hash of the domain that is blocked.
*
* @var string
*/
public string $digest = '';

/**
* The severity of the domain block.
*
* @var string
*/
public string $severity = '';

/**
* An optional reason for the domain block.
*
* @var string
*/
public string $comment = '';
}
Loading

0 comments on commit 0924972

Please sign in to comment.