From df019eb5bdfb39a7c7c660505e394ed0a73fe4c0 Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Thu, 10 May 2018 10:13:48 +0000
Subject: [PATCH] Issue 4772: Display posts with uid=0 as well

---
 include/api.php | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/include/api.php b/include/api.php
index 72997dd3a..628b86c64 100644
--- a/include/api.php
+++ b/include/api.php
@@ -2002,6 +2002,19 @@ function api_statuses_show($type)
 		$sql_extra .= " AND `item`.`id` = %d";
 	}
 
+	// try to fetch the item for the local user - or the public item, if there is no local one
+	$uri_item = dba::selectFirst('item', ['uri'], ['id' => $id]);
+	if (!DBM::is_result($uri_item)) {
+		throw new BadRequestException("There is no status with this id.");
+	}
+
+	$item = dba::selectFirst('item', ['id'], ['uri' => $uri_item['uri'], 'uid' => [0, api_user()]], ['order' => ['uid' => true]]);
+	if (!DBM::is_result($item)) {
+		throw new BadRequestException("There is no status with this id.");
+	}
+
+	$id = $item['id'];
+
 	$r = q(
 		"SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`,
 		`contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
@@ -2011,7 +2024,7 @@ function api_statuses_show($type)
 		INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` = `item`.`uid`
 			AND (NOT `contact`.`blocked` OR `contact`.`pending`)
 		WHERE `item`.`visible` AND NOT `item`.`moderated` AND NOT `item`.`deleted`
-		AND `item`.`uid` = %d AND `item`.`verb` = '%s'
+		AND `item`.`uid` IN (0, %d) AND `item`.`verb` = '%s'
 		$sql_extra",
 		intval(api_user()),
 		dbesc(ACTIVITY_POST),
@@ -2075,22 +2088,25 @@ function api_conversation_show($type)
 
 	logger('API: api_conversation_show: '.$id);
 
-	$r = q("SELECT `parent` FROM `item` WHERE `id` = %d", intval($id));
-	if (DBM::is_result($r)) {
-		$id = $r[0]["parent"];
+	// try to fetch the item for the local user - or the public item, if there is no local one
+	$item = dba::selectFirst('item', ['parent-uri'], ['id' => $id]);
+	if (!DBM::is_result($item)) {
+		throw new BadRequestException("There is no status with this id.");
 	}
 
+	$parent = dba::selectFirst('item', ['id'], ['uri' => $item['parent-uri'], 'uid' => [0, api_user()]], ['order' => ['uid' => true]]);
+	if (!DBM::is_result($parent)) {
+		throw new BadRequestException("There is no status with this id.");
+	}
+
+	$id = $parent['id'];
+
 	$sql_extra = '';
 
 	if ($max_id > 0) {
 		$sql_extra = ' AND `item`.`id` <= ' . intval($max_id);
 	}
 
-	// Not sure why this query was so complicated. We should keep it here for a while,
-	// just to make sure that we really don't need it.
-	//	FROM `item` INNER JOIN (SELECT `uri`,`parent` FROM `item` WHERE `id` = %d) AS `temp1`
-	//	ON (`item`.`thr-parent` = `temp1`.`uri` AND `item`.`parent` = `temp1`.`parent`)
-
 	$r = q(
 		"SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`,
 		`contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
@@ -2101,7 +2117,7 @@ function api_conversation_show($type)
 			AND (NOT `contact`.`blocked` OR `contact`.`pending`)
 		WHERE `item`.`parent` = %d AND `item`.`visible`
 		AND NOT `item`.`moderated` AND NOT `item`.`deleted`
-		AND `item`.`uid` = %d AND `item`.`verb` = '%s'
+		AND `item`.`uid` IN (0, %d) AND `item`.`verb` = '%s'
 		AND `item`.`id`>%d $sql_extra
 		ORDER BY `item`.`id` DESC LIMIT %d ,%d",
 		intval($id),