Skip to content

Commit 5616349

Browse files
authored
Add fetch_by_acct and fetch_by_various methods to Remote_Actors (#2235)
1 parent ca76268 commit 5616349

File tree

4 files changed

+407
-8
lines changed

4 files changed

+407
-8
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
Added support for fetching actors by account identifiers and improved reliability of actor retrieval.

includes/collection/class-remote-actors.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,29 @@ public static function get_by_uri( $actor_uri ) {
202202
return \get_post( $post_id );
203203
}
204204

205+
/**
206+
* Fetch a remote actor post by either actor URI or acct, fetching from remote if not found locally.
207+
*
208+
* @param string $uri_or_acct The actor URI or acct identifier.
209+
*
210+
* @return \WP_Post|\WP_Error Post object or WP_Error if not found.
211+
*/
212+
public static function fetch_by_various( $uri_or_acct ) {
213+
if ( \filter_var( $uri_or_acct, FILTER_VALIDATE_URL ) ) {
214+
return self::fetch_by_uri( $uri_or_acct );
215+
}
216+
217+
if ( preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $uri_or_acct ) ) {
218+
return self::fetch_by_acct( $uri_or_acct );
219+
}
220+
221+
return new \WP_Error(
222+
'activitypub_invalid_actor_identifier',
223+
'The actor identifier is not supported',
224+
array( 'status' => 400 )
225+
);
226+
}
227+
205228
/**
206229
* Lookup a remote actor post by actor URI (guid), fetching from remote if not found locally.
207230
*
@@ -239,6 +262,45 @@ public static function fetch_by_uri( $actor_uri ) {
239262
return \get_post( $post_id );
240263
}
241264

265+
/**
266+
* Fetch a remote actor post by acct, fetching from remote if not found locally.
267+
*
268+
* @param string $acct The acct identifier.
269+
*
270+
* @return \WP_Post|\WP_Error Post object or WP_Error if not found.
271+
*/
272+
public static function fetch_by_acct( $acct ) {
273+
$acct = Sanitize::webfinger( $acct );
274+
275+
// Check local DB for acct post meta.
276+
global $wpdb;
277+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
278+
$post_id = $wpdb->get_var(
279+
$wpdb->prepare(
280+
"SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_activitypub_acct' AND meta_value=%s",
281+
$acct
282+
)
283+
);
284+
285+
if ( $post_id ) {
286+
return \get_post( $post_id );
287+
}
288+
289+
$profile_uri = Webfinger::resolve( $acct );
290+
291+
if ( \is_wp_error( $profile_uri ) ) {
292+
return $profile_uri;
293+
}
294+
295+
$post = self::fetch_by_uri( $profile_uri );
296+
297+
if ( ! \is_wp_error( $post ) ) {
298+
\update_post_meta( $post->ID, '_activitypub_acct', $acct );
299+
}
300+
301+
return $post;
302+
}
303+
242304
/**
243305
* Store an error that occurred when sending an ActivityPub message to a follower.
244306
*

includes/functions.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function get_webfinger_resource( $user_id ) {
8585
*
8686
* @return array|\WP_Error The Actor profile as array or WP_Error on failure.
8787
*/
88-
function get_remote_metadata_by_actor( $actor, $cached = true ) {
88+
function get_remote_metadata_by_actor( $actor, $cached = true ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable, Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
8989
/**
9090
* Filters the metadata before it is retrieved from a remote actor.
9191
*
@@ -101,7 +101,13 @@ function get_remote_metadata_by_actor( $actor, $cached = true ) {
101101
return $pre;
102102
}
103103

104-
return Http::get_remote_object( $actor, $cached );
104+
$remote_actor = Remote_Actors::fetch_by_various( $actor );
105+
106+
if ( is_wp_error( $remote_actor ) ) {
107+
return $remote_actor;
108+
}
109+
110+
return json_decode( $remote_actor->post_content, true );
105111
}
106112

107113
/**

0 commit comments

Comments
 (0)