Skip to content

Commit addd7dd

Browse files
authored
better handling when data is missing (#444)
* better handling when data is missing * WP_Error: add translation key and status * do not use cache for cleanup and update * better queries
1 parent 55e39a0 commit addd7dd

File tree

4 files changed

+72
-9
lines changed

4 files changed

+72
-9
lines changed

includes/activity/class-base-object.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ public function __call( $method, $params ) {
450450

451451
if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
452452
if ( ! $this->has( $var ) ) {
453-
return new WP_Error( 'invalid_key', 'Invalid key' );
453+
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
454454
}
455455

456456
return $this->$var;
@@ -492,7 +492,7 @@ public function to_string() {
492492
*/
493493
public function get( $key ) {
494494
if ( ! $this->has( $key ) ) {
495-
return new WP_Error( 'invalid_key', 'Invalid key' );
495+
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
496496
}
497497

498498
return call_user_func( array( $this, 'get_' . $key ) );
@@ -519,7 +519,7 @@ public function has( $key ) {
519519
*/
520520
public function set( $key, $value ) {
521521
if ( ! $this->has( $key ) ) {
522-
return new WP_Error( 'invalid_key', 'Invalid key' );
522+
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
523523
}
524524

525525
$this->$key = $value;
@@ -537,7 +537,7 @@ public function set( $key, $value ) {
537537
*/
538538
public function add( $key, $value ) {
539539
if ( ! $this->has( $key ) ) {
540-
return new WP_Error( 'invalid_key', 'Invalid key' );
540+
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
541541
}
542542

543543
if ( ! isset( $this->$key ) ) {
@@ -562,6 +562,10 @@ public function add( $key, $value ) {
562562
public static function init_from_json( $json ) {
563563
$array = \json_decode( $json, true );
564564

565+
if ( ! is_array( $array ) ) {
566+
$array = array();
567+
}
568+
565569
return self::init_from_array( $array );
566570
}
567571

@@ -573,6 +577,10 @@ public static function init_from_json( $json ) {
573577
* @return \Activitypub\Activity\Base_Object An Object built from the JSON string.
574578
*/
575579
public static function init_from_array( $array ) {
580+
if ( ! is_array( $array ) ) {
581+
return new WP_Error( 'invalid_array', __( 'Invalid array', 'activitypub' ), array( 'status' => 404 ) );
582+
}
583+
576584
$object = new static();
577585

578586
foreach ( $array as $key => $value ) {

includes/class-scheduler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static function update_followers() {
108108
$followers = Followers::get_outdated_followers();
109109

110110
foreach ( $followers as $follower ) {
111-
$meta = get_remote_metadata_by_actor( $follower->get_url(), true );
111+
$meta = get_remote_metadata_by_actor( $follower->get_url(), false );
112112

113113
if ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) {
114114
Followers::add_error( $follower->get__id(), $meta );
@@ -128,7 +128,7 @@ public static function cleanup_followers() {
128128
$followers = Followers::get_faulty_followers();
129129

130130
foreach ( $followers as $follower ) {
131-
$meta = get_remote_metadata_by_actor( $follower->get_url(), true );
131+
$meta = get_remote_metadata_by_actor( $follower->get_url(), false );
132132

133133
if ( is_tombstone( $meta ) ) {
134134
$follower->delete();

includes/collection/class-followers.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,17 @@ function( $post ) {
361361
public static function get_all_followers() {
362362
$args = array(
363363
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
364-
'meta_query' => array(),
364+
'meta_query' => array(
365+
'relation' => 'AND',
366+
array(
367+
'key' => 'activitypub_inbox',
368+
'compare' => 'EXISTS',
369+
),
370+
array(
371+
'key' => 'activitypub_actor_json',
372+
'compare' => 'EXISTS',
373+
),
374+
),
365375
);
366376
return self::get_followers( null, null, null, $args );
367377
}
@@ -380,10 +390,19 @@ public static function count_followers( $user_id ) {
380390
'fields' => 'ids',
381391
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
382392
'meta_query' => array(
393+
'relation' => 'AND',
383394
array(
384395
'key' => 'activitypub_user_id',
385396
'value' => $user_id,
386397
),
398+
array(
399+
'key' => 'activitypub_inbox',
400+
'compare' => 'EXISTS',
401+
),
402+
array(
403+
'key' => 'activitypub_actor_json',
404+
'compare' => 'EXISTS',
405+
),
387406
),
388407
)
389408
);
@@ -413,6 +432,7 @@ public static function get_inboxes( $user_id ) {
413432
'fields' => 'ids',
414433
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
415434
'meta_query' => array(
435+
'relation' => 'AND',
416436
array(
417437
'key' => 'activitypub_inbox',
418438
'compare' => 'EXISTS',
@@ -421,6 +441,11 @@ public static function get_inboxes( $user_id ) {
421441
'key' => 'activitypub_user_id',
422442
'value' => $user_id,
423443
),
444+
array(
445+
'key' => 'activitypub_inbox',
446+
'value' => '',
447+
'compare' => '!=',
448+
),
424449
),
425450
)
426451
);
@@ -463,7 +488,7 @@ public static function get_outdated_followers( $number = 50, $older_than = 86400
463488
'post_type' => self::POST_TYPE,
464489
'posts_per_page' => $number,
465490
'orderby' => 'modified',
466-
'order' => 'DESC',
491+
'order' => 'ASC',
467492
'post_status' => 'any', // 'any' includes 'trash
468493
'date_query' => array(
469494
array(
@@ -491,16 +516,35 @@ public static function get_outdated_followers( $number = 50, $older_than = 86400
491516
*
492517
* @return mixed The Term list of Followers, the format depends on $output.
493518
*/
494-
public static function get_faulty_followers( $number = 10 ) {
519+
public static function get_faulty_followers( $number = 20 ) {
495520
$args = array(
496521
'post_type' => self::POST_TYPE,
497522
'posts_per_page' => $number,
498523
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
499524
'meta_query' => array(
525+
'relation' => 'OR',
500526
array(
501527
'key' => 'activitypub_errors',
502528
'compare' => 'EXISTS',
503529
),
530+
array(
531+
'key' => 'activitypub_inbox',
532+
'compare' => 'NOT EXISTS',
533+
),
534+
array(
535+
'key' => 'activitypub_actor_json',
536+
'compare' => 'NOT EXISTS',
537+
),
538+
array(
539+
'key' => 'activitypub_inbox',
540+
'value' => '',
541+
'compare' => '=',
542+
),
543+
array(
544+
'key' => 'activitypub_actor_json',
545+
'value' => '',
546+
'compare' => '=',
547+
),
504548
),
505549
);
506550

includes/functions.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,14 @@ function site_supports_blocks() {
476476
*/
477477
return apply_filters( 'activitypub_site_supports_blocks', true );
478478
}
479+
480+
/**
481+
* Check if data is valid JSON.
482+
*
483+
* @param string $data The data to check.
484+
*
485+
* @return boolean True if the data is JSON, false otherwise.
486+
*/
487+
function is_json( $data ) {
488+
return \is_array( \json_decode( $data, true ) ) ? true : false;
489+
}

0 commit comments

Comments
 (0)