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

View file

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