2 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
4 function check_conversations() {
5 $last = get_config('system','ostatus_last_poll');
7 $poll_interval = intval(get_config('system','ostatus_poll_interval'));
9 $poll_interval = OSTATUS_DEFAULT_POLL_INTERVAL;
12 $next = $last + ($poll_interval * 60);
14 logger('complete_conversation: poll intervall not reached');
19 logger('complete_conversation: cron_start');
21 $start = date("Y-m-d H:i:s", time() - 86400);
22 $conversations = q("SELECT * FROM `term` WHERE `type` = 7 AND `term` > '%s'",
24 foreach ($conversations AS $conversation) {
25 $id = $conversation['oid'];
26 $url = $conversation['url'];
27 complete_conversation($id, $url);
30 logger('complete_conversation: cron_end');
32 set_config('system','ostatus_last_poll', time());
35 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
38 //logger('complete_conversation: completing conversation url '.$conversation_url.' for id '.$itemid);
40 $messages = q("SELECT `uid`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
43 $message = $messages[0];
45 // Store conversation url if not done before
46 $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
47 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
50 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`) VALUES (%d, %d, %d, %d, '%s', '%s')",
51 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc(datetime_convert()), dbesc($conversation_url));
52 logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
55 if ($only_add_conversation)
59 $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
60 intval($message["uid"]), intval($message["parent"]));
63 $parent = $parents[0];
65 require_once('include/html2bbcode.php');
66 require_once('include/items.php');
68 $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
70 logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
71 $conv_as = fetch_url($conv);
74 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
75 $conv_as = json_decode($conv_as);
79 if (!is_array($conv_as->items))
81 $items = array_reverse($conv_as->items);
83 foreach ($items as $single_conv) {
84 if ($first_id == "") {
85 $first_id = $single_conv->id;
87 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
88 intval($message["uid"]), dbesc($first_id));
90 $parent = $new_parents[0];
91 logger('complete_conversation: adopting new parent '.$parent["id"].' for '.$itemid);
94 $parent["uri"] = $first_id;
98 if (isset($single_conv->context->inReplyTo->id))
99 $parent_uri = $single_conv->context->inReplyTo->id;
101 $parent_uri = $parent["uri"];
103 if ($parent["id"] != 0) {
104 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
105 intval($message["uid"]), dbesc($single_conv->id));
106 if ($message_exists) {
107 $existing_message = $message_exists[0];
108 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d LIMIT 1",
109 intval($parent["id"]),
110 dbesc($parent["uri"]),
112 intval($existing_message["id"]));
118 $arr["uri"] = $single_conv->id;
119 $arr["plink"] = $single_conv->id;
120 $arr["uid"] = $message["uid"];
121 $arr["contact-id"] = $parent["contact-id"]; // To-Do
122 if ($parent["id"] != 0)
123 $arr["parent"] = $parent["id"];
124 $arr["parent-uri"] = $parent["uri"];
125 $arr["thr-parent"] = $parent_uri;
126 $arr["created"] = $single_conv->published;
127 $arr["edited"] = $single_conv->published;
128 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
129 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
130 $arr["owner-link"] = $single_conv->actor->id;
131 $arr["owner-avatar"] = $single_conv->actor->image->url;
132 //$arr["author-name"] = $single_conv->actor->contact->displayName;
133 $arr["author-name"] = $single_conv->actor->contact->preferredUsername;
134 $arr["author-link"] = $single_conv->actor->id;
135 $arr["author-avatar"] = $single_conv->actor->image->url;
136 $arr["body"] = html2bbcode($single_conv->content);
137 $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
138 if ($arr["app"] == "")
139 $arr["app"] = $single_conv->provider->displayName;
140 $arr["verb"] = $parent["verb"];
141 $arr["visible"] = $parent["visible"];
142 $arr["location"] = $single_conv->location->displayName;
143 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
145 if ($arr["location"] == "")
146 unset($arr["location"]);
148 if ($arr["coord"] == "")
149 unset($arr["coord"]);
151 $newitem = item_store($arr);
153 // Add the conversation entry (but don't fetch the whole conversation)
154 complete_conversation($newitem, $conversation_url, true);
156 // If the newly created item is the top item then change the parent settings of the thread
157 if ($newitem AND ($arr["uri"] == $first_id)) {
158 logger('complete_conversation: setting new parent to id '.$newitem);
159 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
160 intval($message["uid"]), intval($newitem));
162 $parent = $new_parents[0];
163 logger('complete_conversation: done changing parents to parent '.$newitem);
166 /*logger('complete_conversation: changing parents to parent '.$newitem.' old parent: '.$parent["id"].' new uri: '.$arr["uri"]);
167 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `parent` = %d",
170 intval($parent["id"]));
171 logger('complete_conversation: done changing parents to parent '.$newitem.' '.print_r($r, true));*/