From d2c6c21a88239e956b7e6a3c5a41a8a819502991 Mon Sep 17 00:00:00 2001
From: Friendika <info@friendika.com>
Date: Thu, 18 Aug 2011 21:09:44 -0700
Subject: [PATCH] lots more diaspora communications - can see light at the end

---
 include/diaspora.php | 140 ++++++++++++++++++++++++++++++++++++++++++-
 include/notifier.php |  20 ++++---
 2 files changed, 151 insertions(+), 9 deletions(-)

diff --git a/include/diaspora.php b/include/diaspora.php
index 6fe08dcd0e..422e7fc576 100644
--- a/include/diaspora.php
+++ b/include/diaspora.php
@@ -801,7 +801,7 @@ function diaspora_share($me,$contact) {
 function diaspora_send_status($item,$owner,$contact) {
 
 	$a = get_app();
-	$myaddr = $owner['nickname'] . '@' .  substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
+	$myaddr = $owner['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
 	$theiraddr = $contact['addr'];
 	require_once('include/bbcode.php');
 
@@ -830,3 +830,141 @@ function diaspora_send_status($item,$owner,$contact) {
 	return $return_code;
 }
 
+
+function diaspora_send_followup($item,$owner,$contact) {
+
+	$a = get_app();
+	$myaddr = $me['nickname'] . '@' .  substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
+	$theiraddr = $contact['addr'];
+
+	$p = q("select guid from item where parent = %d limit 1",
+		$item['parent']
+	);
+	if(count($p))
+		$parent_guid = $p[0]['guid'];
+	else
+		return;
+
+	if($item['verb'] === ACTIVITY_LIKE) {
+		$tpl = get_markup_template('diaspora_like.tpl');
+		$like = true;
+		$target_type = 'Post';
+		$positive = (($item['deleted']) ? 'false' : 'true');
+	}
+	else {
+		$tpl = get_markup_template('diaspora_comment.tpl');
+		$like = false;
+	}
+
+	$text = bbcode($item['body']);
+
+	// sign it
+
+	if($like)
+		$signed_text = $item['guid'] . ';' . $target_type . ';' . $positive . ';' . $myaddr;
+	else
+		$signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . $myaddr;
+
+	$authorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey']));
+
+	$msg = replace_macros($tpl,array(
+		'$guid' => xmlify($item['guid']),
+		'$parent_guid' => xmlify($parent_guid),
+		'$target_type' =>xmlify($target_type),
+		'$authorsig' => xmlify($authorsig),
+		'$text' => xmlify($text),
+		'$positive' => xmlify($positive),
+		'$diaspora_handle' => xmlify($myaddr)
+	));
+
+	logger('diaspora_followup: base message: ' . $msg, LOGGER_DATA);
+
+	$slap = 'xml=' . urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey']));
+
+	post_url($contact['notify'],$slap);
+	$return_code = $a->get_curl_code();
+	logger('diaspora_send_status: returns: ' . $return_code);
+	return $return_code;
+
+}
+
+
+function diaspora_send_relay($item,$owner,$contact) {
+
+
+	$p = q("select guid from item where parent = %d limit 1",
+		$item['parent']
+	);
+	if(count($p))
+		$parent_guid = $p[0]['guid'];
+	else
+		return;
+
+	// fetch the original signature	
+	$r = q("select * from sign where iid = %d limit 1",
+		intval($item['id'])
+	);
+	if(! count($r)) 
+		return;
+	$orig_sign = $r[0];
+
+	if($item['verb'] === ACTIVITY_LIKE) {
+		$tpl = get_markup_template('diaspora_like_relay.tpl');
+		$like = true;
+		$target_type = 'Post';
+		$positive = (($item['deleted']) ? 'false' : 'true');
+	}
+	else {
+		$tpl = get_markup_template('diaspora_comment_relay.tpl');
+		$like = false;
+	}
+
+	$text = bbcode($item['body']);
+
+	// sign it
+
+	if($like)
+		$parent_signed_text = $orig_sign['signed_text'];
+	else
+		$parent_signed_text = $orig_sign['signed_text'];
+
+	$parentauthorsig = base64_encode(rsa_sign($signed_text,$owner['uprvkey']));
+
+	$msg = replace_macros($tpl,array(
+		'$guid' => xmlify($item['guid']),
+		'$parent_guid' => xmlify($parent_guid),
+		'$target_type' =>xmlify($target_type),
+		'$authorsig' => xmlify($orig_sign['signature']),
+		'$parentsig' => xmlify($parentauthorsig),
+		'$text' => xmlify($text),
+		'$positive' => xmlify($positive),
+		'$diaspora_handle' => xmlify($myaddr)
+	));
+
+	// fetch the original signature	
+	$r = q("select * from sign where iid = %d limit 1",
+		intval($item['id'])
+	);
+	if(! count($r)) 
+		return;
+
+	logger('diaspora_relay_comment: base message: ' . $msg, LOGGER_DATA);
+
+	$slap = 'xml=' . urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey']));
+
+	post_url($contact['notify'],$slap);
+	$return_code = $a->get_curl_code();
+	logger('diaspora_send_status: returns: ' . $return_code);
+	return $return_code;
+
+}
+
+
+
+function diaspora_send_retraction($item,$owner,$contact) {
+
+
+
+
+
+}
\ No newline at end of file
diff --git a/include/notifier.php b/include/notifier.php
index 89c36e4341..afb907f099 100644
--- a/include/notifier.php
+++ b/include/notifier.php
@@ -504,20 +504,24 @@ function notifier_run($argv, $argc){
 				case NETWORK_DIASPORA:
 					if(get_config('system','dfrn_only') || (! get_config('diaspora_enabled')) || (! $normal_mode))
 						break;
-
-					if($target_item['deleted']) {
-						// diaspora delete, (check for like)
-
+					
+					if($target_item['verb'] === ACTIVITY_DISLIKE) {
+						// unsupported
+						break;
+					}
+					elseif($target_item['deleted'] && (! $parent_item['verb'] === ACTIVITY_LIKE)) {
+						// diaspora delete, 
+						diaspora_send_retraction($target_item,$owner,$contact);
 						break;
 					}
 					elseif($followup) {
-						// send to owner to relay
-
+						// send comments, likes and retractions of likes to owner to relay
+						diaspora_send_followup($target_item,$owner,$contact);
 						break;
 					}
 					elseif($target_item['parent'] != $target_item['id']) {
-						// we are the relay
-
+						// we are the relay - send comments, likes and unlikes to our conversants
+						diaspora_send_relay($target_item,$owner,$contact);
 						break;
 					}		
 					elseif($top_level) {
-- 
2.39.5