From bd6ba98e8d8da4fa3b2075734d1db5a65be2c3f5 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 7 Oct 2018 18:41:45 +0000 Subject: [PATCH] Some more cleanup --- src/Protocol/ActivityPub/Processor.php | 83 +++++++++++--------------- src/Protocol/ActivityPub/Receiver.php | 40 +++++++------ 2 files changed, 57 insertions(+), 66 deletions(-) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 74d4416f1e..7d181234fe 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -67,9 +67,9 @@ class Processor } /** - * + * Add attachment data to the item array * - * @param $attachments + * @param array $attachments * @param array $item * * @return item array @@ -101,10 +101,10 @@ class Processor } /** - * + * Prepares data for a message * - * @param array $activity - * @param $body + * @param array $activity Activity array + * @param string $body original source */ public static function createItem($activity, $body) { @@ -129,16 +129,16 @@ class Processor } /** - * + * Prepare the item array for a "like" * - * @param array $activity - * @param $body + * @param array $activity Activity array + * @param string $body original source */ public static function likeItem($activity, $body) { $item = []; $item['verb'] = ACTIVITY_LIKE; - $item['parent-uri'] = JsonLD::fetchElement($activity, 'object'); + $item['parent-uri'] = $activity['object_id']; $item['gravity'] = GRAVITY_ACTIVITY; $item['object-type'] = ACTIVITY_OBJ_NOTE; @@ -149,27 +149,26 @@ class Processor * Delete items * * @param array $activity - * @param $body */ public static function deleteItem($activity) { $owner = Contact::getIdForURL($activity['actor']); - $object = JsonLD::fetchElement($activity, 'object'); - logger('Deleting item ' . $object . ' from ' . $owner, LOGGER_DEBUG); - Item::delete(['uri' => $object, 'owner-id' => $owner]); + + logger('Deleting item ' . $activity['object_id'] . ' from ' . $owner, LOGGER_DEBUG); + Item::delete(['uri' => $activity['object_id'], 'owner-id' => $owner]); } /** - * + * Prepare the item array for a "dislike" * - * @param array $activity - * @param $body + * @param array $activity Activity array + * @param string $body original source */ public static function dislikeItem($activity, $body) { $item = []; $item['verb'] = ACTIVITY_DISLIKE; - $item['parent-uri'] = JsonLD::fetchElement($activity, 'object'); + $item['parent-uri'] = $activity['object_id']; $item['gravity'] = GRAVITY_ACTIVITY; $item['object-type'] = ACTIVITY_OBJ_NOTE; @@ -177,11 +176,11 @@ class Processor } /** - * + * Creates an item post * - * @param array $activity - * @param array $item - * @param $body + * @param array $activity Activity data + * @param array $item item array + * @param string $body original source */ private static function postItem($activity, $item, $body) { @@ -233,7 +232,7 @@ class Processor } /** - * + * Fetches missing posts * * @param $url * @param $child @@ -273,8 +272,7 @@ class Processor */ public static function followUser($activity) { - $actor = JsonLD::fetchElement($activity, 'object'); - $uid = User::getIdForURL($actor); + $uid = User::getIdForURL($activity['object_actor']); if (empty($uid)) { return; } @@ -313,13 +311,12 @@ class Processor */ public static function updatePerson($activity) { - $actor = JsonLD::fetchElement($activity, 'object'); - if ($actor) { + if (empty($activity['object_id'])) { return; } - logger('Updating profile for ' . $actor, LOGGER_DEBUG); - APContact::getByURL($actor, true); + logger('Updating profile for ' . $activity['object_id'], LOGGER_DEBUG); + APContact::getByURL($activity['object_id'], true); } /** @@ -329,26 +326,23 @@ class Processor */ public static function deletePerson($activity) { - $id = JsonLD::fetchElement($activity, 'object'); - $actor = JsonLD::fetchElement($activity, 'object', 'as:actor'); - - if (empty($id) || empty($actor)) { + if (empty($activity['object_id']) || empty($activity['object_actor'])) { logger('Empty object id or actor.', LOGGER_DEBUG); return; } - if ($id != $actor) { + if ($activity['object_id'] != $activity['object_actor']) { logger('Object id does not match actor.', LOGGER_DEBUG); return; } - $contacts = DBA::select('contact', ['id'], ['nurl' => normalise_link($id)]); + $contacts = DBA::select('contact', ['id'], ['nurl' => normalise_link($activity['object_id'])]); while ($contact = DBA::fetch($contacts)) { Contact::remove($contact['id']); } DBA::close($contacts); - logger('Deleted contact ' . $id, LOGGER_DEBUG); + logger('Deleted contact ' . $activity['object_id'], LOGGER_DEBUG); } /** @@ -358,8 +352,7 @@ class Processor */ public static function acceptFollowUser($activity) { - $actor = JsonLD::fetchElement($activity, 'object', 'as:actor'); - $uid = User::getIdForURL($actor); + $uid = User::getIdForURL($activity['object_actor']); if (empty($uid)) { return; } @@ -391,8 +384,7 @@ class Processor */ public static function rejectFollowUser($activity) { - $actor = JsonLD::fetchElement($activity, 'object', 'as:actor'); - $uid = User::getIdForURL($actor); + $uid = User::getIdForURL($activity['object_actor']); if (empty($uid)) { return; } @@ -420,22 +412,20 @@ class Processor */ public static function undoActivity($activity) { - $activity_url = JsonLD::fetchElement($activity, 'object'); - if (empty($activity_url)) { + if (empty($activity['object_id'])) { return; } - $actor = JsonLD::fetchElement($activity, 'object', 'as:actor'); - if (empty($actor)) { + if (empty($activity['object_actor'])) { return; } - $author_id = Contact::getIdForURL($actor); + $author_id = Contact::getIdForURL($activity['object_actor']); if (empty($author_id)) { return; } - Item::delete(['uri' => $activity_url, 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]); + Item::delete(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]); } /** @@ -445,8 +435,7 @@ class Processor */ public static function undoFollowUser($activity) { - $object = JsonLD::fetchElement($activity, 'object', 'as:object'); - $uid = User::getIdForURL($object); + $uid = User::getIdForURL($activity['object_object']); if (empty($uid)) { return; } diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 1a261d2bfd..ba6cd52ba3 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -112,26 +112,26 @@ class Receiver } /** - * + * Prepare the object array * * @param array $activity * @param integer $uid User ID * @param $trust_source * - * @return + * @return array with object data */ - private static function prepareObjectData($ldactivity, $uid, &$trust_source) + private static function prepareObjectData($activity, $uid, &$trust_source) { - $actor = JsonLD::fetchElement($ldactivity, 'as:actor'); + $actor = JsonLD::fetchElement($activity, 'as:actor'); if (empty($actor)) { logger('Empty actor', LOGGER_DEBUG); return []; } - $type = JsonLD::fetchElement($ldactivity, '@type'); + $type = JsonLD::fetchElement($activity, '@type'); // Fetch all receivers from to, cc, bto and bcc - $receivers = self::getReceivers($ldactivity, $actor); + $receivers = self::getReceivers($activity, $actor); // When it is a delivery to a personal inbox we add that user to the receivers if (!empty($uid)) { @@ -142,7 +142,7 @@ class Receiver logger('Receivers: ' . json_encode($receivers), LOGGER_DEBUG); - $object_id = JsonLD::fetchElement($ldactivity, 'as:object'); + $object_id = JsonLD::fetchElement($activity, 'as:object'); if (empty($object_id)) { logger('No object found', LOGGER_DEBUG); return []; @@ -153,7 +153,7 @@ class Receiver if ($type == 'as:Announce') { $trust_source = false; } - $object_data = self::fetchObject($object_id, $ldactivity['as:object'], $trust_source); + $object_data = self::fetchObject($object_id, $activity['as:object'], $trust_source); if (empty($object_data)) { logger("Object data couldn't be processed", LOGGER_DEBUG); return []; @@ -163,19 +163,21 @@ class Receiver } elseif (in_array($type, ['as:Like', 'as:Dislike'])) { // Create a mostly empty array out of the activity data (instead of the object). // This way we later don't have to check for the existence of ech individual array element. - $object_data = self::processObject($ldactivity); + $object_data = self::processObject($activity); $object_data['name'] = $type; - $object_data['author'] = JsonLD::fetchElement($ldactivity, 'as:actor'); + $object_data['author'] = JsonLD::fetchElement($activity, 'as:actor'); $object_data['object'] = $object_id; $object_data['object_type'] = ''; // Since we don't fetch the object, we don't know the type } else { $object_data = []; - $object_data['id'] = JsonLD::fetchElement($ldactivity, '@id'); - $object_data['object'] = $ldactivity['as:object']; - $object_data['object_type'] = JsonLD::fetchElement($ldactivity, 'as:object', '@type'); + $object_data['id'] = JsonLD::fetchElement($activity, '@id'); + $object_data['object_id'] = JsonLD::fetchElement($activity, 'as:object'); + $object_data['object_actor'] = JsonLD::fetchElement($activity['as:object'], 'as:actor'); + $object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object'); + $object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type'); } - $object_data = self::addActivityFields($object_data, $ldactivity); + $object_data = self::addActivityFields($object_data, $activity); $object_data['type'] = $type; $object_data['actor'] = $actor; @@ -194,27 +196,27 @@ class Receiver * @param integer $uid User ID * @param boolean $trust_source Do we trust the source? */ - public static function processActivity($ldactivity, $body = '', $uid = null, $trust_source = false) + public static function processActivity($activity, $body = '', $uid = null, $trust_source = false) { - $type = JsonLD::fetchElement($ldactivity, '@type'); + $type = JsonLD::fetchElement($activity, '@type'); if (!$type) { logger('Empty type', LOGGER_DEBUG); return; } - if (!JsonLD::fetchElement($ldactivity, 'as:object')) { + if (!JsonLD::fetchElement($activity, 'as:object')) { logger('Empty object', LOGGER_DEBUG); return; } - if (!JsonLD::fetchElement($ldactivity, 'as:actor')) { + if (!JsonLD::fetchElement($activity, 'as:actor')) { logger('Empty actor', LOGGER_DEBUG); return; } // $trust_source is called by reference and is set to true if the content was retrieved successfully - $object_data = self::prepareObjectData($ldactivity, $uid, $trust_source); + $object_data = self::prepareObjectData($activity, $uid, $trust_source); if (empty($object_data)) { logger('No object data found', LOGGER_DEBUG); return; -- 2.39.5