Skip to content

Commit 88675ea

Browse files
authored
Simple Remote-Reply endpoint. (#866)
* Simple Remote-Comment endpoint. implements #864 * move sanitize to args * fix PHPDoc * change from actions to filters thanks @akirk for your feedback! * add generic Interaction hook
1 parent b52c079 commit 88675ea

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

activitypub.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function rest_init() {
6262
Rest\Comment::init();
6363
Rest\Server::init();
6464
Rest\Collection::init();
65+
Rest\Interaction::init();
6566

6667
// load NodeInfo endpoints only if blog is public
6768
if ( is_blog_public() ) {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
namespace Activitypub\Rest;
3+
4+
use WP_REST_Response;
5+
use Activitypub\Http;
6+
7+
class Interaction {
8+
/**
9+
* Initialize the class, registering WordPress hooks
10+
*/
11+
public static function init() {
12+
self::register_routes();
13+
}
14+
15+
/**
16+
* Register routes
17+
*/
18+
public static function register_routes() {
19+
\register_rest_route(
20+
ACTIVITYPUB_REST_NAMESPACE,
21+
'/interactions',
22+
array(
23+
array(
24+
'methods' => \WP_REST_Server::READABLE,
25+
'callback' => array( self::class, 'get' ),
26+
'permission_callback' => '__return_true',
27+
'args' => array(
28+
'uri' => array(
29+
'type' => 'string',
30+
'required' => true,
31+
'sanitize_callback' => 'esc_url',
32+
),
33+
),
34+
),
35+
)
36+
);
37+
}
38+
39+
/**
40+
* Handle GET request
41+
*
42+
* @param WP_REST_Request $request The request object.
43+
*
44+
* @return wp_die|WP_REST_Response Redirect to the editor or die
45+
*/
46+
public static function get( $request ) {
47+
$uri = $request->get_param( 'uri' );
48+
$redirect_url = null;
49+
$object = Http::get_remote_object( $uri );
50+
51+
if (
52+
\is_wp_error( $object ) ||
53+
! isset( $object['type'] )
54+
) {
55+
\wp_die(
56+
\esc_html__(
57+
'The URL is not supported!',
58+
'activitypub'
59+
),
60+
400
61+
);
62+
}
63+
64+
if ( ! empty( $object['url'] ) ) {
65+
$uri = \esc_url( $object['url'] );
66+
}
67+
68+
switch ( $object['type'] ) {
69+
case 'Group':
70+
case 'Person':
71+
case 'Service':
72+
case 'Application':
73+
case 'Organization':
74+
$redirect_url = \apply_filters( 'activitypub_interactions_follow_url', $redirect_url, $uri, $object );
75+
break;
76+
default:
77+
$redirect_url = \admin_url( 'post-new.php?in_reply_to=' . $uri );
78+
$redirect_url = \apply_filters( 'activitypub_interactions_reply_url', $redirect_url, $uri, $object );
79+
}
80+
81+
// generic Interaction hook
82+
$redirect_url = \apply_filters( 'activitypub_interactions_url', $redirect_url, $uri, $object );
83+
84+
// check if hook is implemented
85+
if ( ! $redirect_url ) {
86+
\wp_die(
87+
esc_html__(
88+
'This Interaction type is not supported yet!',
89+
'activitypub'
90+
),
91+
400
92+
);
93+
}
94+
95+
return new WP_REST_Response(
96+
null,
97+
302,
98+
array(
99+
'Location' => \esc_url( $redirect_url ),
100+
)
101+
);
102+
}
103+
}

integration/class-webfinger.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use Activitypub\Rest\Webfinger as Webfinger_Rest;
55
use Activitypub\Collection\Users as User_Collection;
66

7+
use function Activitypub\get_rest_url_by_path;
8+
79
/**
810
* Compatibility with the WebFinger plugin
911
*
@@ -45,6 +47,11 @@ public static function add_user_discovery( $array, $resource, $user ) {
4547
'href' => $user->get_url(),
4648
);
4749

50+
$array['links'][] = array(
51+
'rel' => 'http://ostatus.org/schema/1.0/subscribe',
52+
'template' => get_rest_url_by_path( 'interactions?uri={uri}' ),
53+
);
54+
4855
return $array;
4956
}
5057

@@ -85,6 +92,10 @@ public static function add_pseudo_user_discovery( $array, $resource ) {
8592
'type' => 'text/html',
8693
'href' => $user->get_url(),
8794
),
95+
array(
96+
'rel' => 'http://ostatus.org/schema/1.0/subscribe',
97+
'template' => get_rest_url_by_path( 'interactions?uri={uri}' ),
98+
),
8899
),
89100
);
90101

0 commit comments

Comments
 (0)