From 3c48a1f787a6338fc1ce9ba7d7ac2a475f1dd093 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Oct 2018 16:41:29 +0000 Subject: [PATCH 1/5] AP: Announce will now work --- index.php | 2 +- src/Protocol/ActivityPub/Transmitter.php | 29 +++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/index.php b/index.php index dc3dc80f60..6ab405c632 100644 --- a/index.php +++ b/index.php @@ -46,7 +46,7 @@ if ($a->isMaxProcessesReached() || $a->isMaxLoadReached()) { System::httpExit(503, ['title' => 'Error 503 - Service Temporarily Unavailable', 'description' => 'System is currently overloaded. Please try again later.']); } -if (strstr($a->query_string, '.well-known/host-meta') and ($a->query_string != '.well-known/host-meta')) { +if (strstr($a->query_string, '.well-known/host-meta') && ($a->query_string != '.well-known/host-meta')) { System::httpExit(404); } diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 96c069e589..3fc5900afa 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -22,8 +22,11 @@ use Friendica\Model\Profile; use Friendica\Core\Config; use Friendica\Object\Image; use Friendica\Protocol\ActivityPub; +use Friendica\Protocol\Diaspora; use Friendica\Core\Cache; +require_once 'include/api.php'; + /** * @brief ActivityPub Transmitter Protocol class * @@ -37,7 +40,6 @@ use Friendica\Core\Cache; * - Event * * Complicated object types: - * - Announce * - Undo Announce * * General: @@ -467,7 +469,9 @@ class Transmitter */ private static function getTypeOfItem($item) { - if ($item['verb'] == ACTIVITY_POST) { + if (!empty(Diaspora::isReshare($item['body'], false))) { + $type = 'Announce'; + } elseif ($item['verb'] == ACTIVITY_POST) { if ($item['created'] == $item['edited']) { $type = 'Create'; } else { @@ -558,8 +562,10 @@ class Transmitter $data = array_merge($data, self::createPermissionBlockForItem($item)); - if (in_array($data['type'], ['Create', 'Update', 'Announce', 'Delete'])) { + if (in_array($data['type'], ['Create', 'Update', 'Delete'])) { $data['object'] = self::createNote($item); + } elseif ($data['type'] == 'Announce') { + $data['object'] = self::createAnnounce($item); } elseif ($data['type'] == 'Undo') { $data['object'] = self::createActivityFromItem($item_id, true); } else { @@ -805,6 +811,23 @@ class Transmitter return $data; } + /** + * Creates an announce object entry + * + * @param array $item + * + * @return string with announced object url + */ + public static function createAnnounce($item) + { + $announce = api_share_as_retweet($item); + if (empty($announce['plink'])) { + return self::createNote($item); + } + + return $announce['plink']; + } + /** * Transmits a contact suggestion to a given inbox * From 8b9aa80aadd5700387118883db19661c8ac4ebd3 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Oct 2018 18:59:39 +0000 Subject: [PATCH 2/5] "to" is now used for mentions, "cc" for everything else --- src/Protocol/ActivityPub/Transmitter.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 3fc5900afa..f5765ccdd3 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -329,7 +329,7 @@ class Transmitter foreach ($terms as $term) { $profile = APContact::getByURL($term['url'], false); if (!empty($profile) && empty($contacts[$profile['url']])) { - $data['cc'][] = $profile['url']; + $data['to'][] = $profile['url']; $contacts[$profile['url']] = $profile['url']; } } @@ -381,11 +381,6 @@ class Transmitter } DBA::close($parents); - if (empty($data['to'])) { - $data['to'] = $data['cc']; - $data['cc'] = []; - } - return $data; } From f60468677e40374799d39506e37acb96d98ab2f1 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Oct 2018 19:49:20 +0000 Subject: [PATCH 3/5] We now transmit location and application as well. --- src/Protocol/ActivityPub/Receiver.php | 4 ++ src/Protocol/ActivityPub/Transmitter.php | 48 ++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index f54503708b..773d45c83a 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -25,6 +25,10 @@ use Friendica\Util\DateTimeFormat; * - Event * - Undo Announce * + * Missing object fields: + * - Location + * - Generator + * * Check what this is meant to do: * - Add * - Block diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index f5765ccdd3..3c22f8f62b 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -24,6 +24,7 @@ use Friendica\Object\Image; use Friendica\Protocol\ActivityPub; use Friendica\Protocol\Diaspora; use Friendica\Core\Cache; +use Friendica\Util\Map; require_once 'include/api.php'; @@ -32,10 +33,6 @@ require_once 'include/api.php'; * * To-Do: * - * Missing object fields: - * - service (App) - * - location - * * Missing object types: * - Event * @@ -600,6 +597,40 @@ class Transmitter return $data; } + /** + * Creates a location entry for a given item array + * + * @param array $item + * + * @return array with location array + */ + private static function createLocation($item) + { + $location = ['type' => 'Place']; + + if (!empty($item['location'])) { + $location['name'] = $item['location']; + } + + $coord = []; + + if (empty($item['coord'])) { + $coord = Map::getCoordinates($item['location']); + } else { + $coords = explode(' ', $item['coord']); + if (count($coords) == 2) { + $coord = ['lat' => $coords[0], 'lon' => $coords[1]]; + } + } + + if (!empty($coord['lat']) && !empty($coord['lon'])) { + $location['latitude'] = $coord['lat']; + $location['longitude'] = $coord['lon']; + } + + return $location; + } + /** * Returns a tag array for a given item array * @@ -801,6 +832,15 @@ class Transmitter $data['attachment'] = self::createAttachmentList($item, $type); $data['tag'] = self::createTagList($item); + + if (!empty($item['coord']) || !empty($item['location'])) { + $data['location'] = self::createLocation($item); + } + + if (!empty($item['app'])) { + $data['generator'] = ['type' => 'Application', 'name' => $item['app']]; + } + $data = array_merge($data, self::createPermissionBlockForItem($item)); return $data; From aab4fa0c3121a777bd58c3350a13ebaaf1fd5457 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Oct 2018 20:36:44 +0000 Subject: [PATCH 4/5] The instrument is now added, updating AP contacts is now enabled --- mod/crepair.php | 2 +- src/Protocol/ActivityPub/Transmitter.php | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mod/crepair.php b/mod/crepair.php index 076c611db4..d8c6b35f5a 100644 --- a/mod/crepair.php +++ b/mod/crepair.php @@ -133,7 +133,7 @@ function crepair_content(App $a) $remote_self_options = ['0' => L10n::t('No mirroring'), '2' => L10n::t('Mirror as my own posting')]; } - $update_profile = in_array($contact['network'], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]); + $update_profile = in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]); $tab_str = contacts_tab($a, $contact, 5); diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 3c22f8f62b..b261e6060c 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -4,6 +4,7 @@ */ namespace Friendica\Protocol\ActivityPub; +use Friendica\BaseObject; use Friendica\Database\DBA; use Friendica\Core\System; use Friendica\Util\HTTPSignature; @@ -552,6 +553,8 @@ class Transmitter $data['published'] = DateTimeFormat::utc($item['created'] . '+00:00', DateTimeFormat::ATOM); + $data['instrument'] = ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()]; + $data = array_merge($data, self::createPermissionBlockForItem($item)); if (in_array($data['type'], ['Create', 'Update', 'Delete'])) { @@ -883,7 +886,7 @@ class Transmitter 'actor' => $owner['url'], 'object' => $suggestion['url'], 'content' => $suggestion['note'], - 'published' => DateTimeFormat::utc($suggestion['created'] . '+00:00', DateTimeFormat::ATOM), + 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()], 'to' => [ActivityPub::PUBLIC_COLLECTION], 'cc' => []]; @@ -910,6 +913,7 @@ class Transmitter 'actor' => $owner['url'], 'object' => $owner['url'], 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM), + 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()], 'to' => [ActivityPub::PUBLIC_COLLECTION], 'cc' => []]; @@ -936,6 +940,7 @@ class Transmitter 'actor' => $owner['url'], 'object' => self::getProfile($uid), 'published' => DateTimeFormat::utcNow(DateTimeFormat::ATOM), + 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()], 'to' => [$profile['followers']], 'cc' => []]; @@ -963,6 +968,7 @@ class Transmitter 'type' => $activity, 'actor' => $owner['url'], 'object' => $profile['url'], + 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()], 'to' => $profile['url']]; logger('Sending activity ' . $activity . ' to ' . $target . ' for user ' . $uid, LOGGER_DEBUG); @@ -990,6 +996,7 @@ class Transmitter 'object' => ['id' => $id, 'type' => 'Follow', 'actor' => $profile['url'], 'object' => $owner['url']], + 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()], 'to' => $profile['url']]; logger('Sending accept to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG); @@ -1017,6 +1024,7 @@ class Transmitter 'object' => ['id' => $id, 'type' => 'Follow', 'actor' => $profile['url'], 'object' => $owner['url']], + 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()], 'to' => $profile['url']]; logger('Sending reject to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG); @@ -1045,6 +1053,7 @@ class Transmitter 'object' => ['id' => $id, 'type' => 'Follow', 'actor' => $owner['url'], 'object' => $profile['url']], + 'instrument' => ['type' => 'Service', 'name' => BaseObject::getApp()->getUserAgent()], 'to' => $profile['url']]; logger('Sending undo to ' . $target . ' for user ' . $uid . ' with id ' . $id, LOGGER_DEBUG); From e829e074fc227cbf56f01348d7a519e32f82e402 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Oct 2018 21:37:39 +0000 Subject: [PATCH 5/5] Location, coord and app are now processed as well --- src/Protocol/ActivityPub/Processor.php | 7 ++++++- src/Protocol/ActivityPub/Receiver.php | 26 ++++++++------------------ 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 166530f472..d77a0277fb 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -212,8 +212,13 @@ class Processor $item['content-warning'] = HTML::toBBCode($activity['summary']); $item['body'] = self::convertMentions(HTML::toBBCode($activity['content'])); $item['location'] = $activity['location']; + + if (!empty($item['latitude']) && !empty($item['longitude'])) { + $item['coord'] = $item['latitude'] . ' ' . $item['longitude']; + } + $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']); - $item['app'] = $activity['service']; + $item['app'] = $activity['generator']; $item['plink'] = defaults($activity, 'alternate-url', $item['uri']); $item['diaspora_signed_text'] = defaults($activity, 'diaspora:comment', ''); diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 773d45c83a..cde197b0c4 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -25,10 +25,6 @@ use Friendica\Util\DateTimeFormat; * - Event * - Undo Announce * - * Missing object fields: - * - Location - * - Generator - * * Check what this is meant to do: * - Add * - Block @@ -517,24 +513,15 @@ class Receiver private static function addActivityFields($object_data, $activity) { if (!empty($activity['published']) && empty($object_data['published'])) { - $object_data['published'] = JsonLD::fetchElement($activity, 'published', '@value'); - } - - if (!empty($activity['updated']) && empty($object_data['updated'])) { - $object_data['updated'] = JsonLD::fetchElement($activity, 'updated', '@value'); + $object_data['published'] = JsonLD::fetchElement($activity, 'as:published', '@value'); } if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) { $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid'); } - if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) { - $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo'); - } + $object_data['service'] = JsonLD::fetchElement($activity, 'as:instrument', 'as:name', '@type', 'as:Service'); - if (!empty($activity['instrument'])) { - $object_data['service'] = JsonLD::fetchElement($activity, 'instrument', 'name', 'type', 'Service'); - } return $object_data; } @@ -700,10 +687,13 @@ class Receiver $object_data['content'] = JsonLD::fetchElement($object, 'as:content'); $object_data['source'] = JsonLD::fetchElement($object, 'as:source', 'as:content', 'as:mediaType', 'text/bbcode'); $object_data['location'] = JsonLD::fetchElement($object, 'as:location', 'as:name', '@type', 'as:Place'); + $object_data['latitude'] = JsonLD::fetchElement($object, 'as:location', 'as:latitude', '@type', 'as:Place'); + $object_data['latitude'] = JsonLD::fetchElement($object_data, 'latitude', '@value'); + $object_data['longitude'] = JsonLD::fetchElement($object, 'as:location', 'as:longitude', '@type', 'as:Place'); + $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value'); $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment')); $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag')); -// $object_data['service'] = JsonLD::fetchElement($object, 'instrument', 'name', 'type', 'Service'); // todo - $object_data['service'] = null; + $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application'); $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url'); // Special treatment for Hubzilla links @@ -723,7 +713,7 @@ class Receiver // @context, type, actor, signature, mediaType, duration, replies, icon // Also missing: (Defined in the standard, but currently unused) - // audience, preview, endTime, startTime, generator, image + // audience, preview, endTime, startTime, image // Data in Notes: