PluginProbe
ActivityPub / 3.2.4
ActivityPub v3.2.4
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
← All changes | includes/handler/class-update.php +42 -115 8.2.03.2.4 View file →
@@ -1,19 +1,11 @@
1 1 <?php
2 -/**
3 - * Update handler file.
4 - *
5 - * @package Activitypub
6 - */
7 -
8 2 namespace Activitypub\Handler;
9 3
4 +use WP_Error;
10 5 use Activitypub\Collection\Interactions;
11 -use Activitypub\Collection\Remote_Actors;
12 -use Activitypub\Collection\Remote_Posts;
13 -use Activitypub\Http;
14 6
15 -use function Activitypub\is_activity_reply;
7 +use function Activitypub\get_remote_metadata_by_actor;
16 8
17 9 /**
18 10 * Handle Update requests.
19 11 */
@@ -18,43 +10,38 @@
18 10 * Handle Update requests.
19 11 */
20 12 class Update {
21 13 /**
22 - * Initialize the class, registering WordPress hooks.
14 + * Initialize the class, registering WordPress hooks
23 15 */
24 16 public static function init() {
25 - \add_action( 'activitypub_handled_inbox_update', array( self::class, 'handle_update' ), 10, 3 );
17 + \add_action(
18 + 'activitypub_inbox_update',
19 + array( self::class, 'handle_update' )
20 + );
26 21 }
27 22
28 23 /**
29 - * Handle "Update" requests.
24 + * Handle "Update" requests
30 25 *
31 - * @param array $activity The Activity object.
32 - * @param int[] $user_ids The user IDs. Always null for Update activities.
33 - * @param \Activitypub\Activity\Activity $activity_object The activity object. Default null.
26 + * @param array $array The activity-object
27 + * @param int $user_id The id of the local blog-user
34 28 */
35 - public static function handle_update( $activity, $user_ids, $activity_object ) {
36 - $object_type = $activity['object']['type'] ?? '';
29 + public static function handle_update( $array ) {
30 + $object_type = isset( $array['object']['type'] ) ? $array['object']['type'] : '';
37 31
38 32 switch ( $object_type ) {
39 - /*
40 - * Actor Types.
41 - *
42 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
43 - */
33 + // Actor Types
34 + // @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
44 35 case 'Person':
45 36 case 'Group':
46 37 case 'Organization':
47 38 case 'Service':
48 39 case 'Application':
49 - self::update_actor( $activity, $user_ids );
40 + self::update_actor( $array );
50 41 break;
51 -
52 - /*
53 - * Object and Link Types.
54 - *
55 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
56 - */
42 + // Object and Link Types
43 + // @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
57 44 case 'Note':
58 45 case 'Article':
59 46 case 'Image':
60 47 case 'Audio':
@@ -60,16 +47,12 @@
60 47 case 'Audio':
61 48 case 'Video':
62 49 case 'Event':
63 50 case 'Document':
64 - self::update_object( $activity, $user_ids, $activity_object );
51 + self::update_interaction( $array );
65 52 break;
66 -
67 - /*
68 - * Minimal Activity.
69 - *
70 - * @see https://www.w3.org/TR/activitystreams-core/#example-1
71 - */
53 + // Minimal Activity
54 + // @see https://www.w3.org/TR/activitystreams-core/#example-1
72 55 default:
73 56 break;
74 57 }
75 58 }
@@ -74,95 +57,39 @@
74 57 }
75 58 }
76 59
77 60 /**
78 - * Update an Object.
61 + * Update an Interaction
79 62 *
80 - * @param array $activity The Activity object.
81 - * @param int[]|null $user_ids The user IDs. Always null for Update activities.
82 - * @param \Activitypub\Activity\Activity $activity_object The activity object. Default null.
63 + * @param array $activity The activity-object
64 + * @param int $user_id The id of the local blog-user
65 + *
66 + * @return void
83 67 */
84 - public static function update_object( $activity, $user_ids, $activity_object ) {
85 - $result = new \WP_Error( 'activitypub_update_failed', 'Update failed' );
86 - $updated = true;
68 + public static function update_interaction( $activity ) {
69 + $commentdata = Interactions::update_comment( $activity );
70 + $reaction = null;
87 71
88 - // Check for private and/or direct messages.
89 - if ( is_activity_reply( $activity ) ) {
90 - $comment_data = Interactions::update_comment( $activity );
91 -
92 - if ( false === $comment_data ) {
93 - $updated = false;
94 - } elseif ( ! empty( $comment_data['comment_ID'] ) ) {
95 - $result = \get_comment( $comment_data['comment_ID'] );
96 - }
97 - } elseif ( \get_option( 'activitypub_create_posts', false ) ) {
98 - $result = Remote_Posts::update( $activity, $user_ids );
99 -
100 - if ( \is_wp_error( $result ) && 'activitypub_post_not_found' === $result->get_error_code() ) {
101 - $updated = false;
102 - }
72 + if ( ! empty( $commentdata['comment_ID'] ) ) {
73 + $state = 1;
74 + $reaction = \get_comment( $commentdata['comment_ID'] );
75 + } else {
76 + $state = $commentdata;
103 77 }
104 78
105 - // There is no object to update, try to trigger create instead.
106 - if ( ! $updated ) {
107 - return Create::handle_create( $activity, $user_ids, $activity_object );
108 - }
109 -
110 - $success = ( $result && ! \is_wp_error( $result ) );
111 -
112 - /**
113 - * Fires after an ActivityPub Update activity has been handled.
114 - *
115 - * @param array $activity The ActivityPub activity data.
116 - * @param int[]|null $user_ids The local user IDs.
117 - * @param bool $success True on success, false otherwise.
118 - * @param \WP_Comment|\WP_Post|\WP_Error $result The updated post, comment, or error.
119 - */
120 - \do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $success, $result );
79 + \do_action( 'activitypub_handled_update', $activity, null, $state, $reaction );
121 80 }
122 81
123 82 /**
124 - * Update an Actor.
83 + * Update an Actor
125 84 *
126 - * @param array $activity The Activity object.
127 - * @param int[]|null $user_ids The user IDs. Always null for Update activities.
85 + * @param array $activity The activity-object
86 + *
87 + * @return void
128 88 */
129 - public static function update_actor( $activity, $user_ids ) {
130 - /*
131 - * Prefer the actor data embedded in the activity object, as it contains
132 - * the fresh data sent by the remote server.
133 - */
134 - $actor = $activity['object'] ?? null;
89 + public static function update_actor( $activity ) {
90 + // update cache
91 + get_remote_metadata_by_actor( $activity['actor'], false );
135 92
136 - /*
137 - * The object may be a string IRI instead of an embedded object,
138 - * in which case we need to fetch the actor data remotely.
139 - * We use Http::get_remote_object() directly instead of
140 - * get_remote_metadata_by_actor() because the latter returns the
141 - * stale locally cached copy via fetch_by_uri().
142 - */
143 - if ( ! \is_array( $actor ) || ! isset( $actor['id'] ) ) {
144 - $object = Http::get_remote_object( $activity['actor'], false );
145 -
146 - if ( ! \is_wp_error( $object ) && \is_array( $object ) ) {
147 - $actor = $object;
148 - }
149 - }
150 -
151 - if ( \is_array( $actor ) && isset( $actor['id'] ) ) {
152 - $state = Remote_Actors::upsert( $actor );
153 - } else {
154 - $state = new \WP_Error( 'activitypub_update_failed', 'Update failed: missing or invalid actor object in Update activity' );
155 - $actor = array();
156 - }
157 -
158 - /**
159 - * Fires after an ActivityPub Update activity has been handled.
160 - *
161 - * @param array $activity The ActivityPub activity data.
162 - * @param int[] $user_ids The local user IDs.
163 - * @param int|\WP_Error $state Actor post ID on success, WP_Error on failure.
164 - * @param array $actor Remote actor meta data.
165 - */
166 - \do_action( 'activitypub_handled_update', $activity, (array) $user_ids, $state, $actor );
93 + // @todo maybe also update all interactions
167 94 }
168 95 }