updated plugin Simple Local Avatars version 2.7.8

This commit is contained in:
2024-05-09 15:26:55 +00:00
committed by Gitium
parent 496ccfac3d
commit 62f3186aef
18 changed files with 295 additions and 80 deletions

View File

@ -3,10 +3,14 @@ namespace Activitypub\Integration;
use DateTime;
use Activitypub\Webfinger as Webfinger_Util;
use Activitypub\Http;
use Activitypub\Collection\Users;
use Activitypub\Collection\Followers;
use Activitypub\Integration\Nodeinfo;
use Enable_Mastodon_Apps\Mastodon_API;
use Enable_Mastodon_Apps\Entity\Account;
use Enable_Mastodon_Apps\Entity\Status;
use Enable_Mastodon_Apps\Entity\Media_Attachment;
use function Activitypub\get_remote_metadata_by_actor;
@ -23,9 +27,11 @@ class Enable_Mastodon_Apps {
*/
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' ), 20, 2 );
\add_filter( 'mastodon_api_account', array( self::class, 'api_account_add_followers' ), 20, 2 );
\add_filter( 'mastodon_api_account', array( self::class, 'api_account_external' ), 10, 2 );
\add_filter( 'mastodon_api_search', array( self::class, 'api_search' ), 40, 2 );
\add_filter( 'mastodon_api_get_posts_query_args', array( self::class, 'api_get_posts_query_args' ) );
\add_filter( 'mastodon_api_statuses', array( self::class, 'api_statuses_external' ), 10, 2 );
}
/**
@ -95,8 +101,8 @@ class Enable_Mastodon_Apps {
*
* @return Enable_Mastodon_Apps\Entity\Account The filtered Account
*/
public static function api_account( $account, $user_id ) {
if ( ! $account instanceof Account ) {
public static function api_account_add_followers( $account, $user_id ) {
if ( ! $account instanceof Account || ! is_numeric( $user_id ) ) {
return $account;
}
@ -121,8 +127,9 @@ class Enable_Mastodon_Apps {
}
}
$account->acct = $user->get_webfinger();
$account->acct = $user->get_preferred_username();
$account->note = $user->get_summary();
$account->followers_count = Followers::count_followers( $user_id );
return $account;
}
@ -136,34 +143,47 @@ class Enable_Mastodon_Apps {
* @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 ) ) {
if ( $user_data || is_numeric( $user_id ) ) {
// Only augment.
return $user_data;
}
$uri = Webfinger_Util::resolve( $user_id );
if ( ! $uri ) {
if ( ! $uri || is_wp_error( $uri ) ) {
return $user_data;
}
$acct = Webfinger_Util::uri_to_acct( $uri );
$account = self::get_account_for_actor( $uri );
if ( $account ) {
return $account;
}
return $user_data;
}
private static function get_account_for_actor( $uri ) {
if ( ! is_string( $uri ) ) {
return null;
}
$data = get_remote_metadata_by_actor( $uri );
if ( ! $data || is_wp_error( $data ) ) {
return $user_data;
return null;
}
$account = new Account();
$acct = Webfinger_Util::uri_to_acct( $uri );
if ( str_starts_with( $acct, 'acct:' ) ) {
$acct = substr( $acct, 5 );
}
if ( $user_data instanceof Account ) {
$account = $user_data;
} else {
$account = new Account();
}
$account->id = strval( $user_id );
$account->id = $acct;
$account->username = $acct;
$account->acct = $acct;
$account->display_name = $data['name'];
$account->url = $uri;
if ( ! empty( $data['summary'] ) ) {
$account->note = $data['summary'];
}
@ -178,8 +198,8 @@ class Enable_Mastodon_Apps {
}
if ( isset( $data['image'] ) ) {
$account->header = $data['image'];
$account->header_static = $data['image'];
$account->header = $data['image']['url'];
$account->header_static = $data['image']['url'];
}
$account->created_at = new DateTime( $data['published'] );
@ -233,4 +253,114 @@ class Enable_Mastodon_Apps {
return $search_data;
}
public function api_get_posts_query_args( $args ) {
if ( isset( $args['author'] ) && is_string( $args['author'] ) ) {
$uri = Webfinger_Util::resolve( $args['author'] );
if ( $uri && ! is_wp_error( $uri ) ) {
$args['activitypub'] = $uri;
unset( $args['author'] );
}
}
return $args;
}
public static function api_statuses_external( $statuses, $args ) {
if ( ! isset( $args['activitypub'] ) ) {
return $statuses;
}
$data = get_remote_metadata_by_actor( $args['activitypub'] );
if ( ! $data || is_wp_error( $data ) || ! isset( $data['outbox'] ) ) {
return $statuses;
}
$response = Http::get( $data['outbox'], true );
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
return $statuses;
}
$outbox = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! $outbox || is_wp_error( $outbox ) || ! isset( $outbox['first'] ) ) {
return $statuses;
}
$account = self::get_account_for_actor( $args['activitypub'] );
if ( ! $account ) {
return $statuses;
}
$response = Http::get( $outbox['first'], true );
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
return $statuses;
}
$posts = json_decode( wp_remote_retrieve_body( $response ), true );
$activitypub_statuses = array_map(
function ( $item ) use ( $account ) {
$object = $item['object'];
if ( ! isset( $object['type'] ) || 'Note' !== $object['type'] ) {
return null;
}
$status = new Status();
$status->id = Mastodon_API::remap_url( $object['id'] );
$status->created_at = new DateTime( $object['published'] );
$status->content = $object['content'];
$status->account = $account;
if ( ! empty( $object['inReplyTo'] ) ) {
$status->in_reply_to_id = $object['inReplyTo'];
}
if ( ! empty( $object['visibility'] ) ) {
$status->visibility = $object['visibility'];
}
$status->uri = $object['url'];
if ( ! empty( $object['attachment'] ) ) {
$status->media_attachments = array_map(
function ( $attachment ) {
$default_attachment = array(
'url' => null,
'mediaType' => null,
'name' => null,
'width' => 0,
'height' => 0,
'blurhash' => null,
);
$attachment = array_merge( $default_attachment, $attachment );
$media_attachment = new Media_Attachment();
$media_attachment->id = Mastodon_API::remap_url( $attachment['url'], $attachment );
$media_attachment->type = strtok( $attachment['mediaType'], '/' );
$media_attachment->url = $attachment['url'];
$media_attachment->preview_url = $attachment['url'];
$media_attachment->description = $attachment['name'];
$media_attachment->blurhash = $attachment['blurhash'];
$media_attachment->meta = array(
'original' => array(
'width' => $attachment['width'],
'height' => $attachment['height'],
'size' => $attachment['width'] . 'x' . $attachment['height'],
'aspect' => $attachment['width'] / $attachment['height'],
),
);
return $media_attachment;
},
$object['attachment']
);
}
return $status;
},
$posts['orderedItems']
);
return $activitypub_statuses;
}
}