From c9307eb96a1cd6f0f99ef01127a3ec6d356543ef Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Mon, 4 Feb 2013 00:57:23 +0100
Subject: [PATCH] OStatus conversation are now checked every 30 minutes for new
 answers.

---
 include/ostatus_conversation.php           | 61 ++++++++++++++++++----
 include/poller.php                         |  3 +-
 library/fancybox/jquery.fancybox-1.3.4.css | 16 +++---
 3 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/include/ostatus_conversation.php b/include/ostatus_conversation.php
index 3d40d673d1..cdaf80d76b 100644
--- a/include/ostatus_conversation.php
+++ b/include/ostatus_conversation.php
@@ -1,9 +1,39 @@
 <?php
-function complete_conversation($itemid, $conversation_url) {
-	global $a;
+define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
 
-	require_once('include/html2bbcode.php');
-	require_once('include/items.php');
+function check_conversations() {
+        $last = get_config('system','ostatus_last_poll');
+
+        $poll_interval = intval(get_config('system','ostatus_poll_interval'));
+        if(! $poll_interval)
+                $poll_interval = OSTATUS_DEFAULT_POLL_INTERVAL;
+
+        if($last) {
+                $next = $last + ($poll_interval * 60);
+                if($next > time()) {
+                        logger('complete_conversation: poll intervall not reached');
+                        return;
+                }
+        }
+
+        logger('complete_conversation: cron_start');
+
+        $start = date("Y-m-d H:i:s", time() - 86400);
+        $conversations = q("SELECT * FROM `term` WHERE `type` = 7 AND `term` > '%s'", 
+                                dbesc($start));
+        foreach ($conversations AS $conversation) {
+                $id = $conversation['oid'];
+                $url = $conversation['url'];
+                complete_conversation($id, $url);
+        }
+
+        logger('complete_conversation: cron_end');
+
+        set_config('system','ostatus_last_poll', time());
+}
+
+function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
+	global $a;
 
 	//logger('complete_conversation: completing conversation url '.$conversation_url.' for id '.$itemid);
 
@@ -12,13 +42,6 @@ function complete_conversation($itemid, $conversation_url) {
 		return;
 	$message = $messages[0];
 
-	// Get the parent
-	$parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
-			intval($message["uid"]), intval($message["parent"]));
-	if (!$parents)
-		return;
-	$parent = $parents[0];
-
 	// Store conversation url if not done before
 	$conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
 		intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
@@ -29,6 +52,19 @@ function complete_conversation($itemid, $conversation_url) {
 		logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
 	}
 
+	if ($only_add_conversation)
+		return;
+
+	// Get the parent
+	$parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
+			intval($message["uid"]), intval($message["parent"]));
+	if (!$parents)
+		return;
+	$parent = $parents[0];
+
+	require_once('include/html2bbcode.php');
+	require_once('include/items.php');
+
 	$conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
 
 	logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
@@ -111,6 +147,9 @@ function complete_conversation($itemid, $conversation_url) {
 
 			$newitem = item_store($arr);
 
+			// Add the conversation entry (but don't fetch the whole conversation)
+			complete_conversation($newitem, $conversation_url, true);
+
 			// If the newly created item is the top item then change the parent settings of the thread
 			if ($newitem AND ($arr["uri"] == $first_id)) {
 				logger('complete_conversation: setting new parent to id '.$newitem);
diff --git a/include/poller.php b/include/poller.php
index c9c46188f6..e85a4555d3 100644
--- a/include/poller.php
+++ b/include/poller.php
@@ -86,7 +86,8 @@ function poller_run(&$argv, &$argc){
 	if($abandon_days < 1)
 		$abandon_days = 0;
 
-
+	// Check OStatus conversations
+	check_conversations();
 
 	// once daily run birthday_updates and then expire in background
 
diff --git a/library/fancybox/jquery.fancybox-1.3.4.css b/library/fancybox/jquery.fancybox-1.3.4.css
index 6f53d8f4a2..50dbd67a89 100644
--- a/library/fancybox/jquery.fancybox-1.3.4.css
+++ b/library/fancybox/jquery.fancybox-1.3.4.css
@@ -349,11 +349,11 @@
 
 .fancybox-ie .fancybox-bg { background: transparent !important; }
 
-.fancybox-ie #fancybox-bg-n { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_n.png', sizingMethod='scale'); }
-.fancybox-ie #fancybox-bg-ne { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_ne.png', sizingMethod='scale'); }
-.fancybox-ie #fancybox-bg-e { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_e.png', sizingMethod='scale'); }
-.fancybox-ie #fancybox-bg-se { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_se.png', sizingMethod='scale'); }
-.fancybox-ie #fancybox-bg-s { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_s.png', sizingMethod='scale'); }
-.fancybox-ie #fancybox-bg-sw { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_sw.png', sizingMethod='scale'); }
-.fancybox-ie #fancybox-bg-w { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_w.png', sizingMethod='scale'); }
-.fancybox-ie #fancybox-bg-nw { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='fancybox/fancy_shadow_nw.png', sizingMethod='scale'); }
\ No newline at end of file
+.fancybox-ie #fancybox-bg-n { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='library/fancybox/fancy_shadow_n.png', sizingMethod='scale'); }
+.fancybox-ie #fancybox-bg-ne { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='library/fancybox/fancy_shadow_ne.png', sizingMethod='scale'); }
+.fancybox-ie #fancybox-bg-e { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='library/fancybox/fancy_shadow_e.png', sizingMethod='scale'); }
+.fancybox-ie #fancybox-bg-se { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='library/fancybox/fancy_shadow_se.png', sizingMethod='scale'); }
+.fancybox-ie #fancybox-bg-s { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='library/fancybox/fancy_shadow_s.png', sizingMethod='scale'); }
+.fancybox-ie #fancybox-bg-sw { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='library/fancybox/fancy_shadow_sw.png', sizingMethod='scale'); }
+.fancybox-ie #fancybox-bg-w { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='library/fancybox/fancy_shadow_w.png', sizingMethod='scale'); }
+.fancybox-ie #fancybox-bg-nw { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='library/fancybox/fancy_shadow_nw.png', sizingMethod='scale'); }