From bfc0857953d10eec15df807704c3948da962d8ac Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Mon, 7 Dec 2015 21:52:42 +0100
Subject: [PATCH] OStatus: Send even Friendica comments to OStatus

---
 include/notifier.php |  4 +-
 include/ostatus.php  | 92 +++++++++++++++++++++++++++++++++++++-------
 include/session.php  | 83 +++++++++++++++++++++------------------
 3 files changed, 127 insertions(+), 52 deletions(-)

diff --git a/include/notifier.php b/include/notifier.php
index ec2b666bed..553f2918db 100644
--- a/include/notifier.php
+++ b/include/notifier.php
@@ -303,7 +303,8 @@ function notifier_run(&$argv, &$argc){
 			$conversant_str = dbesc($parent['contact-id']);
 			$recipients = array($parent['contact-id']);
 
-			if (!$target_item['private'] AND $target_item['wall'] AND
+			//if (!$target_item['private'] AND $target_item['wall'] AND
+			if (!$target_item['private'] AND
 				(strlen($target_item['allow_cid'].$target_item['allow_gid'].
 					$target_item['deny_cid'].$target_item['deny_gid']) == 0))
 				$push_notify = true;
@@ -329,6 +330,7 @@ function notifier_run(&$argv, &$argc){
 				if (count($url_recipients))
 					logger("url_recipients ".print_r($url_recipients,true));
 			}
+			logger("Notify ".$target_item["guid"]." via PuSH: ".($push_notify?"Yes":"No"), LOGGER_DEBUG);
 		} else {
 			$followup = false;
 
diff --git a/include/ostatus.php b/include/ostatus.php
index 932fc1fa9a..34f8eccc51 100644
--- a/include/ostatus.php
+++ b/include/ostatus.php
@@ -9,6 +9,7 @@ require_once("include/socgraph.php");
 require_once("include/Photo.php");
 require_once("include/Scrape.php");
 require_once("include/follow.php");
+require_once("include/api.php");
 require_once("mod/proxy.php");
 
 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
@@ -1318,10 +1319,13 @@ class="attachment thumbnail" id="attachment-572819" rel="nofollow external">http
 
 */
 
-function ostatus_entry($doc, $item, $owner, $toplevel = false) {
+function ostatus_entry($doc, $item, $owner, $toplevel = false, $repeat = false) {
 	$a = get_app();
 
-	if (!$toplevel) {
+	if (!$toplevel AND $repeat) {
+		$entry = $doc->createElement("activity:object");
+		$title = sprintf("New note by %s", $owner["nick"]);
+	} elseif (!$toplevel) {
 		$entry = $doc->createElement("entry");
 		$title = sprintf("New note by %s", $owner["nick"]);
 	} else {
@@ -1359,7 +1363,11 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
 	// But: it seems as if it doesn't federate well between the GS servers
 	// So we just set it to "note" to be sure that it reaches their target systems
 
-	xml_add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
+	if (!$repeat)
+		xml_add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
+	else
+		xml_add_element($doc, $entry, "activity:object-type", NAMESPACE_ACTIVITY_SCHEMA.'activity');
+
 	xml_add_element($doc, $entry, "id", $item["uri"]);
 	xml_add_element($doc, $entry, "title", $title);
 
@@ -1382,12 +1390,54 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
 							"href" => $a->get_baseurl()."/display/".$item["guid"]));
 
 	xml_add_element($doc, $entry, "status_net", "", array("notice_id" => $item["id"]));
-	xml_add_element($doc, $entry, "activity:verb", construct_verb($item));
+
+	$repeated_item = $item;
+	$is_repeat = false;
+
+	if (!$repeat)
+		$is_repeat = api_share_as_retweet($repeated_item);
+
+	if (!$is_repeat)
+		xml_add_element($doc, $entry, "activity:verb", construct_verb($item));
+	else
+		xml_add_element($doc, $entry, "activity:verb", ACTIVITY_SHARE);
+
 	xml_add_element($doc, $entry, "published", datetime_convert("UTC","UTC",$item["created"]."+00:00",ATOM_TIME));
 	xml_add_element($doc, $entry, "updated", datetime_convert("UTC","UTC",$item["edited"]."+00:00",ATOM_TIME));
 
+	if ($is_repeat) {
+		$repeated_owner = array();
+		$repeated_owner["name"] = $repeated_item["author-name"];
+		$repeated_owner["url"] = $repeated_item["author-link"];
+		$repeated_owner["photo"] = normalise_link($repeated_item["author-avatar"]);
+		$repeated_owner["nick"] = "";
+		$repeated_owner["location"] = "";
+		$repeated_owner["about"] = "";
+		$repeated_owner["uid"] = 0;
+
+		$r =q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", normalise_link($repeated_item["author-link"]));
+		if ($r) {
+			$repeated_owner["nick"] = $r[0]["nick"];
+			$repeated_owner["location"] = $r[0]["location"];
+			$repeated_owner["about"] = $r[0]["about"];
+		}
+
+		$entry_repeat = ostatus_entry($doc, $repeated_item, $repeated_owner, false, true);
+		$entry->appendChild($entry_repeat);
+	} elseif ($repeat) {
+		$profile = array();
+		$profile["name"] = $owner["name"];
+		$profile["photo"] = $owner["photo"];
+		$profile["thumb"] = $owner["photo"];
+		$profile["about"] = $owner["about"];
+		$author = ostatus_add_author($doc, $owner, $profile);
+		$entry->appendChild($author);
+	}
+
+	$mentioned = array();
+
 	if (($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
-		$parent = q("SELECT `guid` FROM `item` WHERE `id` = %d", intval($item["parent"]));
+		$parent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `id` = %d", intval($item["parent"]));
 		$parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
 
 		$attributes = array(
@@ -1400,6 +1450,9 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
 				"rel" => "related",
 				"href" => $a->get_baseurl()."/display/".$parent[0]["guid"]);
 		xml_add_element($doc, $entry, "link", "", $attributes);
+
+		$mentioned[$parent[0]["author-link"]] = $parent[0]["author-link"];
+		$mentioned[$parent[0]["owner-link"]] = $parent[0]["owner-link"];
         }
 
 	xml_add_element($doc, $entry, "link", "", array("rel" => "ostatus:conversation",
@@ -1411,9 +1464,12 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
 	if(count($tags))
 		foreach($tags as $t)
 			if ($t[0] == "@")
-				xml_add_element($doc, $entry, "link", "", array("rel" => "mentioned",
-										"ostatus:object-type" => ACTIVITY_OBJ_PERSON,
-										"href" => $t[1]));
+				$mentioned[$t[1]] = $t[1];
+
+	foreach ($mentioned AS $mention)
+		xml_add_element($doc, $entry, "link", "", array("rel" => "mentioned",
+									"ostatus:object-type" => ACTIVITY_OBJ_PERSON,
+									"href" => $mention));
 
 	if (!$item["private"])
 		xml_add_element($doc, $entry, "link", "", array("rel" => "mentioned",
@@ -1441,7 +1497,12 @@ function ostatus_entry($doc, $item, $owner, $toplevel = false) {
 	if ($app == "")
 		$app = "web";
 
-	xml_add_element($doc, $entry, "statusnet:notice_info", "", array("local_id" => $item["id"], "source" => $app));
+
+	$attributes = array("local_id" => $item["id"], "source" => $app);
+	if ($is_repeat)
+		$attributes["repeat_of"] = $item["id"];
+
+	xml_add_element($doc, $entry, "statusnet:notice_info", "", $attributes);
 
 	return $entry;
 }
@@ -1468,12 +1529,17 @@ function ostatus_feed(&$a, $owner_nick, $last_update) {
 			WHERE `item`.`uid` = %d AND `item`.`received` > '%s' AND NOT `item`.`private` AND NOT `item`.`deleted`
 				AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
 				AND ((`item`.`wall` AND (`item`.`parent` = `item`.`id`))
-					OR (`item`.`network` = '%s' AND ((`thread`.`network`='%s') OR (`thritem`.`network` = '%s'))) AND `thread`.`mention`)
-				AND (`item`.`owner-link` IN ('%s', '%s'))
+					OR (`item`.`network` = '%s' AND ((`thread`.`network` IN ('%s', '%s')) OR (`thritem`.`network` IN ('%s', '%s')))) AND `thread`.`mention`)
+				AND ((`item`.`owner-link` IN ('%s', '%s') AND (`item`.`parent` = `item`.`id`))
+					OR (`item`.`author-link` IN ('%s', '%s')))
 			ORDER BY `item`.`received` DESC
 			LIMIT 0, 300",
-			intval($owner["uid"]), dbesc($check_date),
-			dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_OSTATUS),
+			intval($owner["uid"]), dbesc($check_date), dbesc(NETWORK_DFRN),
+			//dbesc(NETWORK_OSTATUS), dbesc(NETWORK_OSTATUS),
+			//dbesc(NETWORK_OSTATUS), dbesc(NETWORK_OSTATUS),
+			dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN),
+			dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN),
+			dbesc($owner["nurl"]), dbesc(str_replace("http://", "https://", $owner["nurl"])),
 			dbesc($owner["nurl"]), dbesc(str_replace("http://", "https://", $owner["nurl"]))
 		);
 
diff --git a/include/session.php b/include/session.php
index 6632b7e89a..dda84e77a8 100644
--- a/include/session.php
+++ b/include/session.php
@@ -8,62 +8,69 @@ $session_expire = 180000;
 
 if(! function_exists('ref_session_open')) {
 function ref_session_open ($s,$n) {
-  return true;
+	return true;
 }}
 
 if(! function_exists('ref_session_read')) {
 function ref_session_read ($id) {
-  global $session_exists;
-  if(x($id))
-    $r = q("SELECT `data` FROM `session` WHERE `sid`= '%s'", dbesc($id));
-  if(count($r)) {
-    $session_exists = true;
-    return $r[0]['data'];
-  } else {
-    logger("no data for session $id", LOGGER_TRACE);
-  }
-  return '';
+	global $session_exists;
+	if(x($id))
+		$r = q("SELECT `data` FROM `session` WHERE `sid`= '%s'", dbesc($id));
+
+	if(count($r)) {
+		$session_exists = true;
+		return $r[0]['data'];
+	} else {
+		logger("no data for session $id", LOGGER_TRACE);
+	}
+	return '';
 }}
 
 if(! function_exists('ref_session_write')) {
 function ref_session_write ($id,$data) {
-  global $session_exists, $session_expire;
-  if(! $id || ! $data) {
-    return false;
-  }
-
-  $expire = time() + $session_expire;
-  $default_expire = time() + 300;
-
-  if($session_exists)
-    $r = q("UPDATE `session`
-            SET `data` = '%s', `expire` = '%s'
-            WHERE `sid` = '%s'",
-            dbesc($data), dbesc($expire), dbesc($id));
-  else
-    $r = q("INSERT INTO `session`
-            SET `sid` = '%s', `expire` = '%s', `data` = '%s'",
-            dbesc($id), dbesc($default_expire), dbesc($data));
-
-  return true;
+	global $session_exists, $session_expire;
+
+	if(! $id || ! $data) {
+		return false;
+	}
+
+	$expire = time() + $session_expire;
+	$default_expire = time() + 300;
+
+	if($session_exists) {
+		$r = q("UPDATE `session`
+				SET `data` = '%s'
+				WHERE `data` != '%s' AND `sid` = '%s'",
+				dbesc($data), dbesc($data), dbesc($id));
+
+		$r = q("UPDATE `session`
+				SET `expire` = '%s'
+				WHERE `expire` != '%s' AND `sid` = '%s'",
+				dbesc($expire), dbesc($expire), dbesc($id));
+	} else
+		$r = q("INSERT INTO `session`
+				SET `sid` = '%s', `expire` = '%s', `data` = '%s'",
+				dbesc($id), dbesc($default_expire), dbesc($data));
+
+	return true;
 }}
 
 if(! function_exists('ref_session_close')) {
 function ref_session_close() {
-  return true;
+	return true;
 }}
 
 if(! function_exists('ref_session_destroy')) {
 function ref_session_destroy ($id) {
-  q("DELETE FROM `session` WHERE `sid` = '%s'", dbesc($id));
-  return true;
+	q("DELETE FROM `session` WHERE `sid` = '%s'", dbesc($id));
+	return true;
 }}
 
 if(! function_exists('ref_session_gc')) {
 function ref_session_gc($expire) {
-  q("DELETE FROM `session` WHERE `expire` < %d", dbesc(time()));
-  q("OPTIMIZE TABLE `sess_data`");
-  return true;
+	q("DELETE FROM `session` WHERE `expire` < %d", dbesc(time()));
+	q("OPTIMIZE TABLE `sess_data`");
+	return true;
 }}
 
 $gc_probability = 50;
@@ -74,5 +81,5 @@ ini_set('session.cookie_httponly', 1);
 
 
 session_set_save_handler ('ref_session_open', 'ref_session_close',
-                            'ref_session_read', 'ref_session_write',
-                            'ref_session_destroy', 'ref_session_gc');
+			'ref_session_read', 'ref_session_write',
+			'ref_session_destroy', 'ref_session_gc');
-- 
2.39.5