Store incoming posts in a queue

This commit is contained in:
Michael 2022-07-18 03:31:00 +00:00
parent 46fdd9893c
commit 2f462ffa16
3 changed files with 74 additions and 12 deletions

View file

@ -28,6 +28,7 @@ use Friendica\Content\Text\Markdown;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\APContact;
@ -36,6 +37,7 @@ use Friendica\Model\Post;
use Friendica\Model\User;
use Friendica\Protocol\Activity;
use Friendica\Protocol\ActivityPub;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\HTTPSignature;
use Friendica\Util\JsonLD;
use Friendica\Util\LDSignature;
@ -96,7 +98,21 @@ class Receiver
$ldactivity = JsonLD::compact($activity);
$http_signer = HTTPSignature::getSigner($body, $header);
if ($http_signer === false) {
Logger::warning('Invalid HTTP signature, message will be discarded.');
return;
} elseif (empty($http_signer)) {
Logger::info('Signer is a tombstone. The message will be discarded, the signer account is deleted.');
return;
} else {
Logger::info('Valid HTTP signature', ['signer' => $http_signer]);
}
self::enqueuePost($ldactivity, $http_signer, $uid);
$actor = JsonLD::fetchElement($ldactivity, 'as:actor', '@id') ?? '';
$apcontact = APContact::getByURL($actor);
if (empty($apcontact)) {
@ -109,17 +125,6 @@ class Receiver
APContact::unmarkForArchival($apcontact);
}
$http_signer = HTTPSignature::getSigner($body, $header);
if ($http_signer === false) {
Logger::warning('Invalid HTTP signature, message will be discarded.');
return;
} elseif (empty($http_signer)) {
Logger::info('Signer is a tombstone. The message will be discarded, the signer account is deleted.');
return;
} else {
Logger::info('Valid HTTP signature', ['signer' => $http_signer]);
}
$signer = [$http_signer];
Logger::info('Message for user ' . $uid . ' is from actor ' . $actor);
@ -157,6 +162,30 @@ class Receiver
$fetchQueue->process();
}
private static function enqueuePost(array $ldactivity = [], string $signer, int $uid)
{
if (empty($ldactivity['as:object'])) {
return;
}
$url = JsonLD::fetchElement($ldactivity, 'as:object', '@id');
$fields = [
'url' => $url,
'in-reply-to-url' => JsonLD::fetchElement($ldactivity['as:object'], 'as:inReplyTo', '@id'),
'signer' => $signer,
'type' => JsonLD::fetchElement($ldactivity, '@type'),
'object-type' => JsonLD::fetchElement($ldactivity['as:object'], '@type'),
'activity' => json_encode($ldactivity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
'received' => DateTimeFormat::utcNow(),
];
DBA::insert('inbox-queue', $fields, Database::INSERT_IGNORE);
$queue = DBA::selectFirst('inbox-queue', ['id'], ['url' => $url]);
if (!empty($queue['id'])) {
DBA::insert('inbox-queue-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
}
}
/**
* Process incoming posts from relays
*