Skip to content

Commit 24e2de7

Browse files
pfefferleobenland
andauthored
Suggest/object (#1523)
* Update with activities * Remove comments as the function gets used recursively now --------- Co-authored-by: Konstantin Obenland <obenland@gmx.de>
1 parent 6ef67e3 commit 24e2de7

File tree

2 files changed

+53
-58
lines changed

2 files changed

+53
-58
lines changed

includes/collection/class-outbox.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace Activitypub\Collection;
99

10+
use Activitypub\Activity\Base_Object;
1011
use Activitypub\Dispatcher;
1112
use Activitypub\Scheduler;
1213
use Activitypub\Activity\Activity;
13-
use Activitypub\Activity\Generic_Object;
1414
use function Activitypub\add_to_outbox;
1515

1616
/**
@@ -296,34 +296,28 @@ public static function maybe_get_activity( $outbox_item ) {
296296
/**
297297
* Get the object ID of an activity.
298298
*
299-
* @param Generic_Object $data The activity object.
299+
* @param Activity|Base_Object|string $data The activity object.
300300
*
301301
* @return string The object ID.
302302
*/
303303
private static function get_object_id( $data ) {
304-
// If the object is an array, convert it to a Generic_Object.
305-
if ( is_array( $data->get_object() ) ) {
306-
$data->set_object( Generic_Object::init_from_array( $data->get_object() ) );
307-
}
304+
$object = $data->get_object();
308305

309-
// Most common.
310-
if ( is_object( $data->get_object() ) ) {
311-
return self::get_object_id( $data->get_object() );
306+
if ( is_object( $object ) ) {
307+
return self::get_object_id( $object );
312308
}
313309

314-
// Rare.
315-
if ( is_string( $data->get_object() ) ) {
316-
return $data->get_object();
310+
if ( is_string( $object ) ) {
311+
return $object;
317312
}
318313

319-
// Exceptional.
320314
return $data->get_id() ?? $data->get_actor();
321315
}
322316

323317
/**
324318
* Get the title of an activity recursively.
325319
*
326-
* @param \Activitypub\Activity\Base_Object $activity_object The activity object.
320+
* @param Base_Object $activity_object The activity object.
327321
*
328322
* @return string The title.
329323
*/
@@ -340,7 +334,7 @@ private static function get_object_title( $activity_object ) {
340334

341335
$title = $activity_object->get_name() ?? $activity_object->get_content();
342336

343-
if ( ! $title && $activity_object->get_object() instanceof \Activitypub\Activity\Base_Object ) {
337+
if ( ! $title && $activity_object->get_object() instanceof Base_Object ) {
344338
$title = $activity_object->get_object()->get_name() ?? $activity_object->get_object()->get_content();
345339
}
346340

tests/includes/collection/class-test-outbox.php

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,10 @@ public function test_delete_invalidates_all_activities() {
289289
* @dataProvider data_provider_get_object_id
290290
* @covers ::get_object_id
291291
*
292-
* @param array $activity_data The activity data to test.
293-
* @param string $expected The expected object ID.
292+
* @param Activity $activity The activity data to test.
293+
* @param string $expected The expected object ID.
294294
*/
295-
public function test_get_object_id( $activity_data, $expected ) {
296-
$activity = Generic_Object::init_from_array( $activity_data );
297-
295+
public function test_get_object_id( $activity, $expected ) {
298296
// Get the object ID using reflection since it's a private method.
299297
$get_object_id = new \ReflectionMethod( Outbox::class, 'get_object_id' );
300298
$get_object_id->setAccessible( true );
@@ -310,64 +308,67 @@ public function test_get_object_id( $activity_data, $expected ) {
310308
* @return array
311309
*/
312310
public function data_provider_get_object_id() {
311+
$create_with_id = Activity::init_from_array(
312+
array(
313+
'type' => 'Create',
314+
'object' => array(
315+
'type' => 'Note',
316+
'id' => 'https://example.com/note/123',
317+
),
318+
)
319+
);
320+
$create_no_id = Activity::init_from_array(
321+
array(
322+
'type' => 'Create',
323+
'object' => array(
324+
'type' => 'Note',
325+
'content' => 'Test content',
326+
),
327+
)
328+
);
329+
313330
return array(
314331
'object is a string' => array(
315-
'activity' => array(
316-
'type' => 'Create',
317-
'object' => 'https://example.com/note/123',
332+
'activity' => Activity::init_from_array(
333+
array(
334+
'type' => 'Create',
335+
'object' => 'https://example.com/note/123',
336+
)
318337
),
319338
'expected' => 'https://example.com/note/123',
320339
),
321340
'object is an object with id' => array(
322-
'activity' => array(
323-
'type' => 'Create',
324-
'object' => array(
325-
'type' => 'Note',
326-
'id' => 'https://example.com/note/123',
327-
),
328-
),
341+
'activity' => $create_with_id,
329342
'expected' => 'https://example.com/note/123',
330343
),
331344
'object is an object without id' => array(
332-
'activity' => array(
333-
'type' => 'Create',
334-
'object' => array(
335-
'type' => 'Note',
336-
'content' => 'Test content',
337-
),
338-
),
345+
'activity' => $create_no_id,
339346
'expected' => null, // Will use activity ID as fallback.
340347
),
341348
'nested object with id' => array(
342-
'activity' => array(
343-
'type' => 'Create',
344-
'object' => array(
345-
'type' => 'Article',
346-
'object' => array(
347-
'type' => 'Note',
348-
'id' => 'https://example.com/note/123',
349-
),
350-
),
349+
'activity' => Activity::init_from_array(
350+
array(
351+
'type' => 'Announce',
352+
'object' => $create_with_id,
353+
)
351354
),
352355
'expected' => 'https://example.com/note/123',
353356
),
354357
'nested object without id' => array(
355-
'activity' => array(
356-
'type' => 'Create',
357-
'object' => array(
358-
'type' => 'Article',
359-
'object' => array(
360-
'type' => 'Note',
361-
'content' => 'Test content',
362-
),
363-
),
358+
'activity' => Activity::init_from_array(
359+
array(
360+
'type' => 'Announce',
361+
'object' => $create_no_id,
362+
)
364363
),
365364
'expected' => null, // Will use activity ID as fallback.
366365
),
367366
'activity with no object' => array(
368-
'activity' => array(
369-
'type' => 'Delete',
370-
'actor' => 'https://example.com/user/1',
367+
'activity' => Activity::init_from_array(
368+
array(
369+
'type' => 'Delete',
370+
'actor' => 'https://example.com/user/1',
371+
)
371372
),
372373
'expected' => 'https://example.com/user/1', // Will use actor as fallback.
373374
),

0 commit comments

Comments
 (0)