]> git.mxchange.org Git - friendica.git/blobdiff - include/ostatus_conversation.php
OStatus conversation: now the creation date of the item is store with the conversation
[friendica.git] / include / ostatus_conversation.php
index cdaf80d76b1eeedcc990f373fa617ed116c1e042..ef9e4f17937b11cf5f5369ce7bf3be89c33dce63 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
+define('OSTATUS_DEFAULT_POLL_TIMEFRAME', 1440); // given in minutes
 
 function check_conversations() {
         $last = get_config('system','ostatus_last_poll');
@@ -8,17 +9,25 @@ function check_conversations() {
         if(! $poll_interval)
                 $poll_interval = OSTATUS_DEFAULT_POLL_INTERVAL;
 
+       // Don't poll if the interval is set negative
+       if ($poll_interval < 0)
+               return;
+
+        $poll_timeframe = intval(get_config('system','ostatus_poll_timeframe'));
+        if(! $poll_timeframe)
+                $poll_timeframe = OSTATUS_DEFAULT_POLL_TIMEFRAME;
+
         if($last) {
                 $next = $last + ($poll_interval * 60);
                 if($next > time()) {
-                        logger('complete_conversation: poll intervall not reached');
+                        logger('complete_conversation: poll interval not reached');
                         return;
                 }
         }
 
         logger('complete_conversation: cron_start');
 
-        $start = date("Y-m-d H:i:s", time() - 86400);
+        $start = date("Y-m-d H:i:s", time() - ($poll_timeframe * 60));
         $conversations = q("SELECT * FROM `term` WHERE `type` = 7 AND `term` > '%s'", 
                                 dbesc($start));
         foreach ($conversations AS $conversation) {
@@ -37,7 +46,7 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
 
        //logger('complete_conversation: completing conversation url '.$conversation_url.' for id '.$itemid);
 
-       $messages = q("SELECT `uid`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
+       $messages = q("SELECT `uid`, `parent`, `created` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
        if (!$messages)
                return;
        $message = $messages[0];
@@ -48,7 +57,7 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
 
        if (!$conversation) {
                $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`) VALUES (%d, %d, %d, %d, '%s', '%s')",
-                       intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc(datetime_convert()), dbesc($conversation_url));
+                       intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc($message["created"]), dbesc($conversation_url));
                logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
        }
 
@@ -75,6 +84,9 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                $conv_as = json_decode($conv_as);
 
                $first_id = "";
+
+                if (!is_array($conv_as->items))
+                    return;
                $items = array_reverse($conv_as->items);
 
                foreach ($items as $single_conv) {
@@ -97,18 +109,18 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                        else
                                $parent_uri = $parent["uri"];
 
-                       if ($parent["id"] != 0) {
-                               $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
+                       $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
                                                        intval($message["uid"]), dbesc($single_conv->id));
-                               if ($message_exists) {
+                       if ($message_exists) {
+                               if ($parent["id"] != 0) {
                                        $existing_message = $message_exists[0];
                                        $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d LIMIT 1",
                                                intval($parent["id"]),
                                                dbesc($parent["uri"]),
                                                dbesc($parent_uri),
                                                intval($existing_message["id"]));
-                                       continue;
                                }
+                               continue;
                        }
 
                        $arr = array();