|
9 | 9 |
|
10 | 10 | use Activitypub\Activity\Activity; |
11 | 11 | use Activitypub\Activity\Base_Object; |
| 12 | +use Activitypub\Comment; |
12 | 13 |
|
13 | 14 | use function Activitypub\is_activity_public; |
14 | 15 | use function Activitypub\object_to_uri; |
@@ -140,4 +141,94 @@ public static function get( $guid, $user_id ) { |
140 | 141 |
|
141 | 142 | return \get_post( $post_id ); |
142 | 143 | } |
| 144 | + |
| 145 | + /** |
| 146 | + * Get an inbox item by its GUID. |
| 147 | + * |
| 148 | + * @param string $guid The GUID of the inbox item. |
| 149 | + * |
| 150 | + * @return \WP_Post|\WP_Error The inbox item or WP_Error. |
| 151 | + */ |
| 152 | + public static function get_by_guid( $guid ) { |
| 153 | + global $wpdb; |
| 154 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 155 | + $post_id = $wpdb->get_var( |
| 156 | + $wpdb->prepare( |
| 157 | + "SELECT ID FROM $wpdb->posts WHERE guid=%s AND post_type=%s", |
| 158 | + \esc_url( $guid ), |
| 159 | + self::POST_TYPE |
| 160 | + ) |
| 161 | + ); |
| 162 | + |
| 163 | + if ( ! $post_id ) { |
| 164 | + return new \WP_Error( |
| 165 | + 'activitypub_inbox_item_not_found', |
| 166 | + \__( 'Inbox item not found', 'activitypub' ), |
| 167 | + array( 'status' => 404 ) |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + return \get_post( $post_id ); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Undo a received activity. |
| 176 | + * |
| 177 | + * @param string $id The ID of the inbox item to be removed. |
| 178 | + * |
| 179 | + * @return bool|\WP_Error True on success, WP_Error on failure. |
| 180 | + */ |
| 181 | + public static function undo( $id ) { |
| 182 | + $inbox_item = self::get_by_guid( $id ); |
| 183 | + |
| 184 | + if ( \is_wp_error( $inbox_item ) ) { |
| 185 | + // If inbox entry not found, return the error. |
| 186 | + return $inbox_item; |
| 187 | + } |
| 188 | + |
| 189 | + $type = \get_post_meta( $inbox_item->ID, '_activitypub_activity_type', true ); |
| 190 | + |
| 191 | + switch ( $type ) { |
| 192 | + case 'Follow': |
| 193 | + $actor = \get_post_meta( $inbox_item->ID, '_activitypub_activity_remote_actor', true ); |
| 194 | + $remote_actor = Remote_Actors::get_by_uri( $actor ); |
| 195 | + |
| 196 | + if ( \is_wp_error( $remote_actor ) ) { |
| 197 | + return $remote_actor; |
| 198 | + } |
| 199 | + |
| 200 | + return Followers::remove( $remote_actor, $inbox_item->post_author ); |
| 201 | + |
| 202 | + case 'Like': |
| 203 | + case 'Create': |
| 204 | + case 'Announce': |
| 205 | + if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) { |
| 206 | + return new \WP_Error( |
| 207 | + 'activitypub_inbox_undo_interactions_disabled', |
| 208 | + \__( 'Undo is not possible because incoming interactions are disabled.', 'activitypub' ), |
| 209 | + array( 'status' => 403 ) |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + $result = Comment::object_id_to_comment( esc_url_raw( $inbox_item->guid ) ); |
| 214 | + |
| 215 | + if ( empty( $result ) ) { |
| 216 | + return new \WP_Error( |
| 217 | + 'activitypub_inbox_undo_comment_not_found', |
| 218 | + \__( 'Undo is not possible because the comment was not found.', 'activitypub' ), |
| 219 | + array( 'status' => 404 ) |
| 220 | + ); |
| 221 | + } |
| 222 | + |
| 223 | + return \wp_delete_comment( $result, true ); |
| 224 | + |
| 225 | + default: |
| 226 | + return new \WP_Error( |
| 227 | + 'activitypub_inbox_undo_unsupported', |
| 228 | + // Translators: %s is the activity type. |
| 229 | + \sprintf( \__( 'Undo is not supported for %s activities.', 'activitypub' ), $type ), |
| 230 | + array( 'status' => 400 ) |
| 231 | + ); |
| 232 | + } |
| 233 | + } |
143 | 234 | } |
0 commit comments