Fix parser, to schedule using Event_Source object directly, not it's ID.
Some checks failed
PHP Code Checker / PHP Code Checker (pull_request) Successful in 47s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Failing after 58s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Failing after 57s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Failing after 53s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Failing after 57s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Failing after 56s
PHPUnit / PHPUnit – PHP 8.4 (pull_request) Failing after 55s

This commit is contained in:
André Menrath 2025-01-04 14:37:50 +01:00
parent f225db0f1e
commit 1c5f428289
2 changed files with 31 additions and 33 deletions

View file

@ -50,24 +50,24 @@ class Outbox_Parser {
} }
// Schedule the import of events via the outbox. // Schedule the import of events via the outbox.
return self::queue_importing_from_outbox( $outbox_url, $event_source->get_id(), 0 ); return self::queue_importing_from_outbox( $outbox_url, $event_source, 0 );
} }
/** /**
* Import events from an outbox: OrderedCollection or OrderedCollectionPage. * Import events from an outbox: OrderedCollection or OrderedCollectionPage.
* *
* @param string $url The url of the current page or outbox. * @param string $url The url of the current page or outbox.
* @param string $actor The ActivityPub ID/URL of the actor that owns the outbox. * @param Event_Source $event_source The event source that the outbox belongs to.
* @return void * @return void
*/ */
public static function import_events_from_outbox( $url, $actor ) { public static function import_events_from_outbox( $url, $event_source ) {
$outbox = self::fetch_outbox( $url ); $outbox = self::fetch_outbox( $url );
if ( ! $outbox ) { if ( ! $outbox ) {
return; return;
} }
$current_count = self::get_import_count( $actor ); $current_count = self::get_import_count( $event_source );
if ( $current_count >= self::MAX_EVENTS_TO_IMPORT ) { if ( $current_count >= self::MAX_EVENTS_TO_IMPORT ) {
return; return;
@ -77,12 +77,12 @@ class Outbox_Parser {
if ( isset( $outbox['orderedItems'] ) && is_array( $outbox['orderedItems'] ) ) { if ( isset( $outbox['orderedItems'] ) && is_array( $outbox['orderedItems'] ) ) {
$current_count += self::import_events_from_items( $current_count += self::import_events_from_items(
$outbox['orderedItems'], $outbox['orderedItems'],
$actor, $event_source,
self::MAX_EVENTS_TO_IMPORT - $current_count self::MAX_EVENTS_TO_IMPORT - $current_count
); );
} }
self::update_import_count( $actor, $current_count ); self::update_import_count( $event_source, $current_count );
// If the count is already exceeded abort here. // If the count is already exceeded abort here.
if ( $current_count >= self::MAX_EVENTS_TO_IMPORT ) { if ( $current_count >= self::MAX_EVENTS_TO_IMPORT ) {
@ -93,7 +93,7 @@ class Outbox_Parser {
$pagination_url = self::get_pagination_url( $outbox ); $pagination_url = self::get_pagination_url( $outbox );
if ( $pagination_url ) { if ( $pagination_url ) {
self::queue_importing_from_outbox( $pagination_url, $actor ); self::queue_importing_from_outbox( $pagination_url, $event_source );
} }
} }
@ -120,7 +120,7 @@ class Outbox_Parser {
* @param int $max_items The maximum number of items to parse. * @param int $max_items The maximum number of items to parse.
* @return array Parsed events from the collection. * @return array Parsed events from the collection.
*/ */
private static function parse_items_for_events( $items, $max_items ) { private static function parse_outbox_items_for_events( $items, $max_items ) {
$parsed_events = array(); $parsed_events = array();
foreach ( $items as $activity ) { foreach ( $items as $activity ) {
@ -157,13 +157,13 @@ class Outbox_Parser {
/** /**
* Import events from the items of an outbox. * Import events from the items of an outbox.
* *
* @param array $items The items/orderedItems as an associative array. * @param array $items The items/orderedItems as an associative array.
* @param string $actor The actor that owns the items. * @param Event_Source $event_source The Event Source the items belong to.
* @param int $limit The limit of how many events to save locally. * @param int $limit The limit of how many events to save locally.
* @return int The number of saved events (at least attempted). * @return int The number of saved events (at least attempted).
*/ */
private static function import_events_from_items( $items, $actor, $limit = -1 ) { private static function import_events_from_items( $items, $event_source, $limit = -1 ) {
$events = self::parse_items_for_events( $items, $limit ); $events = self::parse_outbox_items_for_events( $items, $limit );
$transmogrifier = Setup::get_transmogrifier(); $transmogrifier = Setup::get_transmogrifier();
@ -174,7 +174,7 @@ class Outbox_Parser {
$imported_count = 0; $imported_count = 0;
foreach ( $events as $event ) { foreach ( $events as $event ) {
$transmogrifier->save( $event, $actor ); $transmogrifier->save( $event, $event_source );
++$imported_count; ++$imported_count;
if ( $limit > 0 && $imported_count >= $limit ) { if ( $limit > 0 && $imported_count >= $limit ) {
break; break;
@ -187,14 +187,14 @@ class Outbox_Parser {
/** /**
* Schedule the import of events from an outbox OrderedCollection or OrderedCollectionPage. * Schedule the import of events from an outbox OrderedCollection or OrderedCollectionPage.
* *
* @param string $url The url of the current page or outbox. * @param string $url The url of the current page or outbox.
* @param string $actor The ActivityPub ID/URL of the actor that owns the outbox. * @param Event_Source $event_source The Event Source that owns the outbox.
* @param int $delay The delay of the current time in seconds. * @param int $delay The delay of the current time in seconds.
* @return void * @return void
*/ */
private static function queue_importing_from_outbox( $url, $actor, $delay = 10 ) { private static function queue_importing_from_outbox( $url, $event_source, $delay = 10 ) {
$hook = 'event_bridge_for_activitypub_import_events_from_outbox'; $hook = 'event_bridge_for_activitypub_import_events_from_outbox';
$args = array( $url, $actor ); $args = array( $url, $event_source );
if ( \wp_next_scheduled( $hook, $args ) ) { if ( \wp_next_scheduled( $hook, $args ) ) {
return; return;
@ -206,24 +206,22 @@ class Outbox_Parser {
/** /**
* Get the current import count for the actor. * Get the current import count for the actor.
* *
* @param string $actor The actor's ID/URL. * @param Event_Source $event_source The event source.
* @return int The current count of imported events. * @return int The current count of imported events.
*/ */
private static function get_import_count( $actor ) { private static function get_import_count( $event_source ) {
$post_id = Event_Source::get_by_id( $actor )->ID; return (int) \get_post_meta( $event_source->get__id(), '_event_bridge_for_activitypub_event_count', true );
return (int) \get_post_meta( $post_id, '_event_bridge_for_activitypub_event_count', true );
} }
/** /**
* Update the import count for the actor. * Update the import count for an event source..
* *
* @param string $actor The actor's ID/URL. * @param Event_Source $event_source The event source.
* @param int $count The new count of imported events. * @param int $count The new count of imported events.
* @return void * @return void
*/ */
private static function update_import_count( $actor, $count ) { private static function update_import_count( $event_source, $count ) {
$post_id = Event_Source::get_by_id( $actor )->ID; \update_post_meta( $event_source->get__id(), '_event_bridge_for_activitypub_event_count', $count );
\update_post_meta( $post_id, '_event_bridge_for_activitypub_event_count', $count );
} }
/** /**

View file

@ -139,10 +139,10 @@ class Test_Outbox_Parser extends \WP_UnitTestCase {
// The function we want to test is private, so we need a Reflection class. // The function we want to test is private, so we need a Reflection class.
$reflection = new \ReflectionClass( \Event_Bridge_For_ActivityPub\Outbox_Parser::class ); $reflection = new \ReflectionClass( \Event_Bridge_For_ActivityPub\Outbox_Parser::class );
$method = $reflection->getMethod( 'import_events_from_items' ); $method = $reflection->getMethod( 'import_events_from_items' );
$method->setAccessible( true ); $method->setAccessible( true );
$count = $method->invoke( null, $items, 'https://remote.example/@organizer' ); $count = $method->invoke( null, $items, 'https://remote.example/@organizer' );
$this->assertEquals( 2, $count ); $this->assertEquals( 2, $count );