PluginProbe
ActivityPub / 5.7.0
ActivityPub v5.7.0
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / rest / class-inbox-controller.php

class-inbox-controller.php in ActivityPub 5.7.0, at includes/rest/class-inbox-controller.php

233 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Inbox_Controller file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Rest;
9
10 use Activitypub\Activity\Activity;
11
12 /**
13 * Inbox_Controller class.
14 *
15 * @author Matthias Pfefferle
16 *
17 * @see https://www.w3.org/TR/activitypub/#inbox
18 */
19 class Inbox_Controller extends \WP_REST_Controller {
20 /**
21 * The namespace of this controller's route.
22 *
23 * @var string
24 */
25 protected $namespace = ACTIVITYPUB_REST_NAMESPACE;
26
27 /**
28 * The base of this controller's route.
29 *
30 * @var string
31 */
32 protected $rest_base = 'inbox';
33
34 /**
35 * Register routes.
36 */
37 public function register_routes() {
38 \register_rest_route(
39 $this->namespace,
40 '/' . $this->rest_base,
41 array(
42 array(
43 'methods' => \WP_REST_Server::CREATABLE,
44 'callback' => array( $this, 'create_item' ),
45 'permission_callback' => array( 'Activitypub\Rest\Server', 'verify_signature' ),
46 'args' => array(
47 'id' => array(
48 'description' => 'The unique identifier for the activity.',
49 'type' => 'string',
50 'format' => 'uri',
51 'required' => true,
52 ),
53 'actor' => array(
54 'description' => 'The actor performing the activity.',
55 'type' => 'string',
56 'required' => true,
57 'sanitize_callback' => '\Activitypub\object_to_uri',
58 ),
59 'type' => array(
60 'description' => 'The type of the activity.',
61 'type' => 'string',
62 'required' => true,
63 ),
64 'object' => array(
65 'description' => 'The object of the activity.',
66 'required' => true,
67 'validate_callback' => function ( $param, $request, $key ) {
68 /**
69 * Filter the ActivityPub object validation.
70 *
71 * @param bool $validate The validation result.
72 * @param array $param The object data.
73 * @param object $request The request object.
74 * @param string $key The key.
75 */
76 return \apply_filters( 'activitypub_validate_object', true, $param, $request, $key );
77 },
78 ),
79 'to' => array(
80 'description' => 'The primary recipients of the activity.',
81 'type' => array( 'string', 'array' ),
82 'required' => false,
83 'sanitize_callback' => function ( $param ) {
84 if ( \is_string( $param ) ) {
85 $param = array( $param );
86 }
87
88 return $param;
89 },
90 ),
91 'cc' => array(
92 'description' => 'The secondary recipients of the activity.',
93 'type' => array( 'string', 'array' ),
94 'sanitize_callback' => function ( $param ) {
95 if ( \is_string( $param ) ) {
96 $param = array( $param );
97 }
98
99 return $param;
100 },
101 ),
102 'bcc' => array(
103 'description' => 'The private recipients of the activity.',
104 'type' => array( 'string', 'array' ),
105 'sanitize_callback' => function ( $param ) {
106 if ( \is_string( $param ) ) {
107 $param = array( $param );
108 }
109
110 return $param;
111 },
112 ),
113 ),
114 ),
115 'schema' => array( $this, 'get_item_schema' ),
116 )
117 );
118 }
119
120 /**
121 * The shared inbox.
122 *
123 * @param \WP_REST_Request $request The request object.
124 *
125 * @return \WP_REST_Response|\WP_Error Response object or WP_Error.
126 */
127 public function create_item( $request ) {
128 $data = $request->get_json_params();
129 $activity = Activity::init_from_array( $data );
130 $type = $request->get_param( 'type' );
131 $type = \strtolower( $type );
132
133 /**
134 * ActivityPub inbox action.
135 *
136 * @param array $data The data array.
137 * @param int|null $user_id The user ID.
138 * @param string $type The type of the activity.
139 * @param Activity|\WP_Error $activity The Activity object.
140 */
141 \do_action( 'activitypub_inbox', $data, null, $type, $activity );
142
143 /**
144 * ActivityPub inbox action for specific activity types.
145 *
146 * @param array $data The data array.
147 * @param int|null $user_id The user ID.
148 * @param Activity|\WP_Error $activity The Activity object.
149 */
150 \do_action( 'activitypub_inbox_' . $type, $data, null, $activity );
151
152 $response = \rest_ensure_response( array() );
153 $response->set_status( 202 );
154 $response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) );
155
156 return $response;
157 }
158
159 /**
160 * Retrieves the schema for a single inbox item, conforming to JSON Schema.
161 *
162 * @return array Item schema data.
163 */
164 public function get_item_schema() {
165 if ( $this->schema ) {
166 return $this->add_additional_fields_schema( $this->schema );
167 }
168
169 $schema = array(
170 '$schema' => 'https://json-schema.org/draft-04/schema#',
171 'title' => 'activity',
172 'type' => 'object',
173 'properties' => array(
174 '@context' => array(
175 'description' => 'The JSON-LD context for the activity.',
176 'type' => array( 'string', 'array', 'object' ),
177 'required' => true,
178 ),
179 'id' => array(
180 'description' => 'The unique identifier for the activity.',
181 'type' => 'string',
182 'format' => 'uri',
183 'required' => true,
184 ),
185 'type' => array(
186 'description' => 'The type of the activity.',
187 'type' => 'string',
188 'required' => true,
189 ),
190 'actor' => array(
191 'description' => 'The actor performing the activity.',
192 'type' => array( 'string', 'object' ),
193 'format' => 'uri',
194 'required' => true,
195 ),
196 'object' => array(
197 'description' => 'The object of the activity.',
198 'type' => array( 'string', 'object' ),
199 'required' => true,
200 ),
201 'to' => array(
202 'description' => 'The primary recipients of the activity.',
203 'type' => 'array',
204 'items' => array(
205 'type' => 'string',
206 'format' => 'uri',
207 ),
208 ),
209 'cc' => array(
210 'description' => 'The secondary recipients of the activity.',
211 'type' => 'array',
212 'items' => array(
213 'type' => 'string',
214 'format' => 'uri',
215 ),
216 ),
217 'bcc' => array(
218 'description' => 'The private recipients of the activity.',
219 'type' => 'array',
220 'items' => array(
221 'type' => 'string',
222 'format' => 'uri',
223 ),
224 ),
225 ),
226 );
227
228 $this->schema = $schema;
229
230 return $this->add_additional_fields_schema( $this->schema );
231 }
232 }
233