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

Outbox: Account for invalid json #1230

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/activity/class-base-object.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ public function add( $key, $value ) {
*
* @param string $json The JSON string.
*
* @return Base_Object An Object built from the JSON string.
* @return Base_Object|WP_Error An Object built from the JSON string or WP_Error when it's not a JSON string.
*/
public static function init_from_json( $json ) {
$array = \json_decode( $json, true );
Expand Down
24 changes: 6 additions & 18 deletions includes/collection/class-followers.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,8 @@ public static function get_followers_with_count( $user_id, $number = -1, $page =
$args = wp_parse_args( $args, $defaults );
$query = new WP_Query( $args );
$total = $query->found_posts;
$followers = array_map(
function ( $post ) {
return Follower::init_from_cpt( $post );
},
$query->get_posts()
);
$followers = array_map( array( Follower::class, 'init_from_cpt' ), $query->get_posts() );
$followers = array_filter( $followers );

return compact( 'followers', 'total' );
}
Expand Down Expand Up @@ -354,13 +350,9 @@ public static function get_outdated_followers( $number = 50, $older_than = 86400
);

$posts = new WP_Query( $args );
$items = array();

foreach ( $posts->get_posts() as $follower ) {
$items[] = Follower::init_from_cpt( $follower );
}
$items = array_map( array( Follower::class, 'init_from_cpt' ), $posts->get_posts() );

return $items;
return array_filter( $items );
}

/**
Expand Down Expand Up @@ -403,13 +395,9 @@ public static function get_faulty_followers( $number = 20 ) {
);

$posts = new WP_Query( $args );
$items = array();

foreach ( $posts->get_posts() as $follower ) {
$items[] = Follower::init_from_cpt( $follower );
}
$items = array_map( array( Follower::class, 'init_from_cpt' ), $posts->get_posts() );

return $items;
return array_filter( $items );
}

/**
Expand Down
7 changes: 6 additions & 1 deletion includes/model/class-follower.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,16 @@ public function get_shared_inbox() {
* Convert a Custom-Post-Type input to an Activitypub\Model\Follower.
*
* @param \WP_Post $post The post object.
* @return \Activitypub\Activity\Base_Object|WP_Error
* @return \Activitypub\Activity\Base_Object|false The Follower object or false on failure.
*/
public static function init_from_cpt( $post ) {
$actor_json = get_post_meta( $post->ID, '_activitypub_actor_json', true );
$object = self::init_from_json( $actor_json );

if ( is_wp_error( $object ) ) {
return false;
}

$object->set__id( $post->ID );
$object->set_id( $post->guid );
$object->set_name( $post->post_title );
Expand Down
12 changes: 12 additions & 0 deletions tests/includes/activity/class-test-base-object.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,16 @@ public function data_magic_add() {
array( array( 'value', 'value' ), array( 'value' ) ),
);
}

/**
* Test init_from_json method.
*
* @covers ::init_from_json
*/
public function test_init_from_json() {
$invalid_json = '{"@context":https:\/\/www.w3.org\/ns\/activitystreams",{"Hashtag":"as:Hashtag","sensitive":"as:sensitive"}],"id":"https:\/\/example.com\/2","type":"Note","content":"\u003Cp\u003EThis is another note\u003C\/p\u003E","contentMap":{"en":"\u003Cp\u003EThis is another note\u003C\/p\u003E"},"tag":[],"to":["https:\/\/www.w3.org\/ns\/activitystreams#Public"],"cc":[],"mediaType":"text\/html","sensitive":false}';
$base_object = Base_Object::init_from_json( $invalid_json );

$this->assertInstanceOf( 'WP_Error', $base_object );
}
}
20 changes: 20 additions & 0 deletions tests/includes/collection/class-test-followers.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ function ( $item ) {
$this->assertEquals( array( 'http://sally.example.org', 'https://example.org/author/doe', 'https://example.com/author/jon' ), $db_followers );
}

/**
* Tests get_followers with corrupted json.
*
* @covers ::get_followers
*/
public function test_get_followers_without_errors() {
$followers = array( 'https://example.com/author/jon', 'https://example.org/author/doe', 'http://sally.example.org' );

foreach ( $followers as $follower ) {
Followers::add_follower( 1, $follower );
}

$follower = Followers::get_follower( 1, 'https://example.org/author/doe' );
update_post_meta( $follower->get__id(), '_activitypub_actor_json', 'invalid json' );

$db_followers = Followers::get_followers( 1 );

$this->assertEquals( 2, \count( $db_followers ) );
}

/**
* Tests add_follower.
*
Expand Down
Loading