From f264923cad9b867771c793cc10af36689c1c657e Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 11 Mar 2022 14:00:05 +0000 Subject: [PATCH] Issue 11309: Check if a post is wanted --- src/Protocol/ActivityPub/Processor.php | 52 ++++++++++++++++++++++++++ src/Protocol/DFRN.php | 5 +-- src/Protocol/Diaspora.php | 5 +-- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 79d24d1602..2e2d18b391 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -572,6 +572,53 @@ class Processor return $host_hash . '-'. hash('fnv164', $path) . '-'. hash('joaat', $path); } + /** + * Checks if an incoming message is wanted + * + * @param array $activity + * @param array $item + * @return boolean Is the message wanted? + */ + private static function isSolicitedMessage(array $activity, array $item) + { + // The checks are split to improve the support when searching why a message was accepted. + if (count($activity['receiver']) != 1) { + // The message has more than one receiver, so it is wanted. + Logger::debug('Message has got several receivers - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]); + return true; + } + + if ($item['private'] == Item::PRIVATE) { + // We only look at public posts here. Private posts are expected to be intentionally posted to the single receiver. + Logger::debug('Message is private - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]); + return true; + } + + if (!empty($activity['from-relay'])) { + // We check relay posts at another place. When it arrived here, the message is already checked. + Logger::debug('Message is a relay post that is already checked - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]); + return true; + } + + if (!empty($activity['thread-completion'])) { + // The thread completion mode means that the post is fetched intentionally. + // This can have several causes, in doubt we keep the message. + // This can possibly be improved in the future. + Logger::debug('Message is in completion mode - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]); + return true; + } + + if ($item['gravity'] != GRAVITY_PARENT) { + // We cannot reliably check at this point if a comment or activity belongs to an accepted post or needs to be fetched + // This can possibly be improved in the future. + Logger::debug('Message is no parent - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]); + return true; + } + + $tags = array_column(Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]), 'name'); + return Relay::isSolicitedPost($tags, $item['body'], $item['author-id'], $item['uri'], Protocol::ACTIVITYPUB); + } + /** * Creates an item post * @@ -589,6 +636,11 @@ class Processor $stored = false; ksort($activity['receiver']); + if (!self::isSolicitedMessage($activity, $item)) { + DBA::delete('item-uri', ['id' => $item['uri-id']]); + return; + } + foreach ($activity['receiver'] as $receiver) { if ($receiver == -1) { continue; diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 2be55b8a55..a20d54fb64 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -1775,12 +1775,11 @@ class DFRN { if (DBA::exists('contact', ["`nurl` = ? AND `uid` != ? AND `rel` IN (?, ?)", Strings::normaliseLink($item["author-link"]), 0, Contact::FRIEND, Contact::SHARING])) { - Logger::info('Author has got followers - accepted', ['uri' => $item['uri'], 'author' => $item["author-link"]]); + Logger::debug('Author has got followers - accepted', ['uri' => $item['uri'], 'author' => $item["author-link"]]); return true; } - $taglist = Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]); - $tags = array_column($taglist, 'name'); + $tags = array_column(Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]), 'name'); return Relay::isSolicitedPost($tags, $item['body'], $item['author-id'], $item['uri'], Protocol::DFRN); } diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index ad8e2bbef8..331a3bd9fe 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -2620,12 +2620,11 @@ class Diaspora $contact = Contact::getByURL($author); if (DBA::exists('contact', ["`nurl` = ? AND `uid` != ? AND `rel` IN (?, ?)", $contact['nurl'], 0, Contact::FRIEND, Contact::SHARING])) { - Logger::info('Author has got followers - accepted', ['url' => $url, 'author' => $author]); + Logger::debug('Author has got followers - accepted', ['url' => $url, 'author' => $author]); return true; } - $taglist = Tag::getByURIId($uriid, [Tag::HASHTAG]); - $tags = array_column($taglist, 'name'); + $tags = array_column(Tag::getByURIId($uriid, [Tag::HASHTAG]), 'name'); return Relay::isSolicitedPost($tags, $body, $contact['id'], $url, Protocol::DIASPORA); }