Detect the object type

This commit is contained in:
Michael 2018-10-07 20:36:15 +00:00
parent 93866b477a
commit 46f77f3486
2 changed files with 72 additions and 5 deletions

View file

@ -30,9 +30,6 @@ use Friendica\Protocol\ActivityPub;
* - Remove
* - Undo Block
* - Undo Accept (Problem: This could invert a contact accept or an event accept)
*
* General:
* - Possibly using the LD-JSON parser
*/
class Receiver
{
@ -111,6 +108,44 @@ class Receiver
self::processActivity($ldactivity, $body, $uid, $trust_source);
}
/**
* Fetches the object type for a given object id
*
* @param array $activity
* @param string $object_id Object ID of the the provided object
*
* @return string with object type
*/
private static function fetchObjectType($activity, $object_id)
{
$object_type = JsonLD::fetchElement($activity['as:object'], '@type');
if (!empty($object_type)) {
return $object_type;
}
if (Item::exists(['uri' => $object_id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]])) {
// We just assume "note" since it doesn't make a difference for the further processing
return 'as:Note';
}
$profile = APContact::getByURL($object_id);
if (!empty($profile['type'])) {
return 'as:' . $profile['type'];
}
$data = ActivityPub::fetchContent($object_id);
if (!empty($data)) {
$object = JsonLD::compact($data);
$type = JsonLD::fetchElement($object, '@type');
if (!empty($type)) {
return $type;
}
}
return null;
}
/**
* Prepare the object array
*
@ -148,6 +183,8 @@ class Receiver
return [];
}
$object_type = self::fetchObjectType($activity, $object_id);
// Fetch the content only on activities where this matters
if (in_array($type, ['as:Create', 'as:Announce'])) {
if ($type == 'as:Announce') {
@ -179,6 +216,10 @@ class Receiver
$object_data = self::addActivityFields($object_data, $activity);
if (empty($object_data['object_type'])) {
$object_data['object_type'] = $object_type;
}
$object_data['type'] = $type;
$object_data['actor'] = $actor;
$object_data['receiver'] = array_merge(defaults($object_data, 'receiver', []), $receivers);