]> git.mxchange.org Git - friendica.git/blobdiff - include/ostatus_conversation.php
Merge pull request #1431 from thorsten23/develop
[friendica.git] / include / ostatus_conversation.php
index 480506a06d20bbbc4718badf7003118d249958b6..667f7dde427bdfd4d0c81a7c7bcf241fbdc36617 100644 (file)
@@ -20,15 +20,15 @@ function check_conversations() {
         if($last) {
                 $next = $last + ($poll_interval * 60);
                 if($next > time()) {
-                        logger('complete_conversation: poll interval not reached');
+                        logger('poll interval not reached');
                         return;
                 }
         }
 
-        logger('complete_conversation: cron_start');
+        logger('cron_start');
 
         $start = date("Y-m-d H:i:s", time() - ($poll_timeframe * 60));
-        $conversations = q("SELECT * FROM `term` WHERE `type` = 7 AND `term` > '%s'", 
+        $conversations = q("SELECT * FROM `term` WHERE `type` = 7 AND `term` > '%s'",
                                 dbesc($start));
         foreach ($conversations AS $conversation) {
                 $id = $conversation['oid'];
@@ -36,7 +36,7 @@ function check_conversations() {
                 complete_conversation($id, $url);
         }
 
-        logger('complete_conversation: cron_end');
+        logger(' cron_end');
 
         set_config('system','ostatus_last_poll', time());
 }
@@ -44,9 +44,15 @@ function check_conversations() {
 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
        global $a;
 
-       //logger('complete_conversation: completing conversation url '.$conversation_url.' for id '.$itemid);
+       if (intval(get_config('system','ostatus_poll_interval')) == -2)
+               return;
+
+       if ($a->last_ostatus_conversation_url == $conversation_url)
+               return;
 
-       $messages = q("SELECT `uid`, `parent`, `created` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
+       $a->last_ostatus_conversation_url = $conversation_url;
+
+       $messages = q("SELECT `uid`, `parent`, `created`, `received`, `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
        if (!$messages)
                return;
        $message = $messages[0];
@@ -56,8 +62,9 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
 
        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($message["created"]), dbesc($conversation_url));
+               $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `created`, `received`, `guid`) VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s')",
+                       intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION),
+                       dbesc($message["created"]), dbesc($conversation_url), dbesc($message["created"]), dbesc($message["received"]), dbesc($message["guid"]));
                logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
        }
 
@@ -81,7 +88,7 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
        logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
 
        do {
-               $conv_as = file_get_contents($conv."?page=".$pageno);
+               $conv_as = fetch_url($conv."?page=".$pageno);
                $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
                $conv_as = json_decode($conv_as);
 
@@ -100,7 +107,7 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
        $items = array_reverse($items);
 
        foreach ($items as $single_conv) {
-               // identi.ca just changed the format of the activity streams. This is a quick fix.
+               // status.net changed the format of the activity streams. This is a quick fix.
                if (@is_string($single_conv->object->id))
                        $single_conv->id = $single_conv->object->id;
 
@@ -117,7 +124,7 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                                intval($message["uid"]), dbesc($first_id));
                        if ($new_parents) {
                                $parent = $new_parents[0];
-                               logger('complete_conversation: adopting new parent '.$parent["id"].' for '.$itemid);
+                               logger('adopting new parent '.$parent["id"].' for '.$itemid);
                        } else {
                                $parent["id"] = 0;
                                $parent["uri"] = $first_id;
@@ -134,7 +141,9 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                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",
+
+                               // This is partly bad, since the entry in the thread table isn't updated
+                               $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
                                        intval($parent["id"]),
                                        dbesc($parent["uri"]),
                                        dbesc($parent_uri),
@@ -143,11 +152,23 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                        continue;
                }
 
+               $contact = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
+                               $message["uid"], normalise_link($single_conv->actor->id), NETWORK_STATUSNET);
+
+               if (count($contact)) {
+                       logger("Found contact for url ".$single_conv->actor->id, LOGGER_DEBUG);
+                       $contact_id = $contact[0]["id"];
+               } else {
+                       logger("No contact found for url ".$single_conv->actor->id, LOGGER_DEBUG);
+                       $contact_id = $parent["contact-id"];
+               }
+
                $arr = array();
+               $arr["network"] = NETWORK_OSTATUS;
                $arr["uri"] = $single_conv->id;
                $arr["plink"] = $single_conv->id;
                $arr["uid"] = $message["uid"];
-               $arr["contact-id"] = $parent["contact-id"]; // To-Do
+               $arr["contact-id"] = $contact_id;
                if ($parent["id"] != 0)
                        $arr["parent"] = $parent["id"];
                $arr["parent-uri"] = $parent["uri"];
@@ -190,20 +211,13 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
 
                // 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);
+                       logger('setting new parent to id '.$newitem);
                        $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
                                intval($message["uid"]), intval($newitem));
                        if ($new_parents) {
                                $parent = $new_parents[0];
-                               logger('complete_conversation: done changing parents to parent '.$newitem);
+                               logger('done changing parents to parent '.$newitem);
                        }
-
-                       /*logger('complete_conversation: changing parents to parent '.$newitem.' old parent: '.$parent["id"].' new uri: '.$arr["uri"]);
-                       $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `parent` = %d",
-                               intval($newitem),
-                               dbesc($arr["uri"]),
-                               intval($parent["id"]));
-                       logger('complete_conversation: done changing parents to parent '.$newitem.' '.print_r($r, true));*/
                }
        }
 }