Skip to content

Commit

Permalink
Merge pull request #78 from akirk/dedicated-integrations
Browse files Browse the repository at this point in the history
Create dedicated Integrations classes
  • Loading branch information
akirk authored Mar 16, 2024
2 parents 217fd06 + e43dd42 commit add3c0a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 72 deletions.
24 changes: 2 additions & 22 deletions enable-mastodon-apps.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,16 @@ function ( $full_class ) {
require_once $file;
} else {
// translators: %s is the class name.
\wp_die( sprintf( esc_html__( 'Required class not found or not readable: %s', 'enable-mastodon-apps' ), esc_html( $full_class ) ) );
\wp_die( sprintf( esc_html__( 'Required class not found or not readable: %s', 'enable-mastodon-apps' ), esc_html( $file ) ) );
}
}
}
);

function mastodon_api_pixelfed_nodeinfo_software( $software ) {
if ( 'okhttp/4.9.2' === $_SERVER['HTTP_USER_AGENT'] ) {
return array(
'name' => 'pixelfed',
'version' => '0.11.5',
);
}

return $software;
}
add_filter( 'mastodon_api_nodeinfo_software', 'mastodon_api_pixelfed_nodeinfo_software' );

function mastodon_api_pixelfed_post_formats( $post_formats, $app_metadata ) {
if ( in_array( $app_metadata, array( 'Pixelfed' ) ) ) {
$post_formats = array( 'image' );
}

return $post_formats;
}
add_filter( 'mastodon_api_new_app_post_formats', 'mastodon_api_pixelfed_post_formats', 10, 2 );

add_action(
'init',
function () {
new Enable_Mastodon_Apps\Mastodon_API();
new Enable_Mastodon_Apps\Integration\Pixelfed();
}
);
108 changes: 58 additions & 50 deletions includes/class-mastodon-api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Friends Mastodon API
* Mastodon API
*
* This contains the REST API handlers.
*
Expand Down Expand Up @@ -142,56 +142,62 @@ public function rewrite_rules() {
$existing_rules = get_option( 'rewrite_rules' );
$needs_flush = false;

$generic = array(
'api/v1/accounts/relationships',
'api/v1/accounts/verify_credentials',
'api/v1/accounts/familiar_followers',
'api/v1/accounts/search',
'api/v1/announcements',
'api/v1/apps',
'api/v1/bookmarks',
'api/v1/conversations',
'api/v1/custom_emojis',
'api/v1/favourites',
'api/v1/filters',
'api/v1/follow_requests',
'api/v1/followed_tags',
'api/v1/instance/peers',
'api/v2/instance',
'api/v1/instance',
'api/v1/lists',
'api/v1/markers',
'api/v1/mutes',
'api/v1/notifications/clear',
'api/v1/preferences',
'api/v1/trends/statuses',
'api/v1/push/subscription',
'api/v1/streaming',
'api/v2/media',
$generic = apply_filters(
'mastodon_api_generic_routes',
array(
'api/v1/accounts/relationships',
'api/v1/accounts/verify_credentials',
'api/v1/accounts/familiar_followers',
'api/v1/accounts/search',
'api/v1/announcements',
'api/v1/apps',
'api/v1/bookmarks',
'api/v1/conversations',
'api/v1/custom_emojis',
'api/v1/favourites',
'api/v1/filters',
'api/v1/follow_requests',
'api/v1/followed_tags',
'api/v1/instance/peers',
'api/v2/instance',
'api/v1/instance',
'api/v1/lists',
'api/v1/markers',
'api/v1/mutes',
'api/v1/notifications/clear',
'api/v1/preferences',
'api/v1/trends/statuses',
'api/v1/push/subscription',
'api/v1/streaming',
'api/v2/media',
)
);
$parametrized = array(
'api/v1/accounts/([^/]+)/featured_tags' => 'api/v1/accounts/$matches[1]/featured_tags',
'api/v1/accounts/([^/]+)/followers' => 'api/v1/accounts/$matches[1]/followers',
'api/v1/accounts/([^/]+)/follow' => 'api/v1/accounts/$matches[1]/follow',
'api/v1/accounts/([^/]+)/unfollow' => 'api/v1/accounts/$matches[1]/unfollow',
'api/v1/accounts/([^/]+)/statuses' => 'api/v1/accounts/$matches[1]/statuses',
'api/v1/statuses/([0-9]+)/context' => 'api/v1/statuses/$matches[1]/context',
'api/v1/statuses/([0-9]+)/favourited_by' => 'api/v1/statuses/$matches[1]/favourited_by',
'api/v1/statuses/([0-9]+)/favourite' => 'api/v1/statuses/$matches[1]/favourite',
'api/v1/statuses/([0-9]+)/unfavourite' => 'api/v1/statuses/$matches[1]/unfavourite',
'api/v1/statuses/([0-9]+)/reblog' => 'api/v1/statuses/$matches[1]/reblog',
'api/v1/statuses/([0-9]+)/unreblog' => 'api/v1/statuses/$matches[1]/unreblog',
'api/v1/notifications/([^/]+)/dismiss' => 'api/v1/notifications/$matches[1]/dismiss',
'api/v1/notifications/([^/|$]+)/?$' => 'api/v1/notifications/$matches[1]',
'api/v1/notifications' => 'api/v1/notifications',
'api/nodeinfo/([0-9]+[.][0-9]+).json' => 'api/nodeinfo/$matches[1].json',
'api/v1/media/([0-9]+)' => 'api/v1/media/$matches[1]',
'api/v1/statuses/([0-9]+)' => 'api/v1/statuses/$matches[1]',
'api/v1/statuses' => 'api/v1/statuses',
'api/v1/accounts/(.+)' => 'api/v1/accounts/$matches[1]',
'api/v1/timelines/(home|public)' => 'api/v1/timelines/$matches[1]',
'api/v1/timelines/tag/([^/|$]+)' => 'api/v1/timelines/tag/$matches[1]',
'api/v2/search' => 'api/v1/search',
$parametrized = apply_filters(
'mastodon_api_parametrized_routes',
array(
'api/v1/accounts/([^/]+)/featured_tags' => 'api/v1/accounts/$matches[1]/featured_tags',
'api/v1/accounts/([^/]+)/followers' => 'api/v1/accounts/$matches[1]/followers',
'api/v1/accounts/([^/]+)/follow' => 'api/v1/accounts/$matches[1]/follow',
'api/v1/accounts/([^/]+)/unfollow' => 'api/v1/accounts/$matches[1]/unfollow',
'api/v1/accounts/([^/]+)/statuses' => 'api/v1/accounts/$matches[1]/statuses',
'api/v1/statuses/([0-9]+)/context' => 'api/v1/statuses/$matches[1]/context',
'api/v1/statuses/([0-9]+)/favourited_by' => 'api/v1/statuses/$matches[1]/favourited_by',
'api/v1/statuses/([0-9]+)/favourite' => 'api/v1/statuses/$matches[1]/favourite',
'api/v1/statuses/([0-9]+)/unfavourite' => 'api/v1/statuses/$matches[1]/unfavourite',
'api/v1/statuses/([0-9]+)/reblog' => 'api/v1/statuses/$matches[1]/reblog',
'api/v1/statuses/([0-9]+)/unreblog' => 'api/v1/statuses/$matches[1]/unreblog',
'api/v1/notifications/([^/]+)/dismiss' => 'api/v1/notifications/$matches[1]/dismiss',
'api/v1/notifications/([^/|$]+)/?$' => 'api/v1/notifications/$matches[1]',
'api/v1/notifications' => 'api/v1/notifications',
'api/nodeinfo/([0-9]+[.][0-9]+).json' => 'api/nodeinfo/$matches[1].json',
'api/v1/media/([0-9]+)' => 'api/v1/media/$matches[1]',
'api/v1/statuses/([0-9]+)' => 'api/v1/statuses/$matches[1]',
'api/v1/statuses' => 'api/v1/statuses',
'api/v1/accounts/(.+)' => 'api/v1/accounts/$matches[1]',
'api/v1/timelines/(home|public)' => 'api/v1/timelines/$matches[1]',
'api/v1/timelines/tag/([^/|$]+)' => 'api/v1/timelines/tag/$matches[1]',
'api/v2/search' => 'api/v1/search',
)
);

foreach ( $generic as $rule ) {
Expand Down Expand Up @@ -704,6 +710,8 @@ public function add_rest_routes() {
'permission_callback' => array( $this, 'public_api_permission' ),
)
);

do_action( 'mastodon_api_register_rest_routes', $this );
}

public function query_vars( $query_vars ) {
Expand Down
44 changes: 44 additions & 0 deletions includes/integration/class-pixelfed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Pixelfed Integration
*
* Add what it takes to make Pixelfed apps talk to WordPress.
*
* @package Enable_Mastodon_Apps
*/

namespace Enable_Mastodon_Apps\Integration;

/**
* This is the class that implements the Pixelfed adapations.
*
* @since 0.1
*
* @package Enable_Mastodon_Apps
* @author Alex Kirk
*/
class Pixelfed {
public function __construct() {
add_filter( 'mastodon_api_nodeinfo_software', array( $this, 'mastodon_api_pixelfed_nodeinfo_software' ) );
add_filter( 'mastodon_api_new_app_post_formats', array( $this, 'mastodon_api_pixelfed_post_formats' ), 10, 2 );
}

public function mastodon_api_pixelfed_nodeinfo_software( $software ) {
if ( 'okhttp/4.9.2' === $_SERVER['HTTP_USER_AGENT'] ) {
return array(
'name' => 'pixelfed',
'version' => '0.11.5',
);
}

return $software;
}

public function mastodon_api_pixelfed_post_formats( $post_formats, $app_metadata ) {
if ( in_array( $app_metadata, array( 'Pixelfed' ) ) ) {
$post_formats = array( 'image' );
}

return $post_formats;
}
}

0 comments on commit add3c0a

Please sign in to comment.