From: Michael Date: Sat, 27 Oct 2018 06:17:17 +0000 (+0000) Subject: AP: Event participation and editing of posts are now supported X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f2ddcbe6321187d141f80267691e50851ec7359b;p=friendica.git AP: Event participation and editing of posts are now supported --- diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index e7073ab878..dee70f152c 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -16,6 +16,7 @@ use Friendica\Content\Text\HTML; use Friendica\Util\JsonLD; use Friendica\Core\Config; use Friendica\Protocol\ActivityPub; +use Friendica\Util\DateTimeFormat; /** * ActivityPub Processor Protocol class @@ -101,6 +102,24 @@ class Processor return $item; } + /** + * Updates a message + * + * @param array $activity Activity array + */ + public static function updateItem($activity) + { + $item = []; + $item['changed'] = DateTimeFormat::utcNow(); + $item['edited'] = $activity['updated']; + $item['title'] = HTML::toBBCode($activity['name']); + $item['content-warning'] = HTML::toBBCode($activity['summary']); + $item['body'] = self::convertMentions(HTML::toBBCode($activity['content'])); + $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']); + + Item::update($item, ['uri' => $activity['id']]); + } + /** * Prepares data for a message * @@ -128,22 +147,6 @@ class Processor self::postItem($activity, $item); } - /** - * Prepare the item array for a "like" - * - * @param array $activity Activity array - */ - public static function likeItem($activity) - { - $item = []; - $item['verb'] = ACTIVITY_LIKE; - $item['parent-uri'] = $activity['object_id']; - $item['gravity'] = GRAVITY_ACTIVITY; - $item['object-type'] = ACTIVITY_OBJ_NOTE; - - self::postItem($activity, $item); - } - /** * Delete items * @@ -158,14 +161,15 @@ class Processor } /** - * Prepare the item array for a "dislike" + * Prepare the item array for an activity * * @param array $activity Activity array + * @param string $verb Activity verb */ - public static function dislikeItem($activity) + public static function createActivity($activity, $verb) { $item = []; - $item['verb'] = ACTIVITY_DISLIKE; + $item['verb'] = $verb; $item['parent-uri'] = $activity['object_id']; $item['gravity'] = GRAVITY_ACTIVITY; $item['object-type'] = ACTIVITY_OBJ_NOTE; @@ -176,7 +180,8 @@ class Processor /** * Create an event * - * @param array $activity Activity array + * @param array $activity Activity array + * @param array $item */ public static function createEvent($activity, $item) { diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index f7c041a8ae..e94940a39e 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -21,14 +21,7 @@ use Friendica\Util\DateTimeFormat; * @brief ActivityPub Receiver Protocol class * * To-Do: - * - Update (Image, Video, Article, Note) * - Undo Announce - * - Accept Event - * - Reject Event - * - TentativeAccept Even - * - Undo Accept Event - * - Undo Reject Event - * - Undo TentativeAccept Event * * Check what this is meant to do: * - Add @@ -36,7 +29,6 @@ use Friendica\Util\DateTimeFormat; * - Flag * - Remove * - Undo Block - * - Undo Accept Person */ class Receiver { @@ -125,10 +117,11 @@ class Receiver */ private static function fetchObjectType($activity, $object_id) { - - $object_type = JsonLD::fetchElement($activity['as:object'], '@type'); - if (!empty($object_type)) { - return $object_type; + if (!empty($activity['as:object'])) { + $object_type = JsonLD::fetchElement($activity['as:object'], '@type'); + if (!empty($object_type)) { + return $object_type; + } } if (Item::exists(['uri' => $object_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]])) { @@ -193,7 +186,7 @@ class Receiver $object_type = self::fetchObjectType($activity, $object_id); // Fetch the content only on activities where this matters - if (in_array($type, ['as:Create', 'as:Announce'])) { + if (in_array($type, ['as:Create', 'as:Update', 'as:Announce'])) { if ($type == 'as:Announce') { $trust_source = false; } @@ -219,6 +212,11 @@ class Receiver $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'); + + // An Undo is done on the object of an object, so we need that type as well + if ($type == 'as:Undo') { + $object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object']); + } } $object_data = self::addActivityFields($object_data, $activity); @@ -311,20 +309,32 @@ class Receiver switch ($type) { case 'as:Create': case 'as:Announce': - ActivityPub\Processor::createItem($object_data); + if (in_array($object_data['object_type'], self::CONTENT_TYPES)) { + ActivityPub\Processor::createItem($object_data); + } break; case 'as:Like': - ActivityPub\Processor::likeItem($object_data); + if (in_array($object_data['object_type'], self::CONTENT_TYPES)) { + ActivityPub\Processor::createActivity($object_data, ACTIVITY_LIKE); + } break; case 'as:Dislike': - ActivityPub\Processor::dislikeItem($object_data); + if (in_array($object_data['object_type'], self::CONTENT_TYPES)) { + ActivityPub\Processor::createActivity($object_data, ACTIVITY_DISLIKE); + } + break; + + case 'as:TentativeAccept': + if (in_array($object_data['object_type'], self::CONTENT_TYPES)) { + ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDMAYBE); + } break; case 'as:Update': if (in_array($object_data['object_type'], self::CONTENT_TYPES)) { - /// @todo + ActivityPub\Processor::updateItem($object_data); } elseif (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) { ActivityPub\Processor::updatePerson($object_data, $body); } @@ -339,31 +349,42 @@ class Receiver break; case 'as:Follow': - ActivityPub\Processor::followUser($object_data); + if (in_array($object_data['object_type'], self::ACCOUNT_TYPES)) { + ActivityPub\Processor::followUser($object_data); + } break; case 'as:Accept': if ($object_data['object_type'] == 'as:Follow') { ActivityPub\Processor::acceptFollowUser($object_data); + } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) { + ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTEND); } break; case 'as:Reject': if ($object_data['object_type'] == 'as:Follow') { ActivityPub\Processor::rejectFollowUser($object_data); + } elseif (in_array($object_data['object_type'], self::CONTENT_TYPES)) { + ActivityPub\Processor::createActivity($object_data, ACTIVITY_ATTENDNO); } break; case 'as:Undo': - if ($object_data['object_type'] == 'as:Follow') { + if (($object_data['object_type'] == 'as:Follow') && + in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) { ActivityPub\Processor::undoFollowUser($object_data); - } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES)) { + } elseif (($object_data['object_type'] == 'as:Accept') && + in_array($object_data['object_object_type'], self::ACCOUNT_TYPES)) { + ActivityPub\Processor::rejectFollowUser($object_data); + } elseif (in_array($object_data['object_type'], self::ACTIVITY_TYPES) && + in_array($object_data['object_object_type'], self::CONTENT_TYPES)) { ActivityPub\Processor::undoActivity($object_data); } break; default: - logger('Unknown activity: ' . $type, LOGGER_DEBUG); + logger('Unknown activity: ' . $type . ' ' . $object_data['object_type'], LOGGER_DEBUG); break; } } diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 9f98409c33..bfc26cd9f1 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -282,8 +282,7 @@ class Transmitter foreach ($activity[$element] as $receiver) { if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) { $permissions[$element][] = $item_profile['followers']; - } - if (!in_array($receiver, $exclude)) { + } elseif (!in_array($receiver, $exclude)) { $permissions[$element][] = $receiver; } } @@ -309,13 +308,13 @@ class Transmitter $data = ['to' => [], 'cc' => [], 'bcc' => []]; - $data = array_merge($data, self::fetchPermissionBlockFromConversation($item)); - $actor_profile = APContact::getByURL($item['author-link']); $terms = Term::tagArrayFromItemId($item['id'], TERM_MENTION); if (!$item['private']) { + $data = array_merge($data, self::fetchPermissionBlockFromConversation($item)); + $data['to'][] = ActivityPub::PUBLIC_COLLECTION; if (!empty($actor_profile['followers'])) { $data['cc'][] = $actor_profile['followers'];