Conversation/context is now stored

This commit is contained in:
Michael 2022-07-23 06:52:43 +00:00
parent 54cebf5a88
commit 7eb410bed7
11 changed files with 161 additions and 84 deletions

View file

@ -280,15 +280,26 @@ class Processor
$item['object-type'] = Activity\ObjectType::COMMENT;
}
if (empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Post::exists(['uri' => $activity['reply-to-id']])) {
if (Queue::hasWorker($activity)) {
Logger::notice('There is already a worker task to dfetch the post.', ['parent' => $activity['reply-to-id']]);
return [];
}
if (!empty($activity['context'])) {
$item['conversation'] = $activity['context'];
} elseif(!empty($activity['conversation'])) {
$item['conversation'] = $activity['conversation'];
}
if (!empty($item['conversation'])) {
$conversation = Post::selectFirstThread(['uri'], ['conversation' => $item['conversation']]);
if (!empty($conversation)) {
Logger::debug('Got conversation', ['conversation' => $item['conversation'], 'parent' => $conversation]);
$item['parent-uri'] = $conversation['uri'];
}
} else {
$conversation = [];
}
if (empty($activity['directmessage']) && ($activity['id'] != $activity['reply-to-id']) && !Post::exists(['uri' => $activity['reply-to-id']])) {
$recursion_depth = $activity['recursion-depth'] ?? 0;
Logger::notice('Parent not found. Try to refetch it.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
if ($recursion_depth < 10) {
if ($recursion_depth < 10000) {
$result = self::fetchMissingActivity($activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO);
$fetch_by_worker = empty($result);
} else {
@ -296,19 +307,32 @@ class Processor
$fetch_by_worker = true;
}
if ($fetch_by_worker && Queue::hasWorker($activity)) {
Logger::notice('There is already a worker task to fetch the post.', ['id' => $activity['id'], 'parent' => $activity['reply-to-id']]);
$fetch_by_worker = false;
if (!empty($conversation)) {
return [];
}
}
if ($fetch_by_worker) {
Logger::notice('Fetching is done by worker.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]);
$activity['recursion-depth'] = 0;
$wid = Worker::add(PRIORITY_HIGH, 'FetchMissingActivity', $activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO);
Queue::setWorkerId($activity, $wid);
return [];
if (!empty($conversation)) {
return [];
}
} elseif (!empty($result)) {
if (($item['thr-parent'] != $result) && Post::exists(['uri' => $result])) {
$item['thr-parent'] = $result;
}
}
}
$item['diaspora_signed_text'] = $activity['diaspora:comment'] ?? '';
/// @todo What to do with $activity['context']?
if (empty($activity['directmessage']) && ($item['gravity'] != GRAVITY_PARENT) && !Post::exists(['uri' => $item['thr-parent']])) {
if (empty($conversation) && empty($activity['directmessage']) && ($item['gravity'] != GRAVITY_PARENT) && !Post::exists(['uri' => $item['thr-parent']])) {
Logger::info('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]);
return [];
}
@ -484,6 +508,7 @@ class Processor
*/
public static function createActivity(array $activity, string $verb)
{
$activity['reply-to-id'] = $activity['object_id'];
$item = self::createItem($activity);
if (empty($item)) {
return;
@ -663,10 +688,11 @@ class Processor
$item['raw-body'] = $content;
$item['body'] = Item::improveSharedDataInBody($item);
} else {
if (empty($activity['directmessage']) && ($item['thr-parent'] != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
$parent = Post::selectFirst(['id', 'uri-id', 'private', 'author-link', 'alias'], ['uri' => $item['thr-parent']]);
$parent_uri = $item['parent-uri'] ?? $item['thr-parent'];
if (empty($activity['directmessage']) && ($parent_uri != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) {
$parent = Post::selectFirst(['id', 'uri-id', 'private', 'author-link', 'alias'], ['uri' => $parent_uri]);
if (!DBA::isResult($parent)) {
Logger::warning('Unknown parent item.', ['uri' => $item['thr-parent']]);
Logger::warning('Unknown parent item.', ['uri' => $parent_uri]);
return false;
}
if (($parent['private'] == Item::PRIVATE) && ($parent['private'] != Item::PRIVATE)) {
@ -1327,8 +1353,8 @@ class Processor
if (empty($contact)) {
Contact::update(['hub-verify' => $activity['id'], 'protocol' => Protocol::ACTIVITYPUB], ['id' => $cid]);
}
Logger::notice('Follow user ' . $uid . ' from contact ' . $cid . ' with id ' . $activity['id']);
Queue::remove($activity);
}
/**
@ -1426,6 +1452,7 @@ class Processor
Contact\User::setIsBlocked($cid, $uid, true);
Logger::info('Contact blocked user', ['contact' => $cid, 'user' => $uid]);
Queue::remove($activity);
}
/**
@ -1450,6 +1477,7 @@ class Processor
Contact\User::setIsBlocked($cid, $uid, false);
Logger::info('Contact unblocked user', ['contact' => $cid, 'user' => $uid]);
Queue::remove($activity);
}
/**

View file

@ -140,6 +140,14 @@ class Queue
$activity['entry-id'] = $entry['id'];
$activity['worker-id'] = $entry['wid'];
$receivers = DBA::select('inbox-entry-receiver', ['uid'], ['queue-id' => $entry['id']]);
while ($receiver = DBA::fetch($receivers)) {
if (!in_array($receiver['uid'], $activity['receiver'])) {
$activity['receiver'][] = $receiver['uid'];
}
}
DBA::close($receivers);
if (!Receiver::routeActivities($activity, $type, $push)) {
self::remove($activity);
}

View file

@ -516,6 +516,10 @@ class Receiver
}
}
if (($type == 'as:Add') && is_array($activity['as:object']) && (count($activity['as:object']) == 1)) {
$trust_source = false;
}
// $trust_source is called by reference and is set to true if the content was retrieved successfully
$object_data = self::prepareObjectData($activity, $uid, $push, $trust_source);
if (empty($object_data)) {

View file

@ -1515,26 +1515,6 @@ class Transmitter
return $body;
}
/**
* Fetches the "context" value for a givem item array from the "conversation" table
*
* @param array $item Item array
* @return string with context url
* @throws \Exception
*/
private static function fetchContextURLForItem(array $item): string
{
$conversation = DBA::selectFirst('conversation', ['conversation-href', 'conversation-uri'], ['item-uri' => $item['parent-uri']]);
if (DBA::isResult($conversation) && !empty($conversation['conversation-href'])) {
$context_uri = $conversation['conversation-href'];
} elseif (DBA::isResult($conversation) && !empty($conversation['conversation-uri'])) {
$context_uri = $conversation['conversation-uri'];
} else {
$context_uri = $item['parent-uri'] . '#context';
}
return $context_uri;
}
/**
* Returns if the post contains sensitive content ("nsfw")
*
@ -1646,7 +1626,7 @@ class Transmitter
$data['url'] = $link ?? $item['plink'];
$data['attributedTo'] = $item['author-link'];
$data['sensitive'] = self::isSensitive($item['uri-id']);
$data['context'] = self::fetchContextURLForItem($item);
$data['conversation'] = $data['context'] = $item['conversation'];
if (!empty($item['title'])) {
$data['name'] = BBCode::toPlaintext($item['title'], false);