removed output formatting

This commit is contained in:
Matthias Pfefferle 2023-05-11 09:46:26 +02:00
parent 47b1b10955
commit b803914180
7 changed files with 41 additions and 57 deletions

View file

@ -29,8 +29,6 @@ function init() {
\define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) );
\define( 'ACTIVITYPUB_OBJECT', 'ACTIVITYPUB_OBJECT' );
Migration::init(); Migration::init();
Activity_Dispatcher::init(); Activity_Dispatcher::init();
Activitypub::init(); Activitypub::init();

View file

@ -95,7 +95,7 @@ class Scheduler {
* @return void * @return void
*/ */
public static function update_followers() { public static function update_followers() {
$followers = Followers::get_outdated_followers( ACTIVITYPUB_OBJECT ); $followers = Followers::get_outdated_followers();
foreach ( $followers as $follower ) { foreach ( $followers as $follower ) {
$meta = get_remote_metadata_by_actor( $follower->get_actor() ); $meta = get_remote_metadata_by_actor( $follower->get_actor() );
@ -116,7 +116,7 @@ class Scheduler {
* @return void * @return void
*/ */
public static function cleanup_followers() { public static function cleanup_followers() {
$followers = Followers::get_faulty_followers( ACTIVITYPUB_OBJECT ); $followers = Followers::get_faulty_followers();
foreach ( $followers as $follower ) { foreach ( $followers as $follower ) {
$meta = get_remote_metadata_by_actor( $follower->get_actor() ); $meta = get_remote_metadata_by_actor( $follower->get_actor() );

View file

@ -316,7 +316,7 @@ class Followers {
* *
* @return array The Term list of Followers, the format depends on $output * @return array The Term list of Followers, the format depends on $output
*/ */
public static function get_followers( $user_id, $output = ARRAY_N, $number = null, $offset = null, $args = array() ) { public static function get_followers( $user_id, $number = null, $offset = null, $args = array() ) {
$defaults = array( $defaults = array(
'taxonomy' => self::TAXONOMY, 'taxonomy' => self::TAXONOMY,
'hide_empty' => false, 'hide_empty' => false,
@ -329,25 +329,13 @@ class Followers {
$args = wp_parse_args( $args, $defaults ); $args = wp_parse_args( $args, $defaults );
$terms = new WP_Term_Query( $args ); $terms = new WP_Term_Query( $args );
$items = array(); $items = array();
// change output format foreach ( $terms->get_terms() as $follower ) {
switch ( $output ) { $items[] = new Follower( $follower->name ); // phpcs:ignore
case ACTIVITYPUB_OBJECT:
foreach ( $terms->get_terms() as $follower ) {
$items[] = new Follower( $follower->name ); // phpcs:ignore
}
return $items;
case OBJECT:
return $terms->get_terms();
case ARRAY_N:
default:
foreach ( $terms->get_terms() as $follower ) {
$items[] = $follower->name; // phpcs:ignore
}
return $items;
} }
return $items;
} }
/** /**
@ -414,7 +402,7 @@ class Followers {
* *
* @return mixed The Term list of Followers, the format depends on $output. * @return mixed The Term list of Followers, the format depends on $output.
*/ */
public static function get_outdated_followers( $output = ARRAY_N, $number = 50, $older_than = 604800 ) { public static function get_outdated_followers( $number = 50, $older_than = 604800 ) {
$args = array( $args = array(
'taxonomy' => self::TAXONOMY, 'taxonomy' => self::TAXONOMY,
'number' => $number, 'number' => $number,
@ -432,25 +420,13 @@ class Followers {
); );
$terms = new WP_Term_Query( $args ); $terms = new WP_Term_Query( $args );
$items = array(); $items = array();
// change output format foreach ( $terms->get_terms() as $follower ) {
switch ( $output ) { $items[] = new Follower( $follower->name ); // phpcs:ignore
case ACTIVITYPUB_OBJECT:
foreach ( $terms->get_terms() as $follower ) {
$items[] = new Follower( $follower->name ); // phpcs:ignore
}
return $items;
case OBJECT:
return $terms->get_terms();
case ARRAY_N:
default:
foreach ( $terms->get_terms() as $follower ) {
$items[] = $follower->name; // phpcs:ignore
}
return $items;
} }
return $items;
} }
/** /**
@ -461,7 +437,7 @@ class Followers {
* *
* @return mixed The Term list of Followers, the format depends on $output. * @return mixed The Term list of Followers, the format depends on $output.
*/ */
public static function get_faulty_followers( $output = ARRAY_N, $number = 10 ) { public static function get_faulty_followers( $number = 10 ) {
$args = array( $args = array(
'taxonomy' => self::TAXONOMY, 'taxonomy' => self::TAXONOMY,
'number' => $number, 'number' => $number,
@ -474,24 +450,12 @@ class Followers {
); );
$terms = new WP_Term_Query( $args ); $terms = new WP_Term_Query( $args );
$items = array(); $items = array();
// change output format foreach ( $terms->get_terms() as $follower ) {
switch ( $output ) { $items[] = new Follower( $follower->name ); // phpcs:ignore
case ACTIVITYPUB_OBJECT:
foreach ( $terms->get_terms() as $follower ) {
$items[] = new Follower( $follower->name ); // phpcs:ignore
}
return $items;
case OBJECT:
return $terms->get_terms();
case ARRAY_N:
default:
foreach ( $terms->get_terms() as $follower ) {
$items[] = $follower->name; // phpcs:ignore
}
return $items;
} }
return $items;
} }
} }

View file

@ -159,6 +159,15 @@ class Follower {
} }
} }
/**
* Magic function to return the Actor-URL when the Object is used as a string
*
* @return string
*/
public function __toString() {
return $this->get_actor();
}
/** /**
* Prefill the Object with the meta data. * Prefill the Object with the meta data.
* *

View file

@ -81,7 +81,13 @@ class Followers {
$json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/followers" ); // phpcs:ignore $json->partOf = \get_rest_url( null, "/activitypub/1.0/users/$user_id/followers" ); // phpcs:ignore
$json->first = $json->partOf; // phpcs:ignore $json->first = $json->partOf; // phpcs:ignore
$json->totalItems = FollowerCollection::count_followers( $user_id ); // phpcs:ignore $json->totalItems = FollowerCollection::count_followers( $user_id ); // phpcs:ignore
$json->orderedItems = FollowerCollection::get_followers( $user_id, ARRAY_N ); // phpcs:ignore // phpcs:ignore
$json->orderedItems = array_map(
function( $item ) {
return $item->get_actor();
},
FollowerCollection::get_followers( $user_id )
);
$response = new WP_REST_Response( $json, 200 ); $response = new WP_REST_Response( $json, 200 );
$response->header( 'Content-Type', 'application/activity+json' ); $response->header( 'Content-Type', 'application/activity+json' );

View file

@ -35,7 +35,7 @@ class Followers extends WP_List_Table {
$page_num = $this->get_pagenum(); $page_num = $this->get_pagenum();
$per_page = 20; $per_page = 20;
$follower = FollowerCollection::get_followers( \get_current_user_id(), ACTIVITYPUB_OBJECT, $per_page, ( $page_num - 1 ) * $per_page ); $follower = FollowerCollection::get_followers( \get_current_user_id(), $per_page, ( $page_num - 1 ) * $per_page );
$counter = FollowerCollection::count_followers( \get_current_user_id() ); $counter = FollowerCollection::count_followers( \get_current_user_id() );
$this->items = array(); $this->items = array();

View file

@ -58,6 +58,13 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase {
$this->assertEquals( 3, \count( $db_followers ) ); $this->assertEquals( 3, \count( $db_followers ) );
$db_followers = array_map(
function( $item ) {
return $item->get_actor();
},
$db_followers
);
$this->assertSame( array( 'https://example.com/author/jon', 'https://example.org/author/doe', 'http://sally.example.org' ), $db_followers ); $this->assertSame( array( 'https://example.com/author/jon', 'https://example.org/author/doe', 'http://sally.example.org' ), $db_followers );
} }