2 require_once("include/Contact.php");
4 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
5 define('OSTATUS_DEFAULT_POLL_TIMEFRAME', 1440); // given in minutes
7 function ostatus_convert_href($href) {
8 $elements = explode(":",$href);
10 if ((count($elements) <= 2) OR ($elements[0] != "tag"))
13 $server = explode(",", $elements[1]);
14 $conversation = explode("=", $elements[2]);
16 if ((count($elements) == 4) AND ($elements[2] == "post"))
17 return "http://".$server[0]."/notice/".$elements[3];
19 if ((count($conversation) != 2) OR ($conversation[1] ==""))
22 if ($elements[3] == "objectType=thread")
23 return "http://".$server[0]."/conversation/".$conversation[1];
25 return "http://".$server[0]."/notice/".$conversation[1];
30 function check_conversations($override = false) {
31 $last = get_config('system','ostatus_last_poll');
33 $poll_interval = intval(get_config('system','ostatus_poll_interval'));
35 $poll_interval = OSTATUS_DEFAULT_POLL_INTERVAL;
37 // Don't poll if the interval is set negative
38 if (($poll_interval < 0) AND !$override)
41 $poll_timeframe = intval(get_config('system','ostatus_poll_timeframe'));
43 $poll_timeframe = OSTATUS_DEFAULT_POLL_TIMEFRAME;
45 if ($last AND !$override) {
46 $next = $last + ($poll_interval * 60);
48 logger('poll interval not reached');
55 $start = date("Y-m-d H:i:s", time() - ($poll_timeframe * 60));
56 $conversations = q("SELECT `oid`, `url` FROM `term` WHERE `type` = 7 AND `term` > '%s' GROUP BY `url` ORDER BY `term` DESC",
59 foreach ($conversations AS $conversation) {
60 $id = $conversation['oid'];
61 $url = $conversation['url'];
62 complete_conversation($id, $url);
67 set_config('system','ostatus_last_poll', time());
70 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
73 $conversation_url = ostatus_convert_href($conversation_url);
75 if (intval(get_config('system','ostatus_poll_interval')) == -2)
78 if ($a->last_ostatus_conversation_url == $conversation_url)
81 $a->last_ostatus_conversation_url = $conversation_url;
83 $messages = q("SELECT `uid`, `parent`, `created`, `received`, `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
86 $message = $messages[0];
88 // Store conversation url if not done before
89 $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
90 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
93 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `created`, `received`, `guid`) VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s')",
94 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION),
95 dbesc($message["created"]), dbesc($conversation_url), dbesc($message["created"]), dbesc($message["received"]), dbesc($message["guid"]));
96 logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
99 if ($only_add_conversation)
103 $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
104 intval($message["uid"]), intval($message["parent"]));
107 $parent = $parents[0];
109 require_once('include/html2bbcode.php');
110 require_once('include/items.php');
112 $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
116 logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
119 $conv_as = fetch_url($conv."?page=".$pageno);
120 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
121 $conv_as = json_decode($conv_as);
123 if (@is_array($conv_as->items))
124 $items = array_merge($items, $conv_as->items);
135 $items = array_reverse($items);
137 foreach ($items as $single_conv) {
138 if (isset($single_conv->object->id))
139 $single_conv->id = $single_conv->object->id;
141 //logger("Got id ".$single_conv->id, LOGGER_DEBUG);
143 $plink = ostatus_convert_href($single_conv->id);
144 if (isset($single_conv->object->url))
145 $plink = ostatus_convert_href($single_conv->object->url);
147 //logger("Got url ".$plink, LOGGER_DEBUG);
149 if (@!$single_conv->id)
152 if ($first_id == "") {
153 $first_id = $single_conv->id;
156 // Only fetch a new parent if the new one doesn't have parents
157 // It can happen that OStatus servers have incomplete threads.
158 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
159 intval($message["uid"]), dbesc($first_id),
160 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
162 $parent = $new_parents[0];
163 if ($parent["id"] != $message["parent"])
164 logger('Fetch new parent id '.$parent["id"].' for '.$itemid.' Old parent: '.$message["parent"]);
167 $parent["uri"] = $first_id;
171 if (isset($single_conv->context->inReplyTo->id))
172 $parent_uri = $single_conv->context->inReplyTo->id;
174 $parent_uri = $parent["uri"];
176 $message_exists = q("SELECT `id`, `parent` FROM `item` WHERE `uid` = %d AND `plink` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
177 intval($message["uid"]), dbesc($plink),
178 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
180 if (!$message_exists)
181 $message_exists = q("SELECT `id`, `parent` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
182 intval($message["uid"]), dbesc($single_conv->id),
183 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
185 if ($message_exists) {
186 if ($parent["id"] != 0) {
187 $existing_message = $message_exists[0];
189 // Normally this shouldn't happen anymore, since we improved the way we fetch OStatus messages
190 if ($existing_message["parent"] != $parent["id"]) {
191 logger('updating id '.$existing_message["id"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG);
193 // This is partly bad, since the entry in the thread table isn't updated
194 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
195 intval($parent["id"]),
196 dbesc($parent["uri"]),
198 intval($existing_message["id"]));
204 $actor = $single_conv->actor->id;
205 if (isset($single_conv->actor->url))
206 $actor = $single_conv->actor->url;
208 $contact = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
209 $message["uid"], normalise_link($actor), NETWORK_STATUSNET);
211 if (count($contact)) {
212 logger("Found contact for url ".$actor, LOGGER_DEBUG);
213 $contact_id = $contact[0]["id"];
215 logger("No contact found for url ".$actor, LOGGER_DEBUG);
217 // Adding a global contact
218 // To-Do: Use this data for the post
219 $global_contact_id = get_contact($actor, 0);
221 logger("Global contact ".$global_contact_id." found for url ".$actor, LOGGER_DEBUG);
223 $contact_id = $parent["contact-id"];
227 $arr["network"] = NETWORK_OSTATUS;
228 $arr["uri"] = $single_conv->id;
229 $arr["plink"] = $plink;
230 $arr["uid"] = $message["uid"];
231 $arr["contact-id"] = $contact_id;
232 if ($parent["id"] != 0)
233 $arr["parent"] = $parent["id"];
234 $arr["parent-uri"] = $parent["uri"];
235 $arr["thr-parent"] = $parent_uri;
236 $arr["created"] = $single_conv->published;
237 $arr["edited"] = $single_conv->published;
238 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
239 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
240 if ($arr["owner-name"] == '')
241 $arr["owner-name"] = $single_conv->actor->portablecontacts_net->preferredUsername;
242 if ($arr["owner-name"] == '')
243 $arr["owner-name"] = $single_conv->actor->displayName;
245 $arr["owner-link"] = $actor;
246 $arr["owner-avatar"] = $single_conv->actor->image->url;
247 //$arr["author-name"] = $single_conv->actor->contact->displayName;
248 //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
249 $arr["author-name"] = $arr["owner-name"];
250 $arr["author-link"] = $actor;
251 $arr["author-avatar"] = $single_conv->actor->image->url;
252 $arr["body"] = add_page_info_to_body(html2bbcode($single_conv->content));
254 if (isset($single_conv->status_net->notice_info->source))
255 $arr["app"] = strip_tags($single_conv->status_net->notice_info->source);
256 elseif (isset($single_conv->statusnet->notice_info->source))
257 $arr["app"] = strip_tags($single_conv->statusnet->notice_info->source);
258 elseif (isset($single_conv->statusnet_notice_info->source))
259 $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
260 elseif (isset($single_conv->provider->displayName))
261 $arr["app"] = $single_conv->provider->displayName;
263 $arr["app"] = "OStatus";
265 $arr["verb"] = $parent["verb"];
266 $arr["visible"] = $parent["visible"];
267 $arr["location"] = $single_conv->location->displayName;
268 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
270 if ($arr["location"] == "")
271 unset($arr["location"]);
273 if ($arr["coord"] == "")
274 unset($arr["coord"]);
276 $newitem = item_store($arr);
278 logger('Stored new item '.$plink.' under id '.$newitem, LOGGER_DEBUG);
280 // Add the conversation entry (but don't fetch the whole conversation)
281 complete_conversation($newitem, $conversation_url, true);
283 // If the newly created item is the top item then change the parent settings of the thread
284 // This shouldn't happen anymore. This could is supposed to be absolote.
285 if ($newitem AND ($arr["uri"] == $first_id)) {
286 logger('setting new parent to id '.$newitem);
287 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
288 intval($message["uid"]), intval($newitem));
290 $parent = $new_parents[0];
291 logger('done changing parents to parent '.$newitem);