Switching to parsing the JSON-LD

This commit is contained in:
Michael 2018-10-07 15:34:51 +00:00
parent 3a74f3364d
commit 36c31e4091
2 changed files with 60 additions and 46 deletions

View file

@ -138,7 +138,7 @@ class Processor
{ {
$item = []; $item = [];
$item['verb'] = ACTIVITY_LIKE; $item['verb'] = ACTIVITY_LIKE;
$item['parent-uri'] = $activity['object']; $item['parent-uri'] = JsonLD::fetchElement($activity, 'object');
$item['gravity'] = GRAVITY_ACTIVITY; $item['gravity'] = GRAVITY_ACTIVITY;
$item['object-type'] = ACTIVITY_OBJ_NOTE; $item['object-type'] = ACTIVITY_OBJ_NOTE;
@ -154,7 +154,7 @@ class Processor
public static function deleteItem($activity) public static function deleteItem($activity)
{ {
$owner = Contact::getIdForURL($activity['owner']); $owner = Contact::getIdForURL($activity['owner']);
$object = JsonLD::fetchElement($activity, 'object', 'id'); $object = JsonLD::fetchElement($activity, 'object');
logger('Deleting item ' . $object . ' from ' . $owner, LOGGER_DEBUG); logger('Deleting item ' . $object . ' from ' . $owner, LOGGER_DEBUG);
Item::delete(['uri' => $object, 'owner-id' => $owner]); Item::delete(['uri' => $object, 'owner-id' => $owner]);
} }
@ -169,7 +169,7 @@ class Processor
{ {
$item = []; $item = [];
$item['verb'] = ACTIVITY_DISLIKE; $item['verb'] = ACTIVITY_DISLIKE;
$item['parent-uri'] = $activity['object']; $item['parent-uri'] = JsonLD::fetchElement($activity, 'object');
$item['gravity'] = GRAVITY_ACTIVITY; $item['gravity'] = GRAVITY_ACTIVITY;
$item['object-type'] = ACTIVITY_OBJ_NOTE; $item['object-type'] = ACTIVITY_OBJ_NOTE;
@ -261,7 +261,8 @@ class Processor
$activity['published'] = $object['published']; $activity['published'] = $object['published'];
$activity['type'] = 'Create'; $activity['type'] = 'Create';
ActivityPub\Receiver::processActivity($activity); $ldactivity = JsonLD::compact($activity);
ActivityPub\Receiver::processActivity($activity, $ldactivity);
logger('Activity ' . $url . ' had been fetched and processed.'); logger('Activity ' . $url . ' had been fetched and processed.');
} }
@ -272,7 +273,7 @@ class Processor
*/ */
public static function followUser($activity) public static function followUser($activity)
{ {
$actor = JsonLD::fetchElement($activity, 'object', 'id'); $actor = JsonLD::fetchElement($activity, 'object');
$uid = User::getIdForURL($actor); $uid = User::getIdForURL($actor);
if (empty($uid)) { if (empty($uid)) {
return; return;
@ -312,12 +313,13 @@ class Processor
*/ */
public static function updatePerson($activity) public static function updatePerson($activity)
{ {
if (empty($activity['object']['id'])) { $actor = JsonLD::fetchElement($activity, 'object');
if ($actor) {
return; return;
} }
logger('Updating profile for ' . $activity['object']['id'], LOGGER_DEBUG); logger('Updating profile for ' . $actor, LOGGER_DEBUG);
APContact::getByURL($activity['object']['id'], true); APContact::getByURL($actor, true);
} }
/** /**
@ -327,23 +329,26 @@ class Processor
*/ */
public static function deletePerson($activity) public static function deletePerson($activity)
{ {
if (empty($activity['object']['id']) || empty($activity['object']['actor'])) { $id = JsonLD::fetchElement($activity, 'object');
$actor = JsonLD::fetchElement($activity, 'object', 'as:actor');
if (empty($id) || empty($actor)) {
logger('Empty object id or actor.', LOGGER_DEBUG); logger('Empty object id or actor.', LOGGER_DEBUG);
return; return;
} }
if ($activity['object']['id'] != $activity['object']['actor']) { if ($id != $actor) {
logger('Object id does not match actor.', LOGGER_DEBUG); logger('Object id does not match actor.', LOGGER_DEBUG);
return; return;
} }
$contacts = DBA::select('contact', ['id'], ['nurl' => normalise_link($activity['object']['id'])]); $contacts = DBA::select('contact', ['id'], ['nurl' => normalise_link($id)]);
while ($contact = DBA::fetch($contacts)) { while ($contact = DBA::fetch($contacts)) {
Contact::remove($contact["id"]); Contact::remove($contact['id']);
} }
DBA::close($contacts); DBA::close($contacts);
logger('Deleted contact ' . $activity['object']['id'], LOGGER_DEBUG); logger('Deleted contact ' . $id, LOGGER_DEBUG);
} }
/** /**
@ -353,7 +358,7 @@ class Processor
*/ */
public static function acceptFollowUser($activity) public static function acceptFollowUser($activity)
{ {
$actor = JsonLD::fetchElement($activity, 'object', 'actor'); $actor = JsonLD::fetchElement($activity, 'object', 'as:actor');
$uid = User::getIdForURL($actor); $uid = User::getIdForURL($actor);
if (empty($uid)) { if (empty($uid)) {
return; return;
@ -386,7 +391,7 @@ class Processor
*/ */
public static function rejectFollowUser($activity) public static function rejectFollowUser($activity)
{ {
$actor = JsonLD::fetchElement($activity, 'object', 'actor'); $actor = JsonLD::fetchElement($activity, 'object', 'as:actor');
$uid = User::getIdForURL($actor); $uid = User::getIdForURL($actor);
if (empty($uid)) { if (empty($uid)) {
return; return;
@ -415,12 +420,12 @@ class Processor
*/ */
public static function undoActivity($activity) public static function undoActivity($activity)
{ {
$activity_url = JsonLD::fetchElement($activity, 'object', 'id'); $activity_url = JsonLD::fetchElement($activity, 'object');
if (empty($activity_url)) { if (empty($activity_url)) {
return; return;
} }
$actor = JsonLD::fetchElement($activity, 'object', 'actor'); $actor = JsonLD::fetchElement($activity, 'object', 'as:actor');
if (empty($actor)) { if (empty($actor)) {
return; return;
} }
@ -440,7 +445,7 @@ class Processor
*/ */
public static function undoFollowUser($activity) public static function undoFollowUser($activity)
{ {
$object = JsonLD::fetchElement($activity, 'object', 'object'); $object = JsonLD::fetchElement($activity, 'object', 'as:object');
$uid = User::getIdForURL($object); $uid = User::getIdForURL($object);
if (empty($uid)) { if (empty($uid)) {
return; return;

View file

@ -150,7 +150,7 @@ 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'])) {
$object_data = self::fetchObject($object_id, $activity['object'], $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);
return []; return [];
@ -168,13 +168,13 @@ class Receiver
} else { } else {
$object_data = []; $object_data = [];
$object_data['id'] = JsonLD::fetchElement($ldactivity, '@id'); $object_data['id'] = JsonLD::fetchElement($ldactivity, '@id');
$object_data['object'] = $activity['object']; $object_data['object'] = $ldactivity['as:object'];
$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, $activity);
$object_data['type'] = $activity['type']; $object_data['type'] = $type;
$object_data['owner'] = $actor; $object_data['owner'] = $actor;
$object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers); $object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);
@ -466,50 +466,49 @@ class Receiver
* *
* @return array with object data * @return array with object data
*/ */
private static function fetchObject($object_id, $object = [], $ldobject = [], $trust_source = false) private static function fetchObject($object_id, $object = [], $trust_source = false)
{ {
if (!$trust_source || is_string($object)) { if (!$trust_source || empty($object)) {
$data = ActivityPub::fetchContent($object_id); $data = ActivityPub::fetchContent($object_id);
if (empty($data)) { if (!empty($data)) {
logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG); $object = JsonLD::compact($data);
$data = $object_id;
} else {
$ldobject = JsonLD::compact($data);
logger('Fetched content for ' . $object_id, LOGGER_DEBUG); logger('Fetched content for ' . $object_id, LOGGER_DEBUG);
}
} else { } else {
logger('Using original object for url ' . $object_id, LOGGER_DEBUG); logger('Empty content for ' . $object_id . ', check if content is available locally.', LOGGER_DEBUG);
$data = $object;
}
if (is_string($data)) { $item = Item::selectFirst([], ['uri' => $object_id]);
$item = Item::selectFirst([], ['uri' => $data]);
if (!DBA::isResult($item)) { if (!DBA::isResult($item)) {
logger('Object with url ' . $data . ' was not found locally.', LOGGER_DEBUG); logger('Object with url ' . $object_id . ' was not found locally.', LOGGER_DEBUG);
return false; return false;
} }
logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG); logger('Using already stored item for url ' . $object_id, LOGGER_DEBUG);
$data = ActivityPub\Transmitter::createNote($item); $data = ActivityPub\Transmitter::createNote($item);
$ldobject = JsonLD::compact($data); $object = JsonLD::compact($data);
}
} else {
logger('Using original object for url ' . $object_id, LOGGER_DEBUG);
} }
if (empty($data['type'])) { $type = JsonLD::fetchElement($object, '@type');
if (empty($type)) {
logger('Empty type', LOGGER_DEBUG); logger('Empty type', LOGGER_DEBUG);
return false; return false;
} }
if (in_array($data['type'], ActivityPub::CONTENT_TYPES)) { if (in_array($type, self::CONTENT_TYPES)) {
return self::processObject($ldobject); return self::processObject($object);
} }
if ($data['type'] == 'Announce') { if ($type == 'as:Announce') {
if (empty($data['object'])) { $object_id = JsonLD::fetchElement($object, 'object');
if (empty($object_id)) {
return false; return false;
} }
return self::fetchObject($data['object']); return self::fetchObject($object_id);
} }
logger('Unhandled object type: ' . $data['type'], LOGGER_DEBUG); logger('Unhandled object type: ' . $type, LOGGER_DEBUG);
} }
/** /**
@ -522,7 +521,12 @@ class Receiver
private static function processTags($tags) private static function processTags($tags)
{ {
$taglist = []; $taglist = [];
foreach ($tags as $tag) { foreach ($tags as $tag) {
if (empty($tag)) {
continue;
}
$taglist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')), $taglist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
'href' => JsonLD::fetchElement($tag, 'as:href'), 'href' => JsonLD::fetchElement($tag, 'as:href'),
'name' => JsonLD::fetchElement($tag, 'as:name')]; 'name' => JsonLD::fetchElement($tag, 'as:name')];
@ -540,7 +544,12 @@ class Receiver
private static function processAttachments($attachments) private static function processAttachments($attachments)
{ {
$attachlist = []; $attachlist = [];
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
if (empty($attachment)) {
continue;
}
$attachlist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')), $attachlist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($attachment, '@type')),
'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType'), 'mediaType' => JsonLD::fetchElement($attachment, 'as:mediaType'),
'name' => JsonLD::fetchElement($attachment, 'as:name'), 'name' => JsonLD::fetchElement($attachment, 'as:name'),