PluginProbe
ActivityPub / 9.0.2
ActivityPub v9.0.2
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 / integration / stream / class-connector.php

class-connector.php in ActivityPub 9.0.2, at integration/stream/class-connector.php

257 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Stream Connector integration file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Integration\Stream;
9
10 use Activitypub\Collection\Actors;
11
12 use function Activitypub\url_to_authorid;
13 use function Activitypub\url_to_commentid;
14
15 /**
16 * Stream Connector for ActivityPub.
17 *
18 * This class is a Stream Connector for the Stream plugin.
19 *
20 * @see https://wordpress.org/plugins/stream/
21 */
22 class Connector extends \WP_Stream\Connector {
23 /**
24 * Connector slug.
25 *
26 * @var string
27 */
28 public $name = 'activitypub';
29
30 /**
31 * Actions registered for this connector.
32 *
33 * @var array
34 */
35 public $actions = array(
36 'activitypub_handled_follow',
37 'activitypub_sent_to_inbox',
38 'activitypub_outbox_processing_complete',
39 'activitypub_outbox_processing_batch_complete',
40 );
41
42 /**
43 * Return translated connector label.
44 *
45 * @return string
46 */
47 public function get_label() {
48 return \__( 'ActivityPub', 'activitypub' );
49 }
50
51 /**
52 * Return translated context labels.
53 *
54 * @return array
55 */
56 public function get_context_labels() {
57 return array();
58 }
59
60 /**
61 * Return translated action labels.
62 *
63 * @return array
64 */
65 public function get_action_labels() {
66 return array(
67 'processed' => \__( 'Processed', 'activitypub' ),
68 );
69 }
70
71 /**
72 * Add action links to Stream drop row in admin list screen
73 *
74 * @filter wp_stream_action_links_{connector}
75 *
76 * @param array $links Previous links registered.
77 * @param Record $record Stream record.
78 *
79 * @return array Action links
80 */
81 public function action_links( $links, $record ) {
82 if ( 'processed' === $record->action ) {
83 $error = \json_decode( $record->get_meta( 'error', true ), true );
84
85 if ( $error ) {
86 $message = \sprintf(
87 '<details><summary>%1$s</summary><pre>%2$s</pre></details>',
88 \__( 'Inbox Error', 'activitypub' ),
89 \wp_json_encode( $error )
90 );
91
92 $links[ $message ] = '';
93 }
94
95 $debug = \json_decode( $record->get_meta( 'debug', true ), true );
96
97 if ( $debug ) {
98 $message = \sprintf(
99 '<details><summary>%1$s</summary><pre>%2$s</pre></details>',
100 \__( 'Debug', 'activitypub' ),
101 \wp_json_encode( $debug )
102 );
103
104 $links[ $message ] = '';
105 }
106 }
107
108 return $links;
109 }
110
111 /**
112 * Callback for activitypub_handled_follow.
113 *
114 * @param array $activity The ActivityPub activity data.
115 * @param int|int[] $user_ids The local user ID(s) of the followed actor(s).
116 * @param bool $success Whether the follow was handled successfully.
117 * @param \WP_Post|\WP_Error $context The remote actor/follower, or WP_Error on failure.
118 */
119 public function callback_activitypub_handled_follow( $activity, $user_ids, $success, $context ) {
120 // The hook passes an array of user IDs; Stream's log() builds a WP_User from this, so coerce to a single integer.
121 $user_id = (int) ( \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids );
122 $actor_url = \is_object( $context ) && ! \is_wp_error( $context ) ? $context->guid : $activity['actor'];
123
124 $this->log(
125 \sprintf(
126 // translators: %s is a URL.
127 \__( 'New Follower: %s', 'activitypub' ),
128 $actor_url
129 ),
130 array(
131 'activity' => \wp_json_encode( $activity, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
132 'remote_actor' => \wp_json_encode( $context, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ),
133 ),
134 null,
135 'notification',
136 'follow',
137 $user_id
138 );
139 }
140
141 /**
142 * Callback for activitypub_outbox_processing_complete.
143 *
144 * @param array $inboxes The inboxes.
145 * @param string $json The ActivityPub Activity JSON.
146 * @param int $actor_id The actor ID.
147 * @param int $outbox_item_id The Outbox item ID.
148 */
149 public function callback_activitypub_outbox_processing_complete( $inboxes, $json, $actor_id, $outbox_item_id ) {
150 $outbox_item = \get_post( $outbox_item_id );
151 $outbox_data = $this->prepare_outbox_data_for_response( $outbox_item );
152
153 $this->log(
154 \sprintf(
155 // translators: %s is a URL.
156 \__( 'Outbox processing complete: %s', 'activitypub' ),
157 $outbox_data['title']
158 ),
159 array(
160 'debug' => \wp_json_encode(
161 array(
162 'actor_id' => $actor_id,
163 'outbox_item_id' => $outbox_item_id,
164 )
165 ),
166 ),
167 $outbox_data['id'],
168 $outbox_data['type'],
169 'processed'
170 );
171 }
172
173 /**
174 * Callback for activitypub_outbox_processing_batch_complete.
175 *
176 * @param array $inboxes The inboxes.
177 * @param string $json The ActivityPub Activity JSON.
178 * @param int $actor_id The actor ID.
179 * @param int $outbox_item_id The Outbox item ID.
180 * @param int $batch_size The batch size.
181 * @param int $offset The offset.
182 */
183 public function callback_activitypub_outbox_processing_batch_complete( $inboxes, $json, $actor_id, $outbox_item_id, $batch_size, $offset ) {
184 $outbox_item = \get_post( $outbox_item_id );
185 $outbox_data = $this->prepare_outbox_data_for_response( $outbox_item );
186
187 $this->log(
188 // translators: %s is a URL.
189 \sprintf( \__( 'Outbox processing batch complete: %s', 'activitypub' ), $outbox_data['title'] ),
190 array(
191 'debug' => \wp_json_encode(
192 array(
193 'actor_id' => $actor_id,
194 'outbox_item_id' => $outbox_item_id,
195 'batch_size' => $batch_size,
196 'offset' => $offset,
197 )
198 ),
199 ),
200 $outbox_data['id'],
201 $outbox_data['type'],
202 'processed'
203 );
204 }
205
206 /**
207 * Get the title of the outbox object.
208 *
209 * @param \WP_Post $outbox_item The outbox item.
210 *
211 * @return array The title, object ID, and object type of the outbox object.
212 */
213 protected function prepare_outbox_data_for_response( $outbox_item ) {
214 $object_id = $outbox_item->ID;
215 $object_type = $outbox_item->post_type;
216 $object_title = $outbox_item->post_title;
217
218 $post_id = \url_to_postid( $outbox_item->post_title );
219 if ( $post_id ) {
220 $post = \get_post( $post_id );
221
222 $object_id = $post_id;
223 $object_type = $post->post_type;
224 $object_title = $post->post_title;
225 } else {
226 $comment_id = url_to_commentid( $outbox_item->post_title );
227 if ( $comment_id ) {
228 $comment = \get_comment( $comment_id );
229
230 $object_id = $comment_id;
231 $object_type = 'comments';
232 $object_title = $comment->comment_content;
233 } else {
234 $author_id = url_to_authorid( $outbox_item->post_title );
235 if ( null !== $author_id ) {
236 $object_id = $author_id;
237 $object_type = 'profiles';
238
239 if ( $author_id ) {
240 $object_title = \get_userdata( $author_id )->display_name;
241 } elseif ( Actors::BLOG_USER_ID === $author_id ) {
242 $object_title = \__( 'Blog User', 'activitypub' );
243 } elseif ( Actors::APPLICATION_USER_ID === $author_id ) {
244 $object_title = \__( 'Application User', 'activitypub' );
245 }
246 }
247 }
248 }
249
250 return array(
251 'id' => $object_id,
252 'type' => $object_type,
253 'title' => $object_title,
254 );
255 }
256 }
257