Skip to content

Commit 134dddb

Browse files
akirkobenland
andauthored
Enable Mastodon Apps: Improve Reply Fetching (#1118)
Co-authored-by: Konstantin Obenland <[email protected]>
1 parent 1293380 commit 134dddb

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
### Fixed
1919

2020
* Undefined array key warnings in various places
21+
* Fetching replies from the same instance for Enable Mastodon Apps
2122
* Image captions not being included in the ActivityPub representation when the image is attached to the post
2223

2324
## [4.6.0] - 2024-12-20

includes/constants.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@
3434
'ACTIVITYPUB_MASTODON_HTML_SANITIZER',
3535
array(
3636
'p' => array(),
37-
'span' => array( 'class' ),
37+
'span' => array( 'class' => true ),
3838
'br' => array(),
39-
'a' => array( 'href', 'rel', 'class' ),
39+
'a' => array(
40+
'href' => true,
41+
'rel' => true,
42+
'class' => true,
43+
),
4044
'del' => array(),
4145
'pre' => array(),
4246
'code' => array(),
@@ -46,8 +50,11 @@
4650
'i' => array(),
4751
'u' => array(),
4852
'ul' => array(),
49-
'ol' => array( 'start', 'reversed' ),
50-
'li' => array( 'value' ),
53+
'ol' => array(
54+
'start' => true,
55+
'reversed' => true,
56+
),
57+
'li' => array( 'value' => true ),
5158
'blockquote' => array(),
5259
'h1' => array(),
5360
'h2' => array(),

integration/class-enable-mastodon-apps.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function init() {
4242
\add_filter( 'mastodon_api_search', array( self::class, 'api_search_by_url' ), 40, 2 );
4343
\add_filter( 'mastodon_api_get_posts_query_args', array( self::class, 'api_get_posts_query_args' ) );
4444
\add_filter( 'mastodon_api_statuses', array( self::class, 'api_statuses_external' ), 10, 2 );
45-
\add_filter( 'mastodon_api_status_context', array( self::class, 'api_get_replies' ), 10, 23 );
45+
\add_filter( 'mastodon_api_status_context', array( self::class, 'api_get_replies' ), 10, 3 );
4646
\add_action( 'mastodon_api_update_credentials', array( self::class, 'api_update_credentials' ), 10, 2 );
4747
}
4848

@@ -800,20 +800,37 @@ public static function api_get_replies( $context, $post_id, $url ) {
800800
return $context;
801801
}
802802

803-
$replies_url = $meta['replies']['first']['next'];
804-
$replies = Http::get_remote_object( $replies_url, true );
805-
if ( is_wp_error( $replies ) || ! isset( $replies['items'] ) ) {
803+
if ( ! empty( $meta['replies']['first']['items'] ) ) {
804+
$replies = $meta['replies']['first'];
805+
} elseif ( isset( $meta['replies']['first']['next'] ) ) {
806+
$replies_url = $meta['replies']['first']['next'];
807+
$replies = Http::get_remote_object( $replies_url, true );
808+
if ( is_wp_error( $replies ) || ! isset( $replies['items'] ) ) {
809+
return $context;
810+
}
811+
} else {
806812
return $context;
807813
}
808814

809-
foreach ( $replies['items'] as $url ) {
810-
$response = Http::get( $url, true );
811-
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
812-
continue;
813-
}
814-
$status = json_decode( wp_remote_retrieve_body( $response ), true );
815-
if ( ! $status || is_wp_error( $status ) ) {
816-
continue;
815+
foreach ( $replies['items'] as $reply ) {
816+
if ( isset( $reply['id'] ) && is_string( $reply['id'] ) && isset( $reply['content'] ) && is_string( $reply['content'] ) ) {
817+
$status = $reply;
818+
} else {
819+
if ( is_string( $reply ) ) {
820+
$url = $reply;
821+
} elseif ( isset( $reply['url'] ) && is_string( $reply['url'] ) ) {
822+
$url = $reply['url'];
823+
} else {
824+
continue;
825+
}
826+
$response = Http::get( $url, true );
827+
if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) !== 200 ) {
828+
continue;
829+
}
830+
$status = json_decode( wp_remote_retrieve_body( $response ), true );
831+
if ( ! $status || is_wp_error( $status ) ) {
832+
continue;
833+
}
817834
}
818835

819836
$account = self::get_account_for_actor( $status['attributedTo'] );

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ For reasons of data protection, it is not possible to see the followers of other
137137
* Added: A filter to make custom comment types manageable in WP.com Calypso
138138
* Changed: Hide ActivityPub post meta keys from the custom Fields UI
139139
* Fixed: Undefined array key warnings in various places
140+
* Fixed: Fetching replies from the same instance for Enable Mastodon Apps
140141
* Fixed: Image captions not being included in the ActivityPub representation when the image is attached to the post
141142

142143
= 4.6.0 =

0 commit comments

Comments
 (0)