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