From d990026fb83eb37a9a73211ed7693d6d563fcae4 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Sun, 9 Mar 2025 07:52:58 +0000
Subject: [PATCH] Issue 14433: Display reshared content in feeds

---
 src/Protocol/Feed.php | 44 ++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 17 deletions(-)

diff --git a/src/Protocol/Feed.php b/src/Protocol/Feed.php
index 2f9be305f1..6a2f5d42dd 100644
--- a/src/Protocol/Feed.php
+++ b/src/Protocol/Feed.php
@@ -1040,34 +1040,44 @@ class Feed
 		$authorid   = Contact::getIdForURL($owner['url']);
 
 		$condition = [
-			"`uid` = ? AND `received` > ? AND NOT `deleted` AND `gravity` IN (?, ?)
-			AND `private` != ? AND `visible` AND `wall` AND `parent-network` IN (?, ?, ?)",
+			"`uid` = ? AND `received` > ? AND NOT `deleted`
+			AND ((`gravity` IN (?, ?) AND `wall`) OR (`gravity` = ? AND `verb` = ?))
+			AND `origin` AND `private` != ? AND `visible` AND `parent-network` IN (?, ?, ?)
+			AND `author-id` = ?",
 			$owner['uid'], $check_date, Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT,
-			Item::PRIVATE, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA
+			Item::GRAVITY_ACTIVITY, Activity::ANNOUNCE,
+			Item::PRIVATE, Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA,
+			$authorid
 		];
 
 		if ($filter === 'comments') {
-			$condition[0] .= " AND `gravity` = ? ";
-			$condition[] = Item::GRAVITY_COMMENT;
-		}
-
-		if ($owner['account-type'] != User::ACCOUNT_TYPE_COMMUNITY) {
-			$condition[0] .= " AND `contact-id` = ? AND `author-id` = ?";
-			$condition[] = $owner['id'];
-			$condition[] = $authorid;
+			$condition = DBA::mergeConditions($condition, ['gravity' => Item::GRAVITY_COMMENT]);
+		} elseif ($filter === 'posts') {
+			$condition = DBA::mergeConditions($condition, ['gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_ACTIVITY]]);
 		}
 
 		$params = ['order' => ['received' => true], 'limit' => $max_items];
 
-		if ($filter === 'posts') {
-			$ret = Post::selectOriginThread(Item::DELIVER_FIELDLIST, $condition, $params);
-		} else {
-			$ret = Post::selectOrigin(Item::DELIVER_FIELDLIST, $condition, $params);
-		}
+		$ret = Post::selectOrigin(Item::DELIVER_FIELDLIST, $condition, $params);
 
 		$items = Post::toArray($ret);
 
-		$doc               = new DOMDocument('1.0', 'utf-8');
+		$reshares = [];
+		foreach ($items as $index => $item) {
+			if ($item['verb'] == Activity::ANNOUNCE) {
+				$reshares[$item['thr-parent-id']] = $index;
+			}
+		}
+
+		if (!empty($reshares)) {
+			$posts = Post::selectToArray(Item::DELIVER_FIELDLIST, ['uri-id' => array_keys($reshares), 'uid' => $owner['uid']]);
+			foreach ($posts as $post) {
+				$items[$reshares[$post['uri-id']]] = $post;
+			}
+		}
+
+		$doc = new DOMDocument('1.0', 'utf-8');
+
 		$doc->formatOutput = true;
 
 		$root = self::addHeader($doc, $owner, $filter);