Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Mastodon Apps: Improve Reply Fetching #1118

Merged
merged 12 commits into from
Jan 8, 2025
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixed

## [Unreleased]

* Fetching replies for Enable Mastodon Apps
* Fediverse Preview: Fix links

## [4.6.0] - 2024-12-20

### Added
Expand Down
8 changes: 4 additions & 4 deletions includes/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
'ACTIVITYPUB_MASTODON_HTML_SANITIZER',
array(
'p' => array(),
'span' => array( 'class' ),
'span' => array_flip( array( 'class' ) ),
obenland marked this conversation as resolved.
Show resolved Hide resolved
'br' => array(),
'a' => array( 'href', 'rel', 'class' ),
'a' => array_flip( array( 'href', 'rel', 'class' ) ),
'del' => array(),
'pre' => array(),
'code' => array(),
Expand All @@ -46,8 +46,8 @@
'i' => array(),
'u' => array(),
'ul' => array(),
'ol' => array( 'start', 'reversed' ),
'li' => array( 'value' ),
'ol' => array_flip( array( 'start', 'reversed' ) ),
'li' => array_flip( array( 'value' ) ),
'blockquote' => array(),
'h1' => array(),
'h2' => array(),
Expand Down
41 changes: 29 additions & 12 deletions integration/class-enable-mastodon-apps.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function init() {
\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 );
\add_filter( 'mastodon_api_status_context', array( self::class, 'api_get_replies' ), 10, 3 );
\add_action( 'mastodon_api_update_credentials', array( self::class, 'api_update_credentials' ), 10, 2 );
}

Expand Down Expand Up @@ -800,20 +800,37 @@ public static function api_get_replies( $context, $post_id, $url ) {
return $context;
}

$replies_url = $meta['replies']['first']['next'];
$replies = Http::get_remote_object( $replies_url, true );
if ( is_wp_error( $replies ) || ! isset( $replies['items'] ) ) {
if ( ! empty( $meta['replies']['first']['items'] ) ) {
$replies = $meta['replies']['first'];
} elseif ( isset( $meta['replies']['first']['next'] ) ) {
$replies_url = $meta['replies']['first']['next'];
$replies = Http::get_remote_object( $replies_url, true );
if ( is_wp_error( $replies ) || ! isset( $replies['items'] ) ) {
return $context;
}
} else {
return $context;
}

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;
foreach ( $replies['items'] as $reply ) {
if ( isset( $reply['id'] ) && is_string( $reply['id'] ) && isset( $reply['content'] ) && is_string( $reply['content'] ) ) {
$status = $reply;
} else {
if ( is_string( $reply ) ) {
$url = $reply;
} elseif ( isset( $reply['url'] ) && is_string( $reply['url'] ) ) {
$url = $reply['url'];
} else {
continue;
}
$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;
}
}

$account = self::get_account_for_actor( $status['attributedTo'] );
Expand Down
5 changes: 5 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ For reasons of data protection, it is not possible to see the followers of other

== Changelog ==

= Unreleased =

* Fetching replies for Enable Mastodon Apps
* Fediverse Preview: Fix links

= 4.6.0 =

* Added: A filter to allow modifying the ActivityPub preview template
Expand Down
Loading