The new parsing is now active

This commit is contained in:
Michael 2018-10-07 17:17:06 +00:00
parent 36c31e4091
commit 616c4a89bd
4 changed files with 39 additions and 28 deletions

View file

@ -26,7 +26,7 @@ class Inbox extends BaseModule
} }
// Enable for test purposes // Enable for test purposes
/*
if (HTTPSignature::getSigner($postdata, $_SERVER)) { if (HTTPSignature::getSigner($postdata, $_SERVER)) {
$filename = 'signed-activitypub'; $filename = 'signed-activitypub';
} else { } else {
@ -37,7 +37,7 @@ class Inbox extends BaseModule
file_put_contents($tempfile, json_encode(['argv' => $a->argv, 'header' => $_SERVER, 'body' => $postdata], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); file_put_contents($tempfile, json_encode(['argv' => $a->argv, 'header' => $_SERVER, 'body' => $postdata], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
logger('Incoming message stored under ' . $tempfile); logger('Incoming message stored under ' . $tempfile);
*/
if (!empty($a->argv[1])) { if (!empty($a->argv[1])) {
$user = DBA::selectFirst('user', ['uid'], ['nickname' => $a->argv[1]]); $user = DBA::selectFirst('user', ['uid'], ['nickname' => $a->argv[1]]);
if (!DBA::isResult($user)) { if (!DBA::isResult($user)) {

View file

@ -134,7 +134,8 @@ class ActivityPub
} }
foreach ($items as $activity) { foreach ($items as $activity) {
ActivityPub\Receiver::processActivity($activity, '', $uid, true); $ldactivity = JsonLD::compact($activity);
ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true);
} }
} }
} }

View file

@ -262,7 +262,7 @@ class Processor
$activity['type'] = 'Create'; $activity['type'] = 'Create';
$ldactivity = JsonLD::compact($activity); $ldactivity = JsonLD::compact($activity);
ActivityPub\Receiver::processActivity($activity, $ldactivity); ActivityPub\Receiver::processActivity($ldactivity);
logger('Activity ' . $url . ' had been fetched and processed.'); logger('Activity ' . $url . ' had been fetched and processed.');
} }

View file

@ -108,7 +108,7 @@ class Receiver
$trust_source = false; $trust_source = false;
} }
self::processActivity($activity, $ldactivity, $body, $uid, $trust_source); self::processActivity($ldactivity, $body, $uid, $trust_source);
} }
/** /**
@ -120,7 +120,7 @@ class Receiver
* *
* @return * @return
*/ */
private static function prepareObjectData($activity, $ldactivity, $uid, &$trust_source) private static function prepareObjectData($ldactivity, $uid, &$trust_source)
{ {
$actor = JsonLD::fetchElement($ldactivity, 'as:actor'); $actor = JsonLD::fetchElement($ldactivity, 'as:actor');
if (empty($actor)) { if (empty($actor)) {
@ -150,6 +150,9 @@ class Receiver
// Fetch the content only on activities where this matters // Fetch the content only on activities where this matters
if (in_array($type, ['as:Create', 'as:Announce'])) { if (in_array($type, ['as:Create', 'as:Announce'])) {
if ($type == 'as:Announce') {
$trust_source = false;
}
$object_data = self::fetchObject($object_id, $ldactivity['as:object'], $trust_source); $object_data = self::fetchObject($object_id, $ldactivity['as:object'], $trust_source);
if (empty($object_data)) { if (empty($object_data)) {
logger("Object data couldn't be processed", LOGGER_DEBUG); logger("Object data couldn't be processed", LOGGER_DEBUG);
@ -172,7 +175,7 @@ class Receiver
$object_data['object_type'] = JsonLD::fetchElement($ldactivity, 'as:object', '@type'); $object_data['object_type'] = JsonLD::fetchElement($ldactivity, 'as:object', '@type');
} }
$object_data = self::addActivityFields($object_data, $activity); $object_data = self::addActivityFields($object_data, $ldactivity);
$object_data['type'] = $type; $object_data['type'] = $type;
$object_data['owner'] = $actor; $object_data['owner'] = $actor;
@ -184,19 +187,15 @@ class Receiver
} }
/** /**
* Processes the activity object
* *
* * @param array $activity Array with activity data
* @param array $activity * @param string $body
* @param $body
* @param integer $uid User ID * @param integer $uid User ID
* @param $trust_source * @param boolean $trust_source Do we trust the source?
*/ */
public static function processActivity($activity, $ldactivity = '', $body = '', $uid = null, $trust_source = false) public static function processActivity($ldactivity, $body = '', $uid = null, $trust_source = false)
{ {
if (empty($ldactivity)) {
$ldactivity = JsonLD::compact($activity);
}
$type = JsonLD::fetchElement($ldactivity, '@type'); $type = JsonLD::fetchElement($ldactivity, '@type');
if (!$type) { if (!$type) {
logger('Empty type', LOGGER_DEBUG); logger('Empty type', LOGGER_DEBUG);
@ -215,7 +214,7 @@ class Receiver
} }
// $trust_source is called by reference and is set to true if the content was retrieved successfully // $trust_source is called by reference and is set to true if the content was retrieved successfully
$object_data = self::prepareObjectData($activity, $ldactivity, $uid, $trust_source); $object_data = self::prepareObjectData($ldactivity, $uid, $trust_source);
if (empty($object_data)) { if (empty($object_data)) {
logger('No object data found', LOGGER_DEBUG); logger('No object data found', LOGGER_DEBUG);
return; return;
@ -436,19 +435,19 @@ class Receiver
private static function addActivityFields($object_data, $activity) private static function addActivityFields($object_data, $activity)
{ {
if (!empty($activity['published']) && empty($object_data['published'])) { if (!empty($activity['published']) && empty($object_data['published'])) {
$object_data['published'] = $activity['published']; $object_data['published'] = JsonLD::fetchElement($activity, 'published', '@value');
} }
if (!empty($activity['updated']) && empty($object_data['updated'])) { if (!empty($activity['updated']) && empty($object_data['updated'])) {
$object_data['updated'] = $activity['updated']; $object_data['updated'] = JsonLD::fetchElement($activity, 'updated', '@value');
} }
if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) { if (!empty($activity['diaspora:guid']) && empty($object_data['diaspora:guid'])) {
$object_data['diaspora:guid'] = $activity['diaspora:guid']; $object_data['diaspora:guid'] = JsonLD::fetchElement($activity, 'diaspora:guid');
} }
if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) { if (!empty($activity['inReplyTo']) && empty($object_data['parent-uri'])) {
$object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo', 'id'); $object_data['parent-uri'] = JsonLD::fetchElement($activity, 'inReplyTo');
} }
if (!empty($activity['instrument'])) { if (!empty($activity['instrument'])) {
@ -458,17 +457,20 @@ class Receiver
} }
/** /**
* Fetches the object data from external ressources if needed
* *
* @param string $object_id Object ID of the the provided object
* @param array $object The provided object array
* @param boolean $trust_source Do we trust the provided object?
* *
* @param $object_id * @return array with trusted and valid object data
* @param $object
* @param $trust_source
*
* @return array with object data
*/ */
private static function fetchObject($object_id, $object = [], $trust_source = false) private static function fetchObject($object_id, $object = [], $trust_source = false)
{ {
if (!$trust_source || empty($object)) { // By fetching the type we check if the object is complete.
$type = JsonLD::fetchElement($object, '@type');
if (!$trust_source || empty($type)) {
$data = ActivityPub::fetchContent($object_id); $data = ActivityPub::fetchContent($object_id);
if (!empty($data)) { if (!empty($data)) {
$object = JsonLD::compact($data); $object = JsonLD::compact($data);
@ -522,6 +524,10 @@ class Receiver
{ {
$taglist = []; $taglist = [];
if (empty($tags)) {
return [];
}
foreach ($tags as $tag) { foreach ($tags as $tag) {
if (empty($tag)) { if (empty($tag)) {
continue; continue;
@ -545,6 +551,10 @@ class Receiver
{ {
$attachlist = []; $attachlist = [];
if (empty($attachments)) {
return [];
}
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
if (empty($attachment)) { if (empty($attachment)) {
continue; continue;