From d9d12681940473ba87c77f61d5e9877925a9bda9 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Thu, 24 Sep 2020 10:26:28 +0000
Subject: [PATCH] Issue 9264: Ensure that "pt_follower" is only set when its a
 real follower

---
 src/Model/Item.php                    |  3 ++-
 src/Protocol/ActivityPub/Receiver.php | 17 ++++++-----------
 2 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/src/Model/Item.php b/src/Model/Item.php
index d03e9ec35..62f5c7148 100644
--- a/src/Model/Item.php
+++ b/src/Model/Item.php
@@ -1712,7 +1712,8 @@ class Item
 		$item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, null, $default);
 
 		$actor = ($item['gravity'] == GRAVITY_PARENT) ? $item['owner-id'] : $item['author-id'];
-		if (!$item['origin'] && in_array($item['post-type'], [self::PT_ARTICLE, self::PT_COMMENT, self::PT_RELAY, self::PT_GLOBAL]) && Contact::isSharing($actor, $item['uid'])) {
+		if (in_array($item['post-type'], [self::PT_ARTICLE, self::PT_COMMENT, self::PT_RELAY, self::PT_GLOBAL])
+			&& !$item['origin'] && ($item['uid'] != 0) && Contact::isSharing($actor, $item['uid'])) {
 			$item['post-type'] = self::PT_FOLLOWER;
 		}
 
diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php
index aaf5d9b0f..00e6d5c31 100644
--- a/src/Protocol/ActivityPub/Receiver.php
+++ b/src/Protocol/ActivityPub/Receiver.php
@@ -734,20 +734,20 @@ class Receiver
 		$basecondition = ['rel' => [Contact::SHARING, Contact::FRIEND, Contact::FOLLOWER],
 			'network' => Protocol::FEDERATED, 'archive' => false, 'pending' => false];
 
-		$condition = DBA::mergeConditions($basecondition, ['nurl' => Strings::normaliseLink($actor)]);
+		$condition = DBA::mergeConditions($basecondition, ["`nurl` = ? AND `uid` != ?", Strings::normaliseLink($actor), 0]);
 		$contacts = DBA::select('contact', ['uid', 'rel'], $condition);
 		while ($contact = DBA::fetch($contacts)) {
-			if (empty($receivers['uid:' . $contact['uid']]) && self::isValidReceiverForActor($contact, $actor, $tags)) {
+			if (empty($receivers['uid:' . $contact['uid']]) && self::isValidReceiverForActor($contact, $tags)) {
 				$receivers['uid:' . $contact['uid']] = ['uid' => $contact['uid'], 'type' => self::TARGET_FOLLOWER];
 			}
 		}
 		DBA::close($contacts);
 
 		// The queries are split because of performance issues
-		$condition = DBA::mergeConditions($basecondition, ["`alias` IN (?, ?)", Strings::normaliseLink($actor), $actor]);
+		$condition = DBA::mergeConditions($basecondition, ["`alias` IN (?, ?) AND `uid` != ?", Strings::normaliseLink($actor), $actor, 0]);
 		$contacts = DBA::select('contact', ['uid', 'rel'], $condition);
 		while ($contact = DBA::fetch($contacts)) {
-			if (empty($receivers['uid:' . $contact['uid']]) && self::isValidReceiverForActor($contact, $actor, $tags)) {
+			if (empty($receivers['uid:' . $contact['uid']]) && self::isValidReceiverForActor($contact, $tags)) {
 				$receivers['uid:' . $contact['uid']] = ['uid' => $contact['uid'], 'type' => self::TARGET_FOLLOWER];
 			}
 		}
@@ -765,13 +765,8 @@ class Receiver
 	 * @return bool with receivers (user id)
 	 * @throws \Exception
 	 */
-	private static function isValidReceiverForActor($contact, $actor, $tags)
+	private static function isValidReceiverForActor($contact, $tags)
 	{
-		// Public contacts are no valid receiver
-		if ($contact['uid'] == 0) {
-			return false;
-		}
-
 		// Are we following the contact? Then this is a valid receiver
 		if (in_array($contact['rel'], [Contact::SHARING, Contact::FRIEND])) {
 			return true;
@@ -789,7 +784,7 @@ class Receiver
 				continue;
 			}
 
-			if ($tag['href'] == $owner['url']) {
+			if (Strings::compareLink($tag['href'], $owner['url'])) {
 				return true;
 			}
 		}