| @@ -1,10 +1,17 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * WebFinger integration file. | |
| 4 | + * | |
| 5 | + * @package Activitypub | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace Activitypub\Integration; |
| 3 | 9 | |
| 4 | -use Activitypub\Rest\Webfinger as Webfinger_Rest; | |
| 5 | -use Activitypub\Collection\Users as User_Collection; | |
| 10 | +use Activitypub\Collection\Actors; | |
| 6 | 11 | |
| 12 | +use function Activitypub\get_rest_url_by_path; | |
| 13 | + | |
| 7 | 14 | /** |
| 8 | 15 | * Compatibility with the WebFinger plugin |
| 9 | 16 | * |
| 10 | 17 | * @see https://wordpress.org/plugins/webfinger/ |
| @@ -10,60 +17,134 @@ | ||
| 10 | 17 | * @see https://wordpress.org/plugins/webfinger/ |
| 11 | 18 | */ |
| 12 | 19 | class Webfinger { |
| 13 | 20 | /** |
| 14 | - * Initialize the class, registering WordPress hooks | |
| 21 | + * Initialize the class, registering WordPress hooks. | |
| 15 | 22 | */ |
| 16 | 23 | public static function init() { |
| 17 | 24 | \add_filter( 'webfinger_user_data', array( self::class, 'add_user_discovery' ), 1, 3 ); |
| 18 | 25 | \add_filter( 'webfinger_data', array( self::class, 'add_pseudo_user_discovery' ), 1, 2 ); |
| 26 | + | |
| 27 | + \add_filter( 'webfinger_data', array( self::class, 'add_interaction_links' ), 11 ); | |
| 19 | 28 | } |
| 20 | 29 | |
| 21 | 30 | /** |
| 22 | - * Add WebFinger discovery links | |
| 31 | + * Add WebFinger discovery links. | |
| 23 | 32 | * |
| 24 | - * @param array $array the jrd array | |
| 25 | - * @param string $resource the WebFinger resource | |
| 26 | - * @param WP_User $user the WordPress user | |
| 33 | + * @param array $jrd The jrd array. | |
| 34 | + * @param string $uri The WebFinger resource. | |
| 35 | + * @param \WP_User $user The WordPress user. | |
| 27 | 36 | * |
| 28 | - * @return array the jrd array | |
| 37 | + * @return array The jrd array. | |
| 29 | 38 | */ |
| 30 | - public static function add_user_discovery( $array, $resource, $user ) { | |
| 31 | - $user = User_Collection::get_by_id( $user->ID ); | |
| 39 | + public static function add_user_discovery( $jrd, $uri, $user ) { | |
| 40 | + $user = Actors::get_by_id( $user->ID ); | |
| 32 | 41 | |
| 33 | 42 | if ( ! $user || is_wp_error( $user ) ) { |
| 34 | - return $array; | |
| 43 | + return $jrd; | |
| 35 | 44 | } |
| 36 | 45 | |
| 37 | - $array['subject'] = sprintf( 'acct:%s', $user->get_webfinger() ); | |
| 46 | + $jrd['subject'] = sprintf( 'acct:%s', $user->get_webfinger() ); | |
| 38 | 47 | |
| 39 | - $array['aliases'][] = $user->get_url(); | |
| 40 | - $array['aliases'][] = $user->get_alternate_url(); | |
| 48 | + $jrd['aliases'][] = $user->get_id(); | |
| 49 | + $jrd['aliases'][] = $user->get_url(); | |
| 50 | + $jrd['aliases'][] = $user->get_alternate_url(); | |
| 51 | + $jrd['aliases'] = array_unique( $jrd['aliases'] ); | |
| 52 | + $jrd['aliases'] = array_values( $jrd['aliases'] ); | |
| 41 | 53 | |
| 42 | - $array['links'][] = array( | |
| 54 | + $jrd['links'][] = array( | |
| 43 | 55 | 'rel' => 'self', |
| 44 | 56 | 'type' => 'application/activity+json', |
| 45 | - 'href' => $user->get_url(), | |
| 57 | + 'href' => $user->get_id(), | |
| 46 | 58 | ); |
| 47 | 59 | |
| 48 | - return $array; | |
| 60 | + return $jrd; | |
| 49 | 61 | } |
| 50 | 62 | |
| 51 | 63 | /** |
| 52 | - * Add WebFinger discovery links | |
| 64 | + * Add WebFinger discovery links. | |
| 53 | 65 | * |
| 54 | - * @param array $array the jrd array | |
| 55 | - * @param string $resource the WebFinger resource | |
| 56 | - * @param WP_User $user the WordPress user | |
| 66 | + * @param array $jrd The jrd array. | |
| 67 | + * @param string $uri The WebFinger resource. | |
| 57 | 68 | * |
| 58 | - * @return array the jrd array | |
| 69 | + * @return array|\WP_Error The jrd array or WP_Error. | |
| 59 | 70 | */ |
| 60 | - public static function add_pseudo_user_discovery( $array, $resource ) { | |
| 61 | - $user = Webfinger_Rest::get_profile( $resource ); | |
| 71 | + public static function add_pseudo_user_discovery( $jrd, $uri ) { | |
| 72 | + $user = Actors::get_by_resource( $uri ); | |
| 62 | 73 | |
| 63 | - if ( ! $user || is_wp_error( $user ) ) { | |
| 64 | - return $array; | |
| 74 | + if ( \is_wp_error( $user ) ) { | |
| 75 | + return $user; | |
| 65 | 76 | } |
| 66 | 77 | |
| 67 | - return $user; | |
| 78 | + $aliases = array( | |
| 79 | + $user->get_id(), | |
| 80 | + $user->get_url(), | |
| 81 | + $user->get_alternate_url(), | |
| 82 | + ); | |
| 83 | + | |
| 84 | + $aliases = array_unique( $aliases ); | |
| 85 | + $aliases = array_values( $aliases ); | |
| 86 | + | |
| 87 | + $profile = array( | |
| 88 | + 'subject' => sprintf( 'acct:%s', $user->get_webfinger() ), | |
| 89 | + 'aliases' => $aliases, | |
| 90 | + 'links' => array( | |
| 91 | + array( | |
| 92 | + 'rel' => 'self', | |
| 93 | + 'type' => 'application/activity+json', | |
| 94 | + 'href' => $user->get_id(), | |
| 95 | + ), | |
| 96 | + array( | |
| 97 | + 'rel' => 'http://webfinger.net/rel/profile-page', | |
| 98 | + 'type' => 'text/html', | |
| 99 | + 'href' => $user->get_id(), | |
| 100 | + ), | |
| 101 | + ), | |
| 102 | + ); | |
| 103 | + | |
| 104 | + if ( 'Person' !== $user->get_type() ) { | |
| 105 | + $profile['links'][0]['properties'] = array( | |
| 106 | + 'https://www.w3.org/ns/activitystreams#type' => $user->get_type(), | |
| 107 | + ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + return $profile; | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Add interaction links to the WebFinger data. | |
| 115 | + * | |
| 116 | + * @see https://codeberg.org/fediverse/fep/src/branch/main/fep/3b86/fep-3b86.md | |
| 117 | + * | |
| 118 | + * @param array $jrd The jrd array. | |
| 119 | + * | |
| 120 | + * @return array The jrd array. | |
| 121 | + */ | |
| 122 | + public static function add_interaction_links( $jrd ) { | |
| 123 | + if ( ! is_array( $jrd ) ) { | |
| 124 | + return $jrd; | |
| 125 | + } | |
| 126 | + | |
| 127 | + $jrd['links'][] = array( | |
| 128 | + 'rel' => 'http://ostatus.org/schema/1.0/subscribe', | |
| 129 | + 'template' => get_rest_url_by_path( 'interactions?uri={uri}' ), | |
| 130 | + ); | |
| 131 | + | |
| 132 | + /* | |
| 133 | + * Note: The parameter name `{inReplyTo}` is used here for all 'Create' intents, | |
| 134 | + * not just replies, to maintain compatibility with existing implementations and | |
| 135 | + * the FEP-3b86 specification. If a more generic parameter name is adopted in the | |
| 136 | + * future, this should be updated accordingly. | |
| 137 | + */ | |
| 138 | + $jrd['links'][] = array( | |
| 139 | + 'rel' => 'https://w3id.org/fep/3b86/Create', | |
| 140 | + 'template' => get_rest_url_by_path( 'interactions?uri={inReplyTo}&intent=create' ), | |
| 141 | + ); | |
| 142 | + | |
| 143 | + $jrd['links'][] = array( | |
| 144 | + 'rel' => 'https://w3id.org/fep/3b86/Follow', | |
| 145 | + 'template' => get_rest_url_by_path( 'interactions?uri={object}&intent=follow' ), | |
| 146 | + ); | |
| 147 | + | |
| 148 | + return $jrd; | |
| 68 | 149 | } |
| 69 | 150 | } |