From 84be62982af156aba8d5e877f74836a08c2f821f Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Mon, 29 Oct 2018 21:15:37 +0000
Subject: [PATCH] Diaspora signature transport via AP/DFRN should be repaired
 now

---
 mod/item.php              |  8 ++++---
 src/Model/Item.php        |  2 +-
 src/Protocol/Diaspora.php | 48 +++++++++++++++++----------------------
 3 files changed, 27 insertions(+), 31 deletions(-)

diff --git a/mod/item.php b/mod/item.php
index fa4573b1b6..34174a36d5 100644
--- a/mod/item.php
+++ b/mod/item.php
@@ -726,9 +726,11 @@ function item_post(App $a) {
 	unset($datarray['self']);
 	unset($datarray['api_source']);
 
-	$signed = Diaspora::createCommentSignature($author, $datarray);
-	if (!empty($signed)) {
-		$datarray['diaspora_signed_text'] = json_encode($signed);
+	if ($origin) {
+		$signed = Diaspora::createCommentSignature($uid, $datarray);
+		if (!empty($signed)) {
+			$datarray['diaspora_signed_text'] = json_encode($signed);
+		}
 	}
 
 	$post_id = Item::insert($datarray);
diff --git a/src/Model/Item.php b/src/Model/Item.php
index c4985aa835..d874f9aed1 100644
--- a/src/Model/Item.php
+++ b/src/Model/Item.php
@@ -3111,7 +3111,7 @@ class Item extends BaseObject
 			'unseen'        => 1,
 		];
 
-		$signed = Diaspora::createLikeSignature($item_contact, $new_item);
+		$signed = Diaspora::createLikeSignature($uid, $new_item);
 		if (!empty($signed)) {
 			$new_item['diaspora_signed_text'] = json_encode($signed);
 		}
diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php
index 9c48c1bf24..e532b565df 100644
--- a/src/Protocol/Diaspora.php
+++ b/src/Protocol/Diaspora.php
@@ -4115,36 +4115,28 @@ class Diaspora
 	/**
 	 * @brief Creates the signature for likes that are created on our system
 	 *
-	 * @param array $contact The contact array of the "like"
-	 * @param array $item Item array
+	 * @param integer $uid  The user of that comment
+	 * @param array   $item Item array
 	 *
 	 * @return array Signed content
 	 */
-	public static function createLikeSignature(array $contact, array $item)
+	public static function createLikeSignature($uid, array $item)
 	{
-		// Is the contact the owner? Then fetch the private key
-		if (!$contact['self'] || ($contact['uid'] == 0)) {
-			logger("No owner post, so not storing signature", LOGGER_DEBUG);
-			return false;
-		}
-
-		$user = DBA::selectFirst('user', ['prvkey'], ['uid' => $contact["uid"]]);
-		if (!DBA::isResult($user)) {
+		$owner = User::getOwnerDataById($uid);
+		if (empty($owner)) {
 			return false;
 		}
 
-		$contact["uprvkey"] = $user['prvkey'];
-
 		if (!in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
 			return false;
 		}
 
-		$message = self::constructLike($item, $contact);
+		$message = self::constructLike($item, $owner);
 		if ($message === false) {
 			return false;
 		}
 
-		$message["author_signature"] = self::signature($contact, $message);
+		$message["author_signature"] = self::signature($owner, $message);
 
 		return $message;
 	}
@@ -4152,32 +4144,34 @@ class Diaspora
 	/**
 	 * @brief Creates the signature for Comments that are created on our system
 	 *
-	 * @param array $contact The contact array of the comment
-	 * @param array $item Item array
+	 * @param integer $uid  The user of that comment
+	 * @param array   $item Item array
 	 *
 	 * @return array Signed content
 	 */
-	public static function createCommentSignature(array $contact, array $item)
+	public static function createCommentSignature($uid, array $item)
 	{
-		// Is the contact the owner? Then fetch the private key
-		if (!$contact['self'] || ($contact['uid'] == 0)) {
-			logger("No owner post, so not storing signature", LOGGER_DEBUG);
+		$owner = User::getOwnerDataById($uid);
+		if (empty($owner)) {
 			return false;
 		}
 
-		$user = DBA::selectFirst('user', ['prvkey'], ['uid' => $contact["uid"]]);
-		if (!DBA::isResult($user)) {
-			return false;
+		// This is a workaround for the behaviour of the "insert" function, see mod/item.php
+		$item['thr-parent'] = $item['parent-uri'];
+
+		$parent = Item::selectFirst(['parent-uri'], ['uri' => $item['parent-uri']]);
+		if (!DBA::isResult($parent)) {
+			return;
 		}
 
-		$contact["uprvkey"] = $user['prvkey'];
+		$item['parent-uri'] = $parent['parent-uri'];
 
-		$message = self::constructComment($item, $contact);
+		$message = self::constructComment($item, $owner);
 		if ($message === false) {
 			return false;
 		}
 
-		$message["author_signature"] = self::signature($contact, $message);
+		$message["author_signature"] = self::signature($owner, $message);
 
 		return $message;
 	}
-- 
2.39.5