wordpress-activitypub/includes/handler/class-create.php

74 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2023-11-08 16:46:02 +01:00
<?php
namespace Activitypub\Handler;
2023-11-13 16:58:04 +01:00
use WP_Error;
use Activitypub\Collection\Interactions;
2023-11-08 16:46:02 +01:00
use function Activitypub\is_activity_public;
2023-11-13 16:58:04 +01:00
use function Activitypub\object_id_to_comment;
2023-11-08 16:46:02 +01:00
/**
* Handle Create requests
*/
class Create {
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
2023-11-13 16:58:04 +01:00
\add_action( 'activitypub_inbox_create', array( self::class, 'handle_create' ), 10, 3 );
2023-11-08 16:46:02 +01:00
}
/**
* Handles "Create" requests
*
2023-11-13 16:58:04 +01:00
* @param array $array The activity-object
* @param int $user_id The id of the local blog-user
* @param Activitypub\Activity $object The activity object
*
* @return void|WP_Error WP_Error on failure
2023-11-08 16:46:02 +01:00
*/
2023-11-13 16:58:04 +01:00
public static function handle_create( $array, $user_id, $object = null ) {
if (
! isset( $array['object'] ) ||
! isset( $array['object']['id'] )
) {
return new WP_Error(
'activitypub_no_valid_object',
__( 'No object id found.', 'activitypub' ),
array( 'status' => 400 )
);
2023-11-08 16:46:02 +01:00
}
// check if Activity is public or not
2023-11-13 16:58:04 +01:00
if ( ! is_activity_public( $array ) ) {
2023-11-08 16:46:02 +01:00
// @todo maybe send email
2023-11-13 16:58:04 +01:00
return new WP_Error(
'activitypub_activity_not_public',
__( 'Activity is not public.', 'activitypub' ),
array( 'status' => 400 )
);
2023-11-08 16:46:02 +01:00
}
2023-11-13 16:58:04 +01:00
$check_dupe = object_id_to_comment( $array['object']['id'] );
2023-11-08 16:46:02 +01:00
2023-11-13 16:58:04 +01:00
// if comment exists, call update action
if ( $check_dupe ) {
\do_action( 'activitypub_inbox_update', $array, $user_id, $object );
return new WP_Error(
'activitypub_comment_exists',
__( 'Comment already exists, initiated Update process.', 'activitypub' ),
array( 'status' => 400 )
);
2023-11-08 16:46:02 +01:00
}
2023-11-13 16:58:04 +01:00
$reaction = Interactions::add_comment( $array );
$state = null;
2023-11-08 16:46:02 +01:00
2023-11-13 16:58:04 +01:00
if ( $reaction ) {
$state = $reaction['comment_ID'];
2023-11-08 16:46:02 +01:00
}
2023-11-13 16:58:04 +01:00
\do_action( 'activitypub_handled_create', $array, $user_id, $state, $reaction );
2023-11-08 16:46:02 +01:00
}
}