| @@ -1,63 +1,54 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Announce handler file. | |
| 4 | - * | |
| 5 | - * @package Activitypub | |
| 6 | - */ | |
| 7 | - | |
| 8 | 2 | namespace Activitypub\Handler; |
| 9 | 3 | |
| 10 | -use Activitypub\Collection\Actors; | |
| 4 | +use Activitypub\Http; | |
| 5 | +use Activitypub\Comment; | |
| 11 | 6 | use Activitypub\Collection\Interactions; |
| 12 | -use Activitypub\Comment; | |
| 13 | -use Activitypub\Http; | |
| 14 | 7 | |
| 15 | -use function Activitypub\is_activity; | |
| 8 | +use function Activitypub\object_to_uri; | |
| 16 | 9 | use function Activitypub\is_activity_public; |
| 17 | -use function Activitypub\object_to_uri; | |
| 18 | 10 | |
| 19 | 11 | /** |
| 20 | - * Handle Create requests. | |
| 12 | + * Handle Create requests | |
| 21 | 13 | */ |
| 22 | 14 | class Announce { |
| 23 | 15 | /** |
| 24 | - * Initialize the class, registering WordPress hooks. | |
| 16 | + * Initialize the class, registering WordPress hooks | |
| 25 | 17 | */ |
| 26 | 18 | public static function init() { |
| 27 | - \add_action( 'activitypub_inbox_announce', array( self::class, 'handle_announce' ), 10, 3 ); | |
| 19 | + \add_action( | |
| 20 | + 'activitypub_inbox_announce', | |
| 21 | + array( self::class, 'handle_announce' ), | |
| 22 | + 10, | |
| 23 | + 3 | |
| 24 | + ); | |
| 28 | 25 | } |
| 29 | 26 | |
| 30 | 27 | /** |
| 31 | - * Handles "Announce" requests. | |
| 28 | + * Handles "Announce" requests | |
| 32 | 29 | * |
| 33 | - * @param array $announcement The activity-object. | |
| 34 | - * @param int|int[] $user_ids The id(s) of the local blog-user(s). | |
| 35 | - * @param \Activitypub\Activity\Activity $activity The activity object. | |
| 30 | + * @param array $array The activity-object | |
| 31 | + * @param int $user_id The id of the local blog-user | |
| 32 | + * @param Activitypub\Activity $activity The activity object | |
| 33 | + * | |
| 34 | + * @return void | |
| 36 | 35 | */ |
| 37 | - public static function handle_announce( $announcement, $user_ids, $activity = null ) { | |
| 38 | - // Check if Activity is public or not. | |
| 39 | - if ( ! is_activity_public( $announcement ) ) { | |
| 36 | + public static function handle_announce( $array, $user_id, $activity = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound | |
| 37 | + // check if Activity is public or not | |
| 38 | + if ( ! is_activity_public( $array ) ) { | |
| 40 | 39 | // @todo maybe send email |
| 41 | 40 | return; |
| 42 | 41 | } |
| 43 | 42 | |
| 44 | - // Ignore announces from the blog actor. | |
| 45 | - if ( Actors::BLOG_USER_ID === Actors::get_id_by_resource( $announcement['actor'] ) ) { | |
| 46 | - return; | |
| 43 | + if ( ! ACTIVITYPUB_DISABLE_REACTIONS ) { | |
| 44 | + self::maybe_save_announce( $array, $user_id, $activity ); | |
| 47 | 45 | } |
| 48 | 46 | |
| 49 | - // Check if reposts are allowed. | |
| 50 | - if ( ! Comment::is_comment_type_enabled( 'repost' ) ) { | |
| 51 | - return; | |
| 52 | - } | |
| 53 | - | |
| 54 | - self::maybe_save_announce( $announcement, $user_ids ); | |
| 55 | - | |
| 56 | - if ( is_string( $announcement['object'] ) ) { | |
| 57 | - $object = Http::get_remote_object( $announcement['object'] ); | |
| 47 | + if ( is_string( $array['object'] ) ) { | |
| 48 | + $object = Http::get_remote_object( $array['object'] ); | |
| 58 | 49 | } else { |
| 59 | - $object = $announcement['object']; | |
| 50 | + $object = $array['object']; | |
| 60 | 51 | } |
| 61 | 52 | |
| 62 | 53 | if ( ! $object || is_wp_error( $object ) ) { |
| 63 | 54 | return; |
| @@ -62,42 +53,29 @@ | ||
| 62 | 53 | if ( ! $object || is_wp_error( $object ) ) { |
| 63 | 54 | return; |
| 64 | 55 | } |
| 65 | 56 | |
| 66 | - if ( ! is_activity( $object ) ) { | |
| 57 | + if ( ! isset( $object['type'] ) ) { | |
| 67 | 58 | return; |
| 68 | 59 | } |
| 69 | 60 | |
| 70 | 61 | $type = \strtolower( $object['type'] ); |
| 71 | 62 | |
| 72 | - /** | |
| 73 | - * Fires after an Announce has been received. | |
| 74 | - * | |
| 75 | - * @param array $object The object. | |
| 76 | - * @param int[] $user_ids The ids of the local blog-users. | |
| 77 | - * @param string $type The type of the activity. | |
| 78 | - * @param \Activitypub\Activity\Activity|null $activity The activity object. | |
| 79 | - */ | |
| 80 | - \do_action( 'activitypub_inbox', $object, (array) $user_ids, $type, $activity ); | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * Fires after an Announce of a specific type has been received. | |
| 84 | - * | |
| 85 | - * @param array $object The object. | |
| 86 | - * @param int[] $user_ids The ids of the local blog-users. | |
| 87 | - * @param \Activitypub\Activity\Activity|null $activity The activity object. | |
| 88 | - */ | |
| 89 | - \do_action( "activitypub_inbox_{$type}", $object, (array) $user_ids, $activity ); | |
| 63 | + \do_action( 'activitypub_inbox', $object, $user_id, $type, $activity ); | |
| 64 | + \do_action( "activitypub_inbox_{$type}", $object, $user_id, $activity ); | |
| 90 | 65 | } |
| 91 | 66 | |
| 92 | 67 | /** |
| 93 | - * Try to save the Announce. | |
| 68 | + * Try to save the Announce | |
| 94 | 69 | * |
| 95 | - * @param array $activity The activity-object. | |
| 96 | - * @param int|int[] $user_ids The id of the local blog-user. | |
| 70 | + * @param array $array The activity-object | |
| 71 | + * @param int $user_id The id of the local blog-user | |
| 72 | + * @param Activitypub\Activity $activity The activity object | |
| 73 | + * | |
| 74 | + * @return void | |
| 97 | 75 | */ |
| 98 | - public static function maybe_save_announce( $activity, $user_ids ) { | |
| 99 | - $url = object_to_uri( $activity ); | |
| 76 | + public static function maybe_save_announce( $array, $user_id, $activity ) { // phpcs:ignore | |
| 77 | + $url = object_to_uri( $array['object'] ); | |
| 100 | 78 | |
| 101 | 79 | if ( empty( $url ) ) { |
| 102 | 80 | return; |
| 103 | 81 | } |
| @@ -106,28 +84,14 @@ | ||
| 106 | 84 | if ( $exists ) { |
| 107 | 85 | return; |
| 108 | 86 | } |
| 109 | 87 | |
| 110 | - // If the object is a Create activity, extract the actual object from it. | |
| 111 | - if ( isset( $activity['object']['type'] ) && 'Create' === $activity['object']['type'] ) { | |
| 112 | - $activity['object'] = object_to_uri( $activity['object']['object'] ); | |
| 113 | - } | |
| 88 | + $state = Interactions::add_reaction( $array ); | |
| 89 | + $reaction = null; | |
| 114 | 90 | |
| 115 | - $success = false; | |
| 116 | - $result = Interactions::add_reaction( $activity ); | |
| 117 | - | |
| 118 | - if ( $result && ! is_wp_error( $result ) ) { | |
| 119 | - $success = true; | |
| 120 | - $result = get_comment( $result ); | |
| 91 | + if ( $state && ! is_wp_error( $state ) ) { | |
| 92 | + $reaction = get_comment( $state ); | |
| 121 | 93 | } |
| 122 | 94 | |
| 123 | - /** | |
| 124 | - * Fires after an ActivityPub Announce activity has been handled. | |
| 125 | - * | |
| 126 | - * @param array $activity The ActivityPub activity data. | |
| 127 | - * @param int[] $user_ids The local user IDs. | |
| 128 | - * @param bool $success True on success, false otherwise. | |
| 129 | - * @param array|string|int|\WP_Error|false $result The WP_Comment object of the created announce/repost comment, or null if creation failed. | |
| 130 | - */ | |
| 131 | - \do_action( 'activitypub_handled_announce', $activity, (array) $user_ids, $success, $result ); | |
| 95 | + do_action( 'activitypub_handled_announce', $array, $user_id, $state, $reaction ); | |
| 132 | 96 | } |
| 133 | 97 | } |