From f92338d79be7694ee39dc2e311eed8314e973776 Mon Sep 17 00:00:00 2001
From: Zach Prezkuta <fermion@gmx.com>
Date: Thu, 12 Jul 2012 18:53:51 -0600
Subject: [PATCH] make likes on comments work with Diaspora, some small bugs

---
 include/diaspora.php | 69 +++++++++++++++++++++++++++-----------------
 include/items.php    |  2 +-
 mod/like.php         |  5 ++--
 3 files changed, 47 insertions(+), 29 deletions(-)

diff --git a/include/diaspora.php b/include/diaspora.php
index d07d756e6e..af9a91f028 100755
--- a/include/diaspora.php
+++ b/include/diaspora.php
@@ -1236,6 +1236,7 @@ function diaspora_comment($importer,$xml,$msg) {
 
 	$datarray['uid'] = $importer['uid'];
 	$datarray['contact-id'] = $contact['id'];
+	$datarray['type'] = 'remote-comment';
 	$datarray['wall'] = $parent_item['wall'];
 	$datarray['gravity'] = GRAVITY_COMMENT;
 	$datarray['guid'] = $guid;
@@ -1673,8 +1674,8 @@ function diaspora_like($importer,$xml,$msg) {
 
 	// likes on comments not supported here and likes on photos not supported by Diaspora
 
-	if($target_type !== 'Post')
-		return;
+//	if($target_type !== 'Post')
+//		return;
 
 	$contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']);
 	if(! $contact) {
@@ -2253,22 +2254,30 @@ function diaspora_send_followup($item,$owner,$contact,$public_batch = false) {
 	$myaddr = $owner['nickname'] . '@' .  substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
 //	$theiraddr = $contact['addr'];
 
-	// The first item in the `item` table with the parent id is the parent. However, MySQL doesn't always
-	// return the items ordered by `item`.`id`, in which case the wrong item is chosen as the parent.
-	// The only item with `parent` and `id` as the parent id is the parent item.
-	$p = q("select guid from item where parent = %d and id = %d limit 1",
-		intval($item['parent']),
-		intval($item['parent'])
-	);
+	if($item['thr-parent']) {
+		$p = q("select guid, type, uri, `parent-uri` from item where uri = '%s' limit 1",
+		        dbesc($item['thr-parent'])
+		      );
+	}
+	else {
+		// The first item in the `item` table with the parent id is the parent. However, MySQL doesn't always
+		// return the items ordered by `item`.`id`, in which case the wrong item is chosen as the parent.
+		// The only item with `parent` and `id` as the parent id is the parent item.
+		$p = q("select guid, type, uri, `parent-uri` from item where parent = %d and id = %d limit 1",
+			intval($item['parent']),
+			intval($item['parent'])
+		);
+	}
 	if(count($p))
-		$parent_guid = $p[0]['guid'];
+		$parent = $p[0];
 	else
 		return;
 
 	if($item['verb'] === ACTIVITY_LIKE) {
 		$tpl = get_markup_template('diaspora_like.tpl');
 		$like = true;
-		$target_type = 'Post';
+		$target_type = ( $parent['uri'] === $parent['parent-uri']  ? 'Post' : 'Comment');
+//		$target_type = (strpos($parent['type'], 'comment') ? 'Comment' : 'Post');
 //		$positive = (($item['deleted']) ? 'false' : 'true');
 		$positive = 'true';
 
@@ -2285,15 +2294,15 @@ function diaspora_send_followup($item,$owner,$contact,$public_batch = false) {
 	// sign it
 
 	if($like)
-		$signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $myaddr;
+		$signed_text = $item['guid'] . ';' . $target_type . ';' . $parent['guid'] . ';' . $positive . ';' . $myaddr;
 	else
-		$signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
+		$signed_text = $item['guid'] . ';' . $parent['guid'] . ';' . $text . ';' . $myaddr;
 
 	$authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey'],'sha256'));
 
 	$msg = replace_macros($tpl,array(
 		'$guid' => xmlify($item['guid']),
-		'$parent_guid' => xmlify($parent_guid),
+		'$parent_guid' => xmlify($parent['guid']),
 		'$target_type' =>xmlify($target_type),
 		'$authorsig' => xmlify($authorsig),
 		'$body' => xmlify($text),
@@ -2321,15 +2330,22 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
 	$text = html_entity_decode(bb2diaspora($body));
 
 
-	// The first item in the `item` table with the parent id is the parent. However, MySQL doesn't always
-	// return the items ordered by `item`.`id`, in which case the wrong item is chosen as the parent.
-	// The only item with `parent` and `id` as the parent id is the parent item.
-	$p = q("select guid from item where parent = %d and id = %d limit 1",
-		intval($item['parent']),
-		intval($item['parent'])
-	);
+	if($item['thr-parent']) {
+		$p = q("select guid, type, uri, `parent-uri` from item where uri = '%s' limit 1",
+		        dbesc($item['thr-parent'])
+		      );
+	}
+	else {
+		// The first item in the `item` table with the parent id is the parent. However, MySQL doesn't always
+		// return the items ordered by `item`.`id`, in which case the wrong item is chosen as the parent.
+		// The only item with `parent` and `id` as the parent id is the parent item.
+		$p = q("select guid, type, uri, `parent-uri` from item where parent = %d and id = %d limit 1",
+			intval($item['parent']),
+			intval($item['parent'])
+		      );
+	}
 	if(count($p))
-		$parent_guid = $p[0]['guid'];
+		$parent = $p[0];
 	else
 		return;
 
@@ -2347,7 +2363,8 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
 	elseif($item['verb'] === ACTIVITY_LIKE) {
 		$like = true;
 
-		$target_type = 'Post';
+		$target_type = ( $parent['uri'] === $parent['parent-uri']  ? 'Post' : 'Comment');
+//		$target_type = (strpos($parent['type'], 'comment') ? 'Comment' : 'Post');
 //		$positive = (($item['deleted']) ? 'false' : 'true');
 		$positive = 'true';
 
@@ -2382,9 +2399,9 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
 	if($relay_retract)
 		$sender_signed_text = $item['guid'] . ';' . $target_type;
 	elseif($like)
-		$sender_signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $handle;
+		$sender_signed_text = $item['guid'] . ';' . $target_type . ';' . $parent['guid'] . ';' . $positive . ';' . $handle;
 	else
-		$sender_signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $handle;
+		$sender_signed_text = $item['guid'] . ';' . $parent['guid'] . ';' . $text . ';' . $handle;
 
 	// Sign the relayable with the top-level owner's signature
 	//
@@ -2401,7 +2418,7 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
 
 	$msg = replace_macros($tpl,array(
 		'$guid' => xmlify($item['guid']),
-		'$parent_guid' => xmlify($parent_guid),
+		'$parent_guid' => xmlify($parent['guid']),
 		'$target_type' =>xmlify($target_type),
 		'$authorsig' => xmlify($authorsig),
 		'$parentsig' => xmlify($parentauthorsig),
diff --git a/include/items.php b/include/items.php
index 388b27a196..6d58bd182a 100755
--- a/include/items.php
+++ b/include/items.php
@@ -890,7 +890,7 @@ function item_store($arr,$force_parent = false) {
 			if($r[0]['uri'] != $r[0]['parent-uri']) {
 				$arr['thr-parent'] = $arr['parent-uri'];
 				$arr['parent-uri'] = $r[0]['parent-uri'];
-				$z = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d 
+				$z = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d 
 					ORDER BY `id` ASC LIMIT 1",
 					dbesc($r[0]['parent-uri']),
 					dbesc($r[0]['parent-uri']),
diff --git a/mod/like.php b/mod/like.php
index 5519bffdb8..8fe0cf7353 100755
--- a/mod/like.php
+++ b/mod/like.php
@@ -106,11 +106,12 @@ function like_content(&$a) {
 
 
 	$r = q("SELECT * FROM `item` WHERE `verb` = '%s' AND `deleted` = 0 
-		AND `contact-id` = %d AND ( `parent` = '%s' OR `parent-uri` = '%s') LIMIT 1",
+		AND `contact-id` = %d AND ( `parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s') LIMIT 1",
 		dbesc($activity),
 		intval($contact['id']),
 		dbesc($item_id),
-		dbesc($item_id)
+		dbesc($item_id),
+		dbesc($item['uri'])
 	);
 	if(count($r)) {
 		$like_item = $r[0];