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;
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;
$ldactivity = JsonLD::compact($activity);
$actor = JsonLD::fetchElement($ldactivity, 'as:actor', '@id') ?? '';
+
$apcontact = APContact::getByURL($actor);
if (empty($apcontact)) {
Logger::notice('Unable to retrieve AP contact for actor - message is discarded', ['actor' => $actor]);
return;
- } elseif ($apcontact['type'] == 'Application' && $apcontact['nick'] == 'relay') {
+ } elseif (APContact::isRelay($apcontact)) {
self::processRelayPost($ldactivity, $actor);
return;
} else {
}
$fetchQueue = new FetchQueue();
- self::processActivity($fetchQueue, $ldactivity, $body, $uid, $trust_source, true, $signer);
+ self::processActivity($fetchQueue, $ldactivity, $body, $uid, $trust_source, true, $signer, $http_signer);
$fetchQueue->process();
}
+ private static function enqueuePost(array $ldactivity = [], string $type, int $uid, string $http_signer): array
+ {
+ $fields = [
+ 'activity-id' => $ldactivity['id'],
+ 'object-id' => $ldactivity['object_id'],
+ 'type' => $type,
+ 'object-type' => $ldactivity['object_type'],
+ 'activity' => json_encode($ldactivity, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT),
+ 'received' => DateTimeFormat::utcNow(),
+ ];
+
+ if (!empty($ldactivity['object_object_type'])) {
+ $fields['object-object-type'] = $ldactivity['object_object_type'];
+ }
+
+ if (!empty($http_signer)) {
+ $fields['signer'] = $http_signer;
+ }
+
+ DBA::insert('inbox-entry', $fields, Database::INSERT_IGNORE);
+
+ $queue = DBA::selectFirst('inbox-entry', ['id'], ['activity-id' => $ldactivity['id']]);
+ if (!empty($queue['id'])) {
+ $ldactivity['entry-id'] = $queue['id'];
+ DBA::insert('inbox-entry-receiver', ['queue-id' => $queue['id'], 'uid' => $uid], Database::INSERT_IGNORE);
+ }
+ return $ldactivity;
+ }
+
+ public static function removeFromQueue(array $activity = [])
+ {
+ if (empty($activity['entry-id'])) {
+ return;
+ }
+ DBA::delete('inbox-entry', ['id' => $activity['entry-id']]);
+ }
+
/**
* Process incoming posts from relays
*
{
$type = JsonLD::fetchElement($activity, '@type');
if (!$type) {
- Logger::info('Empty type', ['activity' => $activity]);
+ Logger::info('Empty type', ['activity' => $activity, 'actor' => $actor]);
return;
}
if ($type != 'as:Announce') {
- Logger::info('Not an announcement', ['activity' => $activity]);
+ Logger::info('Not an announcement', ['activity' => $activity, 'actor' => $actor]);
return;
}
$object_id = JsonLD::fetchElement($activity, 'as:object', '@id');
if (empty($object_id)) {
- Logger::info('No object id found', ['activity' => $activity]);
+ Logger::info('No object id found', ['activity' => $activity, 'actor' => $actor]);
return;
}
return;
}
- Logger::info('Got relayed message id', ['id' => $object_id]);
+ Logger::info('Got relayed message id', ['id' => $object_id, 'actor' => $actor]);
$item_id = Item::searchByLink($object_id);
if ($item_id) {
- Logger::info('Relayed message already exists', ['id' => $object_id, 'item' => $item_id]);
+ Logger::info('Relayed message already exists', ['id' => $object_id, 'item' => $item_id, 'actor' => $actor]);
return;
}
$id = Processor::fetchMissingActivity($fetchQueue, $object_id, [], $actor, self::COMPLETION_RELAY);
if (empty($id)) {
- Logger::notice('Relayed message had not been fetched', ['id' => $object_id]);
+ Logger::notice('Relayed message had not been fetched', ['id' => $object_id, 'actor' => $actor]);
return;
}
$item_id = Item::searchByLink($object_id);
if ($item_id) {
- Logger::info('Relayed message had been fetched and stored', ['id' => $object_id, 'item' => $item_id]);
+ Logger::info('Relayed message had been fetched and stored', ['id' => $object_id, 'item' => $item_id, 'actor' => $actor]);
} else {
- Logger::notice('Relayed message had not been stored', ['id' => $object_id]);
+ Logger::notice('Relayed message had not been stored', ['id' => $object_id, 'actor' => $actor]);
}
}
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/
- public static function processActivity(FetchQueue $fetchQueue, array $activity, string $body = '', int $uid = null, bool $trust_source = false, bool $push = false, array $signer = [])
+ public static function processActivity(FetchQueue $fetchQueue, array $activity, string $body = '', int $uid = null, bool $trust_source = false, bool $push = false, array $signer = [], string $http_signer = '')
{
$type = JsonLD::fetchElement($activity, '@type');
if (!$type) {
$object_data['from-relay'] = $activity['from-relay'];
}
+ $object_data = self::enqueuePost($object_data, $type, $uid, $http_signer);
+
if (in_array('as:Question', [$object_data['object_type'] ?? '', $object_data['object_object_type'] ?? ''])) {
self::storeUnhandledActivity(false, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer);
}
ActivityPub\Processor::postItem($object_data, $item);
} elseif (in_array($object_data['object_type'], ['pt:CacheFile'])) {
// Unhandled Peertube activity
+ self::removeFromQueue($object_data);
} else {
self::storeUnhandledActivity(true, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer);
}
ActivityPub\Processor::updatePerson($object_data);
} elseif (in_array($object_data['object_type'], ['pt:CacheFile'])) {
// Unhandled Peertube activity
+ self::removeFromQueue($object_data);
} else {
self::storeUnhandledActivity(true, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer);
}
} elseif (in_array($object_data['object_type'], array_merge(self::ACTIVITY_TYPES, ['as:Announce', 'as:Create', ''])) &&
empty($object_data['object_object_type'])) {
// We cannot detect the target object. So we can ignore it.
+ self::removeFromQueue($object_data);
} elseif (in_array($object_data['object_type'], ['as:Create']) &&
in_array($object_data['object_object_type'], ['pt:CacheFile'])) {
// Unhandled Peertube activity
+ self::removeFromQueue($object_data);
} else {
self::storeUnhandledActivity(true, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer);
}
ActivityPub\Processor::createActivity($fetchQueue, $object_data, Activity::VIEW);
} elseif ($object_data['object_type'] == '') {
// The object type couldn't be determined. Most likely we don't have it here. We ignore this activity.
+ self::removeFromQueue($object_data);
} else {
self::storeUnhandledActivity(true, $type, $object_data, $activity, $body, $uid, $trust_source, $push, $signer);
}
private static function storeUnhandledActivity(bool $unknown, string $type, array $object_data, array $activity, string $body = '', int $uid = null, bool $trust_source = false, bool $push = false, array $signer = [])
{
if (!DI::config()->get('debug', 'ap_log_unknown')) {
+ self::removeFromQueue($activity);
return;
}