Skip to content

Commit c4c4981

Browse files
Actors: Add utility function to get actor type (#1473)
* Actors: Add utility function to get actor type * Add changelog --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent c03e736 commit c4c4981

File tree

5 files changed

+36
-25
lines changed

5 files changed

+36
-25
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Introduced utility function to determine actor type based on user ID.

includes/collection/class-actors.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,22 @@ public static function get_collection() {
298298

299299
return $return;
300300
}
301+
302+
/**
303+
* Returns the actor type based on the user ID.
304+
*
305+
* @param int $user_id The user ID to check.
306+
* @return string The user type.
307+
*/
308+
public static function get_type_by_id( $user_id ) {
309+
if ( self::APPLICATION_USER_ID === $user_id ) {
310+
return 'application';
311+
}
312+
313+
if ( self::BLOG_USER_ID === $user_id ) {
314+
return 'blog';
315+
}
316+
317+
return 'user';
318+
}
301319
}

includes/collection/class-outbox.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,7 @@ class Outbox {
3333
* @return false|int|\WP_Error The added item or an error.
3434
*/
3535
public static function add( $activity_object, $activity_type, $user_id, $content_visibility = ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC ) {
36-
switch ( $user_id ) {
37-
case Actors::APPLICATION_USER_ID:
38-
$actor_type = 'application';
39-
break;
40-
case Actors::BLOG_USER_ID:
41-
$actor_type = 'blog';
42-
break;
43-
default:
44-
$actor_type = 'user';
45-
break;
46-
}
47-
36+
$actor_type = Actors::get_type_by_id( $user_id );
4837
$title = $activity_object->get_name() ?? $activity_object->get_content();
4938
$activitypub_object_id = $activity_object->get_id();
5039

includes/rest/class-outbox-controller.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,6 @@ public function get_items( $request ) {
114114
*/
115115
$activity_types = apply_filters( 'rest_activitypub_outbox_activity_types', array( 'Announce', 'Create', 'Like', 'Update' ) );
116116

117-
switch ( $user_id ) {
118-
case Actors::APPLICATION_USER_ID:
119-
$actor_type = 'application';
120-
break;
121-
case Actors::BLOG_USER_ID:
122-
$actor_type = 'blog';
123-
break;
124-
default:
125-
$actor_type = 'user';
126-
break;
127-
}
128-
129117
$args = array(
130118
'posts_per_page' => $request->get_param( 'per_page' ),
131119
'author' => $user_id > 0 ? $user_id : null,
@@ -137,7 +125,7 @@ public function get_items( $request ) {
137125
'meta_query' => array(
138126
array(
139127
'key' => '_activitypub_activity_actor',
140-
'value' => $actor_type,
128+
'value' => Actors::get_type_by_id( $user_id ),
141129
),
142130
),
143131
);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,16 @@ public function the_resource_provider() {
113113
array( 'http://example.org/@blogs/', 'WP_Error' ),
114114
);
115115
}
116+
117+
/**
118+
* Test get_type_by_id()
119+
*
120+
* @covers ::get_type_by_id
121+
*/
122+
public function test_get_type_by_id() {
123+
$this->assertSame( 'application', Actors::get_type_by_id( Actors::APPLICATION_USER_ID ) );
124+
$this->assertSame( 'blog', Actors::get_type_by_id( Actors::BLOG_USER_ID ) );
125+
$this->assertSame( 'user', Actors::get_type_by_id( 1 ) );
126+
$this->assertSame( 'user', Actors::get_type_by_id( 2 ) );
127+
}
116128
}

0 commit comments

Comments
 (0)