From 5c13ecdf6ecc4433f5b1f3b87334ae3c04da4589 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 22 Oct 2018 07:24:47 +0000 Subject: [PATCH] Improved parsing of AP profiles --- src/Model/APContact.php | 55 +++++++++++++++++++++++++++++------------ src/Util/JsonLD.php | 2 +- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/Model/APContact.php b/src/Model/APContact.php index 4593e0377b..72c644ab9a 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -99,32 +99,55 @@ class APContact extends BaseObject } $data = ActivityPub::fetchContent($url); - - if (empty($data) || empty($data['id']) || empty($data['inbox'])) { + if (empty($data)) { return false; } + $compacted = JsonLD::compact($data); + $apcontact = []; - $apcontact['url'] = $data['id']; - $apcontact['uuid'] = defaults($data, 'diaspora:guid', null); - $apcontact['type'] = defaults($data, 'type', null); - $apcontact['following'] = defaults($data, 'following', null); - $apcontact['followers'] = defaults($data, 'followers', null); - $apcontact['inbox'] = defaults($data, 'inbox', null); - $apcontact['outbox'] = defaults($data, 'outbox', null); - $apcontact['sharedinbox'] = JsonLD::fetchElement($data, 'endpoints', 'sharedInbox'); - $apcontact['nick'] = defaults($data, 'preferredUsername', null); - $apcontact['name'] = defaults($data, 'name', $apcontact['nick']); - $apcontact['about'] = HTML::toBBCode(defaults($data, 'summary', '')); - $apcontact['photo'] = JsonLD::fetchElement($data, 'icon', 'url'); - $apcontact['alias'] = JsonLD::fetchElement($data, 'url', 'href'); + $apcontact['url'] = $compacted['@id']; + $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid'); + $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type')); + $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id'); + $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id'); + $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id'); + $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id'); + + $apcontact['sharedinbox'] = ''; + if (!empty($compacted['as:endpoints'])) { + $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id'); + } + + $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername'); + $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name'); + + if (empty($apcontact['name'])) { + $apcontact['name'] = $apcontact['nick']; + } + + $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary')); + + $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id'); + if (is_array($apcontact['photo'])) { + $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id'); + } + + $apcontact['alias'] = JsonLD::fetchElement($compacted, 'as:url', '@id'); + if (is_array($apcontact['alias'])) { + $apcontact['alias'] = JsonLD::fetchElement($compacted['as:url'], 'as:href', '@id'); + } + + if (empty($apcontact['url']) || empty($apcontact['inbox'])) { + return false; + } $parts = parse_url($apcontact['url']); unset($parts['scheme']); unset($parts['path']); $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts)); - $apcontact['pubkey'] = trim(JsonLD::fetchElement($data, 'publicKey', 'publicKeyPem')); + $apcontact['pubkey'] = trim(JsonLD::fetchElement($compacted, 'w3id:publicKey', 'w3id:publicKeyPem')); // To-Do // manuallyApprovesFollowers diff --git a/src/Util/JsonLD.php b/src/Util/JsonLD.php index 6f4545565f..793f5e9d12 100644 --- a/src/Util/JsonLD.php +++ b/src/Util/JsonLD.php @@ -88,7 +88,7 @@ class JsonLD 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'], 'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'], 'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'], - 'uuid' => (object)['@id' => 'http://schema.org/identifier', '@type' => '@id']]; + 'ldp' => (object)['@id' => 'http://www.w3.org/ns/ldp#', '@type' => '@id']]; $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); -- 2.39.5