Improved message handling / new activity relay handling

This commit is contained in:
Michael
2022-07-27 17:39:00 +00:00
parent 7834359957
commit 86105635ca
26 changed files with 280 additions and 428 deletions

View File

@@ -110,7 +110,10 @@ class Delivery
} elseif ($cmd == WorkerDelivery::PROFILEUPDATE) {
$success = ActivityPub\Transmitter::sendProfileUpdate($uid, $inbox);
} else {
$data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
$data = Post\Activity::getByURIId($uri_id);
if (empty($data)) {
$data = ActivityPub\Transmitter::createCachedActivityFromItem($item_id);
}
if (!empty($data)) {
$timestamp = microtime(true);
$response = HTTPSignature::post($data, $inbox, $uid);

View File

@@ -361,8 +361,6 @@ class Processor
if (!empty($activity['raw'])) {
$item['source'] = $activity['raw'];
$item['protocol'] = Conversation::PARCEL_ACTIVITYPUB;
$item['conversation-href'] = $activity['context'] ?? '';
$item['conversation-uri'] = $activity['conversation'] ?? '';
if (isset($activity['push'])) {
$item['direction'] = $activity['push'] ? Conversation::PUSH : Conversation::PULL;
@@ -475,7 +473,19 @@ class Processor
}
// @todo To ensure that the remote system is working correctly, we can check if the "Content-Type" contains JSON
return in_array($curlResult->getReturnCode(), [404]);
if (in_array($curlResult->getReturnCode(), [404])) {
return true;
}
$object = json_decode($curlResult->getBody(), true);
if (!empty($object)) {
$activity = JsonLD::compact($object);
if (JsonLD::fetchElement($activity, '@type') == 'as:Tombstone') {
return true;
}
}
return false;
}
/**
* Delete items

View File

@@ -25,7 +25,9 @@ use Friendica\Core\Logger;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Protocol\ActivityPub;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\JsonLD;
/**
* This class handles the processing of incoming posts
@@ -214,6 +216,7 @@ class Queue
Logger::debug('Process leftover entry', $entry);
self::process($entry['id']);
}
DBA::close($entries);
}
/**
@@ -247,5 +250,58 @@ class Queue
while ($entry = DBA::fetch($entries)) {
self::process($entry['id']);
}
DBA::close($entries);
}
/**
* Prepare the queue entry.
* This is a test function that is used solely for development.
*
* @param integer $id
* @return array
*/
public static function reprepareActivityById(int $id): array
{
$entry = DBA::selectFirst('inbox-entry', [], ['id' => $id]);
if (empty($entry)) {
return [];
}
$receiver = DBA::selectFirst('inbox-entry-receiver', ['uid'], ['queue-id' => $id]);
if (!empty($receiver)) {
$uid = $receiver['uid'];
} else {
$uid = 0;
}
$trust_source = $entry['trust'];
$data = json_decode($entry['activity'], true);
$activity = json_decode($data['raw'], true);
$ldactivity = JsonLD::compact($activity);
return [
'data' => Receiver::prepareObjectData($ldactivity, $uid, $entry['push'], $trust_source),
'trust' => $trust_source
];
}
/**
* Set the trust for all untrusted entries.
* This is a test function that is used solely for development.
*
* @return void
*/
public static function reprepareAll()
{
$entries = DBA::select('inbox-entry', ['id'], ["NOT `trust` AND `wid` IS NULL"], ['order' => ['id' => true]]);
while ($entry = DBA::fetch($entries)) {
$data = self::reprepareActivityById($entry['id'], false);
if ($data['trust']) {
DBA::update('inbox-entry', ['trust' => true], ['id' => $entry['id']]);
}
}
DBA::close($entries);
}
}

View File

@@ -272,26 +272,39 @@ class Receiver
*/
public static function prepareObjectData(array $activity, int $uid, bool $push, bool &$trust_source): array
{
$id = JsonLD::fetchElement($activity, '@id');
$id = JsonLD::fetchElement($activity, '@id');
$type = JsonLD::fetchElement($activity, '@type');
$object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
if (!empty($object_id) && in_array($type, ['as:Create', 'as:Update'])) {
$fetch_id = $object_id;
} else {
$fetch_id = $id;
}
if (!empty($activity['as:object'])) {
$object_type = JsonLD::fetchElement($activity['as:object'], '@type');
}
if (!empty($id) && !$trust_source) {
$fetch_uid = $uid ?: self::getBestUserForActivity($activity);
$fetched_activity = ActivityPub::fetchContent($id, $fetch_uid);
$fetched_activity = ActivityPub::fetchContent($fetch_id, $fetch_uid);
if (!empty($fetched_activity)) {
$object = JsonLD::compact($fetched_activity);
$fetched_id = JsonLD::fetchElement($object, '@id');
if ($fetched_id == $id) {
$fetched_id = JsonLD::fetchElement($object, '@id');
$fetched_type = JsonLD::fetchElement($object, '@type');
if (($fetched_id == $id) && !empty($fetched_type) && ($fetched_type == $type)) {
Logger::info('Activity had been fetched successfully', ['id' => $id]);
$trust_source = true;
if ($id != $object_id) {
$activity = $object;
} else {
Logger::info('Fetched data is the object instead of the activity', ['id' => $id]);
unset($object['@context']);
$activity['as:object'] = $object;
}
$activity = $object;
} elseif (($fetched_id == $object_id) && !empty($fetched_type) && ($fetched_type == $object_type)) {
Logger::info('Fetched data is the object instead of the activity', ['id' => $id]);
$trust_source = true;
unset($object['@context']);
$activity['as:object'] = $object;
} else {
Logger::info('Activity id is not equal', ['id' => $id, 'fetched' => $fetched_id]);
}
@@ -371,9 +384,9 @@ class Receiver
$object_data['object_object'] = JsonLD::fetchElement($activity['as:object'], 'as:object');
$object_data['object_type'] = JsonLD::fetchElement($activity['as:object'], '@type');
$object_data['push'] = $push;
if ($type == 'as:Delete') {
if (!$trust_source && ($type == 'as:Delete')) {
$apcontact = APContact::getByURL($object_data['object_id'], true);
$trust_source = ($apcontact['type'] == 'Tombstone');
$trust_source = empty($apcontact) || ($apcontact['type'] == 'Tombstone') || $apcontact['suspended'];
}
} elseif (in_array($type, ['as:Create', 'as:Update', 'as:Announce', 'as:Invite']) || strpos($type, '#emojiReaction')) {
// Fetch the content only on activities where this matters
@@ -430,8 +443,11 @@ class Receiver
$object_data['object_object_type'] = self::fetchObjectType([], $object_data['object_object'], $fetch_uid);
}
if (($type == 'as:Delete') && in_array($object_data['object_type'], array_merge(['as:Tombstone'], self::CONTENT_TYPES))) {
if (!$trust_source && ($type == 'as:Delete') && in_array($object_data['object_type'], array_merge(['as:Tombstone', ''], self::CONTENT_TYPES))) {
$trust_source = Processor::isActivityGone($object_data['object_id']);
if (!$trust_source) {
$trust_source = !empty(APContact::getByURL($object_data['object_id'], false));
}
}
}
@@ -668,6 +684,9 @@ class Receiver
if (!empty($object_data['raw'])) {
$announce_object_data['raw'] = $object_data['raw'];
}
if (!empty($object_data['raw-object'])) {
$announce_object_data['raw-object'] = $object_data['raw-object'];
}
ActivityPub\Processor::createActivity($announce_object_data, Activity::ANNOUNCE);
}
} else {
@@ -1305,7 +1324,7 @@ class Receiver
$object_data = self::processObject($object);
if (!empty($data)) {
$object_data['raw'] = json_encode($data);
$object_data['raw-object'] = json_encode($data);
}
return $object_data;
}

View File

@@ -1232,10 +1232,8 @@ class Transmitter
}
if (!$item['deleted']) {
$condition = ['item-uri' => $item['uri'], 'protocol' => Conversation::PARCEL_ACTIVITYPUB];
$conversation = DBA::selectFirst('conversation', ['source'], $condition);
if (!$item['origin'] && DBA::isResult($conversation)) {
$data = json_decode($conversation['source'], true);
$data = Post\Activity::getByURIId($item['uri-id']);
if (!$item['origin'] && !empty($data)) {
if (!empty($data['type'])) {
if (in_array($data['type'], ['Create', 'Update'])) {
if ($object_mode) {

View File

@@ -810,27 +810,12 @@ class DFRN
}
// Add conversation data. This is used for OStatus
$conversation_href = DI::baseUrl()."/display/".$item["parent-guid"];
$conversation_uri = $conversation_href;
if (isset($parent_item)) {
$conversation = DBA::selectFirst('conversation', ['conversation-uri', 'conversation-href'], ['item-uri' => $item['thr-parent']]);
if (DBA::isResult($conversation)) {
if ($conversation['conversation-uri'] != '') {
$conversation_uri = $conversation['conversation-uri'];
}
if ($conversation['conversation-href'] != '') {
$conversation_href = $conversation['conversation-href'];
}
}
}
$attributes = [
'href' => $conversation_href,
'ref' => $conversation_uri,
'href' => $item['conversation'],
'ref' => $item['conversation'],
];
XML::addElement($doc, $entry, 'ostatus:conversation', $conversation_uri, $attributes);
XML::addElement($doc, $entry, 'ostatus:conversation', $item['conversation'], $attributes);
XML::addElement($doc, $entry, 'id', $item['uri']);
XML::addElement($doc, $entry, 'title', $item['title']);
@@ -1994,16 +1979,16 @@ class DFRN
self::parseLinks($links, $item);
}
$item['conversation-uri'] = XML::getFirstNodeValue($xpath, 'ostatus:conversation/text()', $entry);
$item['conversation'] = XML::getFirstNodeValue($xpath, 'ostatus:conversation/text()', $entry);
$conv = $xpath->query('ostatus:conversation', $entry);
if (is_object($conv->item(0))) {
foreach ($conv->item(0)->attributes as $attributes) {
if ($attributes->name == 'ref') {
$item['conversation-uri'] = $attributes->textContent;
$item['conversation'] = $attributes->textContent;
}
if ($attributes->name == 'href') {
$item['conversation-href'] = $attributes->textContent;
$item['conversation'] = $attributes->textContent;
}
}
}

View File

@@ -616,16 +616,16 @@ class OStatus
$item['created'] = XML::getFirstNodeValue($xpath, 'atom:published/text()', $entry);
$item['edited'] = XML::getFirstNodeValue($xpath, 'atom:updated/text()', $entry);
$item['conversation-uri'] = XML::getFirstNodeValue($xpath, 'ostatus:conversation/text()', $entry);
$item['conversation'] = XML::getFirstNodeValue($xpath, 'ostatus:conversation/text()', $entry);
$conv = $xpath->query('ostatus:conversation', $entry);
if (is_object($conv->item(0))) {
foreach ($conv->item(0)->attributes as $attributes) {
if ($attributes->name == 'ref') {
$item['conversation-uri'] = $attributes->textContent;
$item['conversation'] = $attributes->textContent;
}
if ($attributes->name == 'href') {
$item['conversation-href'] = $attributes->textContent;
$item['conversation'] = $attributes->textContent;
}
}
}
@@ -704,14 +704,6 @@ class OStatus
}
}
if (($self != '') && empty($item['protocol'])) {
self::fetchSelf($self, $item);
}
if (!empty($item['conversation-href'])) {
self::fetchConversation($item['conversation-href'], $item['conversation-uri']);
}
if (isset($item['thr-parent'])) {
if (!Post::exists(['uid' => $importer['uid'], 'uri' => $item['thr-parent']])) {
if ($related != '') {
@@ -725,194 +717,9 @@ class OStatus
$item['gravity'] = GRAVITY_PARENT;
}
if (($item['author-link'] != '') && !empty($item['protocol'])) {
$item = Conversation::insert($item);
}
self::$itemlist[] = $item;
}
/**
* Fetch the conversation for posts
*
* @param string $conversation The link to the conversation
* @param string $conversation_uri The conversation in "uri" format
* @return void
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
private static function fetchConversation(string $conversation, string $conversation_uri)
{
// Ensure that we only store a conversation once in a process
if (isset(self::$conv_list[$conversation])) {
return;
}
self::$conv_list[$conversation] = true;
$curlResult = DI::httpClient()->get($conversation, HttpClientAccept::ATOM_XML);
if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
return;
}
$xml = '';
if ($curlResult->inHeader('Content-Type') &&
in_array('application/atom+xml', $curlResult->getHeader('Content-Type'))) {
$xml = $curlResult->getBody();
}
if ($xml == '') {
$doc = new DOMDocument();
if (!@$doc->loadHTML($curlResult->getBody())) {
return;
}
$xpath = new DOMXPath($doc);
$links = $xpath->query('//link');
if ($links) {
$file = '';
foreach ($links as $link) {
$attribute = self::readAttributes($link);
if (($attribute['rel'] == 'alternate') && ($attribute['type'] == 'application/atom+xml')) {
$file = $attribute['href'];
}
}
if ($file != '') {
$conversation_atom = DI::httpClient()->get($attribute['href'], HttpClientAccept::ATOM_XML);
if ($conversation_atom->isSuccess()) {
$xml = $conversation_atom->getBody();
}
}
}
}
if ($xml == '') {
return;
}
self::storeConversation($xml, $conversation, $conversation_uri);
}
/**
* Store a feed in several conversation entries
*
* @param string $xml The feed
* @param string $conversation conversation
* @param string $conversation_uri conversation uri
* @return void
* @throws \Exception
*/
private static function storeConversation(string $xml, string $conversation = '', string $conversation_uri = '')
{
$doc = new DOMDocument();
@$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('atom', ActivityNamespace::ATOM1);
$xpath->registerNamespace('thr', ActivityNamespace::THREAD);
$xpath->registerNamespace('ostatus', ActivityNamespace::OSTATUS);
$entries = $xpath->query('/atom:feed/atom:entry');
// Now store the entries
foreach ($entries as $entry) {
$doc2 = new DOMDocument();
$doc2->preserveWhiteSpace = false;
$doc2->formatOutput = true;
$conv_data = [];
$conv_data['protocol'] = Conversation::PARCEL_SPLIT_CONVERSATION;
$conv_data['direction'] = Conversation::PULL;
$conv_data['network'] = Protocol::OSTATUS;
$conv_data['uri'] = XML::getFirstNodeValue($xpath, 'atom:id/text()', $entry);
$inreplyto = $xpath->query('thr:in-reply-to', $entry);
if (is_object($inreplyto->item(0))) {
foreach ($inreplyto->item(0)->attributes as $attributes) {
if ($attributes->name == 'ref') {
$conv_data['reply-to-uri'] = $attributes->textContent;
}
}
}
$conv_data['conversation-uri'] = XML::getFirstNodeValue($xpath, 'ostatus:conversation/text()', $entry);
$conv = $xpath->query('ostatus:conversation', $entry);
if (is_object($conv->item(0))) {
foreach ($conv->item(0)->attributes as $attributes) {
if ($attributes->name == 'ref') {
$conv_data['conversation-uri'] = $attributes->textContent;
}
if ($attributes->name == 'href') {
$conv_data['conversation-href'] = $attributes->textContent;
}
}
}
if ($conversation != '') {
$conv_data['conversation-uri'] = $conversation;
}
if ($conversation_uri != '') {
$conv_data['conversation-uri'] = $conversation_uri;
}
$entry = $doc2->importNode($entry, true);
$doc2->appendChild($entry);
$conv_data['source'] = $doc2->saveXML();
Logger::info('Store conversation data for uri '.$conv_data['uri']);
Conversation::insert($conv_data);
}
}
/**
* Fetch the own post so that it can be stored later
*
* We want to store the original data for later processing.
* This function is meant for cases where we process a feed with multiple entries.
* In that case we need to fetch the single posts here.
*
* @param string $self The link to the self item
* @param array $item The item array
* @return void
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
private static function fetchSelf(string $self, array &$item)
{
$condition = ['item-uri' => $self, 'protocol' => [Conversation::PARCEL_DFRN,
Conversation::PARCEL_DIASPORA_DFRN, Conversation::PARCEL_LOCAL_DFRN,
Conversation::PARCEL_DIRECT, Conversation::PARCEL_SALMON]];
if (DBA::exists('conversation', $condition)) {
Logger::info('Conversation '.$item['uri'].' is already stored.');
return;
}
$curlResult = DI::httpClient()->get($self, HttpClientAccept::ATOM_XML);
if (!$curlResult->isSuccess()) {
return;
}
// We reformat the XML to make it better readable
$doc = new DOMDocument();
$doc->loadXML($curlResult->getBody());
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$xml = $doc->saveXML();
$item['protocol'] = Conversation::PARCEL_SALMON;
$item['source'] = $xml;
$item['direction'] = Conversation::PULL;
Logger::info('Conversation '.$item['uri'].' is now fetched.');
}
/**
* Fetch related posts and processes them
*
@@ -925,30 +732,6 @@ class OStatus
*/
private static function fetchRelated(string $related, string $related_uri, array $importer)
{
$condition = [
'item-uri' => $related_uri,
'protocol' => [
Conversation::PARCEL_DFRN,
Conversation::PARCEL_DIASPORA_DFRN,
Conversation::PARCEL_LOCAL_DFRN,
Conversation::PARCEL_DIRECT,
Conversation::PARCEL_SALMON,
],
];
$conversation = DBA::selectFirst('conversation', ['source', 'protocol'], $condition);
if (DBA::isResult($conversation)) {
$stored = true;
$xml = $conversation['source'];
if (self::process($xml, $importer, $contact, $hub, $stored, false, Conversation::PULL)) {
Logger::info('Got valid cached XML for URI '.$related_uri);
return;
}
if ($conversation['protocol'] == Conversation::PARCEL_SALMON) {
Logger::info('Delete invalid cached XML for URI '.$related_uri);
DBA::delete('conversation', ['item-uri' => $related_uri]);
}
}
$stored = false;
$curlResult = DI::httpClient()->get($related, HttpClientAccept::ATOM_XML);
@@ -1013,17 +796,6 @@ class OStatus
}
}
// Finally we take the data that we fetched from "ostatus:conversation"
if ($xml == '') {
$condition = ['item-uri' => $related_uri, 'protocol' => Conversation::PARCEL_SPLIT_CONVERSATION];
$conversation = DBA::selectFirst('conversation', ['source'], $condition);
if (DBA::isResult($conversation)) {
$stored = true;
Logger::info('Got cached XML from conversation for URI ' . $related_uri);
$xml = $conversation['source'];
}
}
if ($xml != '') {
self::process($xml, $importer, $contact, $hub, $stored, false, Conversation::PULL);
} else {
@@ -1130,10 +902,7 @@ class OStatus
case 'ostatus:conversation':
$link_data['conversation'] = $attribute['href'];
$item['conversation-href'] = $link_data['conversation'];
if (!isset($item['conversation-uri'])) {
$item['conversation-uri'] = $item['conversation-href'];
}
$item['conversation'] = $link_data['conversation'];
break;
case 'enclosure':
@@ -1904,19 +1673,7 @@ class OStatus
}
if (intval($item['parent']) > 0) {
$conversation_href = $conversation_uri = str_replace('/objects/', '/context/', $item['thr-parent']);
if (isset($parent_item)) {
$conversation = DBA::selectFirst('conversation', ['conversation-uri', 'conversation-href'], ['item-uri' => $parent_item]);
if (DBA::isResult($conversation)) {
if ($conversation['conversation-uri'] != '') {
$conversation_uri = $conversation['conversation-uri'];
}
if ($conversation['conversation-href'] != '') {
$conversation_href = $conversation['conversation-href'];
}
}
}
$conversation_href = $conversation_uri = $item['conversation'];
XML::addElement($doc, $entry, 'link', '', ['rel' => 'ostatus:conversation', 'href' => $conversation_href]);