Skip to content

Commit

Permalink
Enable Mastodon Apps: Implement the account endpoint for an external …
Browse files Browse the repository at this point in the history
…account (#674)

* Implement the account endpoint for an external account

* phpcs

* typos

* Change namespace calling

* Populate with more data

* phpcs

* some formatting

* some phpdoc

* Potentially extend an existing account

props @pfefferle

Co-authored-by: Matthias Pfefferle <[email protected]>

* phpcs

* Switch to using jsonSerialize

---------

Co-authored-by: Matthias Pfefferle <[email protected]>
  • Loading branch information
akirk and pfefferle authored Jan 25, 2024
1 parent 48feabe commit e560d1b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
66 changes: 66 additions & 0 deletions integration/class-enable-mastodon-apps.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
<?php
namespace Activitypub\Integration;

use DateTime;
use Activitypub\Webfinger as Webfinger_Util;
use Activitypub\Collection\Followers;
use Enable_Mastodon_Apps\Entity\Account;

use function Activitypub\get_remote_metadata_by_actor;

/**
* Class Enable_Mastodon_Apps
*
* This class is used to enable Mastodon Apps to work with ActivityPub
*
* @see https://github.com/akirk/enable-mastodon-apps
*/
class Enable_Mastodon_Apps {
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
\add_filter( 'mastodon_api_account_followers', array( self::class, 'api_account_followers' ), 10, 2 );
\add_filter( 'mastodon_api_account', array( self::class, 'api_account_external' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -71,4 +83,58 @@ function ( $item ) {

return $followers;
}

/**
* Resolve external accounts for Mastodon API
*
* @param Enable_Mastodon_Apps\Entity\Account $user_data The user data
* @param string $user_id The user id
*
* @return Enable_Mastodon_Apps\Entity\Account The filtered Account
*/
public static function api_account_external( $user_data, $user_id ) {
if ( ! preg_match( '/^' . ACTIVITYPUB_USERNAME_REGEXP . '$/', $user_id ) ) {
return $user_data;
}

$uri = Webfinger_Util::resolve( $user_id );

if ( ! $uri ) {
return $user_data;
}

$acct = Webfinger_Util::uri_to_acct( $uri );
$data = get_remote_metadata_by_actor( $uri );

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

if ( $user_data instanceof Account ) {
$account = $user_data;
} else {
$account = new Account();
}

$account->id = strval( $user_id );
$account->username = $acct;
$account->acct = $acct;
$account->display_name = $data['name'];
$account->url = $uri;
$account->note = $data['summary'];

if ( isset( $data['icon']['type'] ) && isset( $data['icon']['url'] ) && 'Image' === $data['icon']['type'] ) {
$account->avatar = $data['icon']['url'];
$account->avatar_static = $data['icon']['url'];
}

if ( isset( $data['image'] ) ) {
$account->header = $data['image'];
$account->header_static = $data['image'];
}

$account->created_at = new DateTime( $data['published'] );

return $account;
}
}
4 changes: 4 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
*/
function _manually_load_plugin() {
require \dirname( __DIR__ ) . '/activitypub.php';
$enable_mastodon_apps_plugin = dirname( dirname( __DIR__ ) ) . '/enable-mastodon-apps/enable-mastodon-apps.php'; // phpcs:ignore
if ( file_exists( $enable_mastodon_apps_plugin ) ) {
require $enable_mastodon_apps_plugin;
}
}
\tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

Expand Down
23 changes: 23 additions & 0 deletions tests/test-class-enable-mastodon-apps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
class Test_Enable_Mastodon_Apps extends WP_UnitTestCase {
public function set_up() {
parent::set_up();

if ( ! class_exists( '\Enable_Mastodon_Apps\Entity\Entity' ) ) {
self::markTestSkipped( 'The Enable_Mastodon_Apps plugin is not active.' );
}
}

public function test_api_account_external() {
$account = apply_filters( 'mastodon_api_account', array(), '[email protected]' );
$this->assertNotEmpty( $account );
$account = $account->jsonSerialize();
$this->assertArrayHasKey( 'id', $account );
$this->assertArrayHasKey( 'username', $account );
$this->assertArrayHasKey( 'acct', $account );
$this->assertArrayHasKey( 'display_name', $account );
$this->assertArrayHasKey( 'url', $account );
$this->assertEquals( 'https://alex.kirk.at/author/alex/', $account['url'] );
$this->assertEquals( 'Alex Kirk', $account['display_name'] );
}
}

0 comments on commit e560d1b

Please sign in to comment.