From 8391e713c94771343f79ba39a7d2feb3b183d665 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Mon, 12 Dec 2022 16:36:22 +0100 Subject: [PATCH] Cache more metadata and webfinger results --- includes/class-mention.php | 6 +++++- includes/class-webfinger.php | 9 +++++---- includes/functions.php | 16 +++++++++++----- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/includes/class-mention.php b/includes/class-mention.php index 09c7046..886df58 100644 --- a/includes/class-mention.php +++ b/includes/class-mention.php @@ -35,9 +35,13 @@ class Mention { * @return string the final string */ public static function replace_with_links( $result ) { + error_log($result[0]); $metadata = \ActivityPub\get_remote_metadata_by_actor( $result[0] ); if ( ! is_wp_error( $metadata ) ) { - $username = $metadata['name']; + $username = ltrim( $result[0], '@'); + if ( ! empty( $metadata['name'] ) ) { + $username = $metadata['name']; + } if ( ! empty( $metadata['preferredUsername'] ) ) { $username = $metadata['preferredUsername']; } diff --git a/includes/class-webfinger.php b/includes/class-webfinger.php index 97b05b4..f83214c 100644 --- a/includes/class-webfinger.php +++ b/includes/class-webfinger.php @@ -40,7 +40,9 @@ class Webfinger { $url = \add_query_arg( 'resource', 'acct:' . ltrim( $account, '@' ), 'https://' . $m[1] . '/.well-known/webfinger' ); if ( ! \wp_http_validate_url( $url ) ) { - return new \WP_Error( 'invalid_webfinger_url', null, $url ); + $response = new \WP_Error( 'invalid_webfinger_url', null, $url ); + \set_transient( $transient_key, $response, HOUR_IN_SECONDS ); // Cache the error for a shorter period. + return $response; } // try to access author URL @@ -49,6 +51,7 @@ class Webfinger { array( 'headers' => array( 'Accept' => 'application/activity+json' ), 'redirection' => 0, + 'timeout' => 2, ) ); @@ -58,12 +61,10 @@ class Webfinger { return $link; } - $response_code = \wp_remote_retrieve_response_code( $response ); - $body = \wp_remote_retrieve_body( $response ); $body = \json_decode( $body, true ); - if ( ! isset( $body['links'] ) ) { + if ( empty( $body['links'] ) ) { $link = new \WP_Error( 'webfinger_url_invalid_response', null, $url ); \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period. return $link; diff --git a/includes/functions.php b/includes/functions.php index 3fb79a4..138c557 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -61,14 +61,14 @@ function safe_remote_post( $url, $body, $user_id ) { return $response; } -function safe_remote_get( $url, $user_id ) { +function safe_remote_get( $url, $user_id, $override_args = array() ) { $date = \gmdate( 'D, d M Y H:i:s T' ); $signature = \Activitypub\Signature::generate_signature( $user_id, 'get', $url, $date ); $wp_version = \get_bloginfo( 'version' ); $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) ); $args = array( - 'timeout' => 100, + 'timeout' => apply_filters( 'activitypub_remote_get_timeout', 100 ), 'limit_response_size' => 1048576, 'redirection' => 3, 'user-agent' => "$user_agent; ActivityPub", @@ -130,7 +130,9 @@ function get_remote_metadata_by_actor( $actor ) { } if ( ! \wp_http_validate_url( $actor ) ) { - return new \WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), $actor ); + $metadata = new \WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), $actor ); + \set_transient( $transient_key, $metadata, HOUR_IN_SECONDS ); // Cache the error for a shorter period. + return $metadata; } $user = \get_users( @@ -143,10 +145,14 @@ function get_remote_metadata_by_actor( $actor ) { // we just need any user to generate a request signature $user_id = \reset( $user ); - + $short_timeout = function() { + return 3; + }; + add_filter( 'activitypub_remote_get_timeout', $short_timeout ); $response = \Activitypub\safe_remote_get( $actor, $user_id ); - + remove_filter( 'activitypub_remote_get_timeout', $short_timeout ); if ( \is_wp_error( $response ) ) { + \set_transient( $transient_key, $response, HOUR_IN_SECONDS ); // Cache the error for a shorter period. return $response; }