Merge pull request #5989 from annando/ap-profile

Improved parsing of AP profiles
This commit is contained in:
Hypolite Petovan 2018-10-22 08:35:59 -04:00 committed by GitHub
commit 43db120967
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 17 deletions

View file

@ -99,32 +99,55 @@ class APContact extends BaseObject
} }
$data = ActivityPub::fetchContent($url); $data = ActivityPub::fetchContent($url);
if (empty($data)) {
if (empty($data) || empty($data['id']) || empty($data['inbox'])) {
return false; return false;
} }
$compacted = JsonLD::compact($data);
$apcontact = []; $apcontact = [];
$apcontact['url'] = $data['id']; $apcontact['url'] = $compacted['@id'];
$apcontact['uuid'] = defaults($data, 'diaspora:guid', null); $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid');
$apcontact['type'] = defaults($data, 'type', null); $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type'));
$apcontact['following'] = defaults($data, 'following', null); $apcontact['following'] = JsonLD::fetchElement($compacted, 'as:following', '@id');
$apcontact['followers'] = defaults($data, 'followers', null); $apcontact['followers'] = JsonLD::fetchElement($compacted, 'as:followers', '@id');
$apcontact['inbox'] = defaults($data, 'inbox', null); $apcontact['inbox'] = JsonLD::fetchElement($compacted, 'ldp:inbox', '@id');
$apcontact['outbox'] = defaults($data, 'outbox', null); $apcontact['outbox'] = JsonLD::fetchElement($compacted, 'as:outbox', '@id');
$apcontact['sharedinbox'] = JsonLD::fetchElement($data, 'endpoints', 'sharedInbox');
$apcontact['nick'] = defaults($data, 'preferredUsername', null); $apcontact['sharedinbox'] = '';
$apcontact['name'] = defaults($data, 'name', $apcontact['nick']); if (!empty($compacted['as:endpoints'])) {
$apcontact['about'] = HTML::toBBCode(defaults($data, 'summary', '')); $apcontact['sharedinbox'] = JsonLD::fetchElement($compacted['as:endpoints'], 'as:sharedInbox', '@id');
$apcontact['photo'] = JsonLD::fetchElement($data, 'icon', 'url'); }
$apcontact['alias'] = JsonLD::fetchElement($data, 'url', 'href');
$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']); $parts = parse_url($apcontact['url']);
unset($parts['scheme']); unset($parts['scheme']);
unset($parts['path']); unset($parts['path']);
$apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts)); $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 // To-Do
// manuallyApprovesFollowers // manuallyApprovesFollowers

View file

@ -88,7 +88,7 @@ class JsonLD
'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'], 'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'], 'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@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)); $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));