Skip to content

Commit 91cd81a

Browse files
committed
Use fetch_by_uri
1 parent c476b27 commit 91cd81a

File tree

2 files changed

+17
-53
lines changed

2 files changed

+17
-53
lines changed

integration/class-enable-mastodon-apps.php

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Activitypub\Integration;
99

10+
use Activitypub\Activity\Actor;
1011
use Activitypub\Collection\Actors;
1112
use Activitypub\Collection\Extra_Fields;
1213
use Activitypub\Collection\Followers;
@@ -362,82 +363,38 @@ private static function api_post_status( $post_id ) {
362363
/**
363364
* Get account for actor.
364365
*
365-
* @param string|\Activitypub\Activity\Actor $actor_or_uri The Actor object or URI.
366+
* @param string|Actor $actor_or_uri The Actor object or URI.
366367
*
367368
* @return Account|null The account.
368369
*/
369370
private static function get_account_for_actor( $actor_or_uri ) {
370371
// If it's already an Actor object, use it directly.
371-
if ( $actor_or_uri instanceof \Activitypub\Activity\Actor ) {
372+
if ( $actor_or_uri instanceof Actor ) {
372373
return self::actor_to_account( $actor_or_uri );
373374
}
374375

375376
if ( ! \is_string( $actor_or_uri ) || empty( $actor_or_uri ) ) {
376377
return null;
377378
}
378379

379-
// Try to get cached actor first.
380-
$actor = Remote_Actors::get_by_uri( $actor_or_uri );
381-
if ( $actor && ! \is_wp_error( $actor ) ) {
382-
$actor_object = Remote_Actors::get_actor( $actor );
383-
if ( $actor_object && ! \is_wp_error( $actor_object ) ) {
384-
return self::actor_to_account( $actor_object );
385-
}
386-
}
387-
388-
// Fall back to fetching remote metadata.
389-
$data = get_remote_metadata_by_actor( $actor_or_uri );
390-
391-
if ( ! $data || \is_wp_error( $data ) ) {
380+
// Fetch actor from cache or remote.
381+
$actor_post = Remote_Actors::fetch_by_uri( $actor_or_uri );
382+
if ( ! $actor_post || \is_wp_error( $actor_post ) ) {
392383
return null;
393384
}
394385

395-
$account = new Account();
396-
397-
$acct = Webfinger_Util::uri_to_acct( $actor_or_uri );
398-
if ( ! $acct || \is_wp_error( $acct ) ) {
386+
$actor = Remote_Actors::get_actor( $actor_post );
387+
if ( ! $actor || \is_wp_error( $actor ) ) {
399388
return null;
400389
}
401390

402-
if ( \str_starts_with( $acct, 'acct:' ) ) {
403-
$acct = \substr( $acct, 5 );
404-
}
405-
406-
$account->id = $acct;
407-
$account->username = $acct;
408-
$account->acct = $acct;
409-
$account->display_name = $data['name'] ?? '';
410-
$account->url = $actor_or_uri;
411-
412-
if ( ! empty( $data['summary'] ) ) {
413-
$account->note = $data['summary'];
414-
}
415-
416-
if (
417-
isset( $data['icon']['type'] ) &&
418-
isset( $data['icon']['url'] ) &&
419-
'Image' === $data['icon']['type']
420-
) {
421-
$account->avatar = $data['icon']['url'];
422-
$account->avatar_static = $data['icon']['url'];
423-
}
424-
425-
if ( isset( $data['image'] ) ) {
426-
$account->header = $data['image']['url'];
427-
$account->header_static = $data['image']['url'];
428-
}
429-
if ( ! isset( $data['published'] ) ) {
430-
$data['published'] = 'now';
431-
}
432-
$account->created_at = new \DateTime( $data['published'] );
433-
434-
return $account;
391+
return self::actor_to_account( $actor );
435392
}
436393

437394
/**
438395
* Convert an Actor object to an Account.
439396
*
440-
* @param \Activitypub\Activity\Actor $actor The actor object.
397+
* @param Actor $actor The actor object.
441398
*
442399
* @return Account The account.
443400
*/

tests/phpunit/tests/integration/class-test-enable-mastodon-apps.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Test_Enable_Mastodon_Apps extends \WP_UnitTestCase {
2727
*/
2828
public static $users = array(
2929
'[email protected]' => array(
30+
'type' => 'Person',
3031
'id' => 'https://example.org/users/username',
3132
'url' => 'https://example.org/users/username',
3233
'inbox' => 'https://example.org/users/username/inbox',
@@ -35,41 +36,47 @@ class Test_Enable_Mastodon_Apps extends \WP_UnitTestCase {
3536
'published' => '2024-01-01T00:00:00+00:00',
3637
),
3738
'[email protected]' => array(
39+
'type' => 'Person',
3840
'id' => 'https://example.com/author/jon',
3941
'url' => 'https://example.com/author/jon',
4042
'inbox' => 'https://example.com/author/jon/inbox',
4143
'name' => 'jon',
4244
'preferredUsername' => 'jon',
4345
),
4446
'[email protected]' => array(
47+
'type' => 'Person',
4548
'id' => 'https://example.org/author/doe',
4649
'url' => 'https://example.org/author/doe',
4750
'inbox' => 'https://example.org/author/doe/inbox',
4851
'name' => 'doe',
4952
'preferredUsername' => 'doe',
5053
),
5154
'[email protected]' => array(
55+
'type' => 'Person',
5256
'id' => 'http://sally.example.org',
5357
'url' => 'http://sally.example.org',
5458
'inbox' => 'http://sally.example.org/inbox',
5559
'name' => 'jon',
5660
'preferredUsername' => 'jon',
5761
),
5862
'[email protected]' => array(
63+
'type' => 'Person',
5964
'id' => 'https://12345.example.com',
6065
'url' => 'https://12345.example.com',
6166
'inbox' => 'https://12345.example.com/inbox',
6267
'name' => '12345',
6368
'preferredUsername' => '12345',
6469
),
6570
'[email protected]' => array(
71+
'type' => 'Person',
6672
'id' => 'https://user2.example.com',
6773
'url' => 'https://user2.example.com',
6874
'inbox' => 'https://user2.example.com/inbox',
6975
'name' => 'úser2',
7076
'preferredUsername' => 'user2',
7177
),
7278
'[email protected]' => array(
79+
'type' => 'Person',
7380
'url' => 'https://error.example.com',
7481
'name' => 'error',
7582
'preferredUsername' => 'error',

0 commit comments

Comments
 (0)