fix delete and add improve undo

This commit is contained in:
Matthias Pfefferle 2023-11-10 19:29:07 +01:00
parent a29dc2ec1b
commit 700cd59fb7
3 changed files with 37 additions and 2 deletions

View file

@ -2,6 +2,7 @@
namespace Activitypub;
use WP_Error;
use WP_Comment_Query;
use Activitypub\Http;
use Activitypub\Activity\Activity;
use Activitypub\Collection\Followers;
@ -622,3 +623,29 @@ function get_total_users() {
return $users + 1;
}
/**
* Examine a comment ID and look up an existing comment it represents.
*
* @param string $id ActivityPub object ID (usually a URL) to check.
*
* @return int|boolean Comment ID, or false on failure.
*/
function object_id_to_comment( $id ) {
$comment_query = new WP_Comment_Query(
array(
'meta_key' => 'source_id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
)
);
if ( ! $comment_query->comments ) {
return false;
}
if ( count( $comment_query->comments ) > 1 ) {
return false;
}
return $comment_query->comments[0];
}

View file

@ -21,7 +21,11 @@ class Delete {
* @param int $user_id The ID of the user performing the delete activity.
*/
public function handle_delete( $activity, $user_id ) {
if ( ! isset( $activity['object'] ) || ! is_array( $activity['object'] ) ) {
if (
! isset( $activity['object'] ) ||
! is_array( $activity['object'] ) ||
! isset( $activity['object']['id'] )
) {
return;
}
@ -33,7 +37,10 @@ class Delete {
case 'Organization':
case 'Service':
case 'Application':
Followers::remove_follower( $activity['object']['id'], $user_id );
$follower = Followers::get_follower( $user_id, $activity['actor'] );
if ( $follower ) {
$follower->delete();
}
break;
case 'Tombstone':

View file

@ -23,6 +23,7 @@ class Undo {
public static function handle_undo( $activity, $user_id ) {
if (
isset( $activity['object'] ) &&
isset( $activity['actor'] ) &&
isset( $activity['object']['type'] ) &&
'Follow' === $activity['object']['type']
) {