]> git.mxchange.org Git - friendica.git/commitdiff
Improved parsing of AP profiles
authorMichael <heluecht@pirati.ca>
Mon, 22 Oct 2018 07:24:47 +0000 (07:24 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 22 Oct 2018 07:24:47 +0000 (07:24 +0000)
src/Model/APContact.php
src/Util/JsonLD.php

index 4593e0377b1de25f3d707231e294f89245a4f3c9..72c644ab9ac0d15302d321f4ca0489f4c19c50a0 100644 (file)
@@ -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
index 6f4545565fa7e78ccc9ed1a519364e471681d0cf..793f5e9d1280c727cd9dba8782c07ac8fd1a7225 100644 (file)
@@ -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));