updated plugin ActivityPub
version 2.4.0
This commit is contained in:
@ -28,10 +28,12 @@ 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_add_followers' ), 20, 2 );
|
||||
\add_filter( 'mastodon_api_account', array( self::class, 'api_account_external' ), 10, 2 );
|
||||
\add_filter( 'mastodon_api_account', array( self::class, 'api_account_external' ), 15, 2 );
|
||||
\add_filter( 'mastodon_api_search', array( self::class, 'api_search' ), 40, 2 );
|
||||
\add_filter( 'mastodon_api_search', array( self::class, 'api_search_by_url' ), 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 );
|
||||
\add_filter( 'mastodon_api_status_context', array( self::class, 'api_get_replies' ), 10, 23 );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -102,11 +104,11 @@ class Enable_Mastodon_Apps {
|
||||
* @return Enable_Mastodon_Apps\Entity\Account The filtered Account
|
||||
*/
|
||||
public static function api_account_add_followers( $account, $user_id ) {
|
||||
if ( ! $account instanceof Account || ! is_numeric( $user_id ) ) {
|
||||
if ( ! $account instanceof Account ) {
|
||||
return $account;
|
||||
}
|
||||
|
||||
$user = Users::get_by_id( $user_id );
|
||||
$user = Users::get_by_various( $user_id );
|
||||
|
||||
if ( ! $user || is_wp_error( $user ) ) {
|
||||
return $account;
|
||||
@ -130,7 +132,7 @@ class Enable_Mastodon_Apps {
|
||||
$account->acct = $user->get_preferred_username();
|
||||
$account->note = $user->get_summary();
|
||||
|
||||
$account->followers_count = Followers::count_followers( $user_id );
|
||||
$account->followers_count = Followers::count_followers( $user->get__id() );
|
||||
return $account;
|
||||
}
|
||||
|
||||
@ -143,7 +145,7 @@ class Enable_Mastodon_Apps {
|
||||
* @return Enable_Mastodon_Apps\Entity\Account The filtered Account
|
||||
*/
|
||||
public static function api_account_external( $user_data, $user_id ) {
|
||||
if ( $user_data || is_numeric( $user_id ) ) {
|
||||
if ( $user_data || ( is_numeric( $user_id ) && $user_id ) ) {
|
||||
// Only augment.
|
||||
return $user_data;
|
||||
}
|
||||
@ -201,12 +203,38 @@ class Enable_Mastodon_Apps {
|
||||
$account->header = $data['image']['url'];
|
||||
$account->header_static = $data['image']['url'];
|
||||
}
|
||||
|
||||
if ( ! isset( $data['published'] ) ) {
|
||||
$data['published'] = 'now';
|
||||
}
|
||||
$account->created_at = new DateTime( $data['published'] );
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
public static function api_search_by_url( $search_data, $request ) {
|
||||
$p = \wp_parse_url( $request->get_param( 'q' ) );
|
||||
if ( ! $p || ! isset( $p['host'] ) ) {
|
||||
return $search_data;
|
||||
}
|
||||
|
||||
$object = Http::get_remote_object( $request->get_param( 'q' ), true );
|
||||
if ( is_wp_error( $object ) || ! isset( $object['attributedTo'] ) ) {
|
||||
return $search_data;
|
||||
}
|
||||
|
||||
$account = self::get_account_for_actor( $object['attributedTo'] );
|
||||
if ( ! $account ) {
|
||||
return $search_data;
|
||||
}
|
||||
|
||||
$status = self::activity_to_status( $object, $account );
|
||||
if ( $status ) {
|
||||
$search_data['statuses'][] = $status;
|
||||
}
|
||||
|
||||
return $search_data;
|
||||
}
|
||||
|
||||
public static function api_search( $search_data, $request ) {
|
||||
$user_id = \get_current_user_id();
|
||||
if ( ! $user_id ) {
|
||||
@ -254,7 +282,7 @@ class Enable_Mastodon_Apps {
|
||||
return $search_data;
|
||||
}
|
||||
|
||||
public function api_get_posts_query_args( $args ) {
|
||||
public static 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 ) ) {
|
||||
@ -266,6 +294,78 @@ class Enable_Mastodon_Apps {
|
||||
return $args;
|
||||
}
|
||||
|
||||
private static function activity_to_status( $item, $account ) {
|
||||
if ( isset( $item['object'] ) ) {
|
||||
$object = $item['object'];
|
||||
} else {
|
||||
$object = $item;
|
||||
}
|
||||
|
||||
if ( ! isset( $object['type'] ) || 'Note' !== $object['type'] ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$status = new Status();
|
||||
$status->id = $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'];
|
||||
}
|
||||
if ( ! empty( $object['url'] ) ) {
|
||||
$status->url = $object['url'];
|
||||
$status->uri = $object['url'];
|
||||
} else {
|
||||
$status->uri = $object['id'];
|
||||
}
|
||||
|
||||
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 = $attachment['url'];
|
||||
$media_attachment->type = strtok( $attachment['mediaType'], '/' );
|
||||
$media_attachment->url = $attachment['url'];
|
||||
$media_attachment->preview_url = $attachment['url'];
|
||||
$media_attachment->description = $attachment['name'];
|
||||
if ( $attachment['blurhash'] ) {
|
||||
$media_attachment->blurhash = $attachment['blurhash'];
|
||||
}
|
||||
if ( $attachment['width'] > 0 && $attachment['height'] > 0 ) {
|
||||
$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;
|
||||
}
|
||||
|
||||
public static function api_statuses_external( $statuses, $args ) {
|
||||
if ( ! isset( $args['activitypub'] ) ) {
|
||||
return $statuses;
|
||||
@ -277,13 +377,8 @@ class Enable_Mastodon_Apps {
|
||||
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'] ) ) {
|
||||
$outbox = Http::get_remote_object( $data['outbox'], true );
|
||||
if ( is_wp_error( $outbox ) || ! isset( $outbox['first'] ) ) {
|
||||
return $statuses;
|
||||
}
|
||||
|
||||
@ -291,76 +386,77 @@ class Enable_Mastodon_Apps {
|
||||
if ( ! $account ) {
|
||||
return $statuses;
|
||||
}
|
||||
|
||||
$response = Http::get( $outbox['first'], true );
|
||||
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
|
||||
return $statuses;
|
||||
$limit = 10;
|
||||
if ( isset( $args['posts_per_page'] ) ) {
|
||||
$limit = $args['posts_per_page'];
|
||||
}
|
||||
$posts = json_decode( wp_remote_retrieve_body( $response ), true );
|
||||
if ( $limit > 40 ) {
|
||||
$limit = 40;
|
||||
}
|
||||
$activitypub_statuses = array();
|
||||
$url = $outbox['first'];
|
||||
$tries = 0;
|
||||
while ( $url ) {
|
||||
if ( ++$tries > 3 ) {
|
||||
break;
|
||||
}
|
||||
|
||||
$activitypub_statuses = array_map(
|
||||
function ( $item ) use ( $account ) {
|
||||
$object = $item['object'];
|
||||
if ( ! isset( $object['type'] ) || 'Note' !== $object['type'] ) {
|
||||
return null;
|
||||
}
|
||||
$posts = Http::get_remote_object( $url, true );
|
||||
if ( is_wp_error( $posts ) ) {
|
||||
return $statuses;
|
||||
}
|
||||
|
||||
$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;
|
||||
$new_statuses = array_map(
|
||||
function ( $item ) use ( $account, $args ) {
|
||||
if ( $args['exclude_replies'] ) {
|
||||
if ( isset( $item['object']['inReplyTo'] ) && $item['object']['inReplyTo'] ) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return self::activity_to_status( $item, $account );
|
||||
},
|
||||
$posts['orderedItems']
|
||||
);
|
||||
$activitypub_statuses = array_merge( $activitypub_statuses, array_filter( $new_statuses ) );
|
||||
$url = $posts['next'];
|
||||
|
||||
if ( ! empty( $object['inReplyTo'] ) ) {
|
||||
$status->in_reply_to_id = $object['inReplyTo'];
|
||||
}
|
||||
if ( count( $activitypub_statuses ) >= $limit ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $object['visibility'] ) ) {
|
||||
$status->visibility = $object['visibility'];
|
||||
}
|
||||
return array_slice( $activitypub_statuses, 0, $limit );
|
||||
}
|
||||
|
||||
$status->uri = $object['url'];
|
||||
public static function api_get_replies( $context, $post_id, $url ) {
|
||||
$meta = Http::get_remote_object( $url, true );
|
||||
if ( is_wp_error( $meta ) || ! isset( $meta['replies']['first']['next'] ) ) {
|
||||
return $context;
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
$replies_url = $meta['replies']['first']['next'];
|
||||
$replies = Http::get_remote_object( $replies_url, true );
|
||||
if ( is_wp_error( $replies ) || ! isset( $replies['items'] ) ) {
|
||||
return $context;
|
||||
}
|
||||
|
||||
$attachment = array_merge( $default_attachment, $attachment );
|
||||
foreach ( $replies['items'] as $url ) {
|
||||
$response = Http::get( $url, true );
|
||||
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
|
||||
continue;
|
||||
}
|
||||
$status = json_decode( wp_remote_retrieve_body( $response ), true );
|
||||
if ( ! $status || is_wp_error( $status ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$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']
|
||||
);
|
||||
}
|
||||
$account = self::get_account_for_actor( $status['attributedTo'] );
|
||||
$status = self::activity_to_status( $status, $account );
|
||||
if ( $status ) {
|
||||
$context['descendants'][ $status->id ] = $status;
|
||||
}
|
||||
}
|
||||
|
||||
return $status;
|
||||
},
|
||||
$posts['orderedItems']
|
||||
);
|
||||
|
||||
return $activitypub_statuses;
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user