- private function completion($conversation_url, $uid, $item = array(), $self = "") {
-
- /// @todo This function is totally ugly and has to be rewritten totally
-
- // Import all threads or only threads that were started by our followers?
- $all_threads = !get_config('system','ostatus_full_threads');
-
- $item_stored = -1;
-
- $conversation_url = self::fetch_conversation($self, $conversation_url);
-
- // If the thread shouldn't be completed then store the item and go away
- // Don't do a completion on liked content
- if (((intval(get_config('system','ostatus_poll_interval')) == -2) && (count($item) > 0)) ||
- ($item["verb"] == ACTIVITY_LIKE) || ($conversation_url == "")) {
- $item_stored = item_store($item, $all_threads);
- return $item_stored;
- }
-
- // Get the parent
- $parents = q("SELECT `item`.`id`, `item`.`parent`, `item`.`uri`, `item`.`contact-id`, `item`.`type`,
- `item`.`verb`, `item`.`visible` FROM `term`
- STRAIGHT_JOIN `item` AS `thritem` ON `thritem`.`parent` = `term`.`oid`
- STRAIGHT_JOIN `item` ON `item`.`parent` = `thritem`.`parent`
- WHERE `term`.`uid` = %d AND `term`.`otype` = %d AND `term`.`type` = %d AND `term`.`url` = '%s'",
- intval($uid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc($conversation_url));
- if ($parents)
- $parent = $parents[0];
- elseif (count($item) > 0) {
- $parent = $item;
- $parent["type"] = "remote";
- $parent["verb"] = ACTIVITY_POST;
- $parent["visible"] = 1;
- } else {
- // Preset the parent
- $r = q("SELECT `id` FROM `contact` WHERE `self` AND `uid`=%d", $uid);
- if (!$r)
- return(-2);
-
- $parent = array();
- $parent["id"] = 0;
- $parent["parent"] = 0;
- $parent["uri"] = "";
- $parent["contact-id"] = $r[0]["id"];
- $parent["type"] = "remote";
- $parent["verb"] = ACTIVITY_POST;
- $parent["visible"] = 1;
- }
-
- $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
- $pageno = 1;
- $items = array();
-
- logger('fetching conversation url '.$conv.' (Self: '.$self.') for user '.$uid);
-
- do {
- $conv_arr = z_fetch_url($conv."?page=".$pageno);
-
- // If it is a non-ssl site and there is an error, then try ssl or vice versa
- if (!$conv_arr["success"] && (substr($conv, 0, 7) == "http://")) {
- $conv = str_replace("http://", "https://", $conv);
- $conv_as = fetch_url($conv."?page=".$pageno);
- } elseif (!$conv_arr["success"] && (substr($conv, 0, 8) == "https://")) {
- $conv = str_replace("https://", "http://", $conv);
- $conv_as = fetch_url($conv."?page=".$pageno);
- } else
- $conv_as = $conv_arr["body"];
-
- $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
- $conv_as = json_decode($conv_as);
-
- $no_of_items = sizeof($items);
-
- if (@is_array($conv_as->items))
- foreach ($conv_as->items AS $single_item)
- $items[$single_item->id] = $single_item;
-
- if ($no_of_items == sizeof($items))
- break;
-
- $pageno++;
-
- } while (true);
-
- logger('fetching conversation done. Found '.count($items).' items');
-
- if (!sizeof($items)) {
- if (count($item) > 0) {
- $item_stored = item_store($item, $all_threads);
-
- if ($item_stored) {
- logger("Conversation ".$conversation_url." couldn't be fetched. Item uri ".$item["uri"]." stored: ".$item_stored, LOGGER_DEBUG);
- self::store_conversation($item_id, $conversation_url);
- }
-
- return($item_stored);
- } else
- return(-3);