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;
155 $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",
156 intval($message["uid"]), dbesc($first_id),
157 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
159 $parent = $new_parents[0];
160 logger('adopting new parent '.$parent["id"].' for '.$itemid);
163 $parent["uri"] = $first_id;
167 if (isset($single_conv->context->inReplyTo->id))
168 $parent_uri = $single_conv->context->inReplyTo->id;
170 $parent_uri = $parent["uri"];
172 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `plink` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
173 intval($message["uid"]), dbesc($plink),
174 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
176 if (!$message_exists)
177 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
178 intval($message["uid"]), dbesc($single_conv->id),
179 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
181 if ($message_exists) {
182 if ($parent["id"] != 0) {
183 $existing_message = $message_exists[0];
185 logger('updating id '.$existing_message["id"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG);
187 // This is partly bad, since the entry in the thread table isn't updated
188 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
189 intval($parent["id"]),
190 dbesc($parent["uri"]),
192 intval($existing_message["id"]));
197 $actor = $single_conv->actor->id;
198 if (isset($single_conv->actor->url))
199 $actor = $single_conv->actor->url;
201 $contact = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
202 $message["uid"], normalise_link($actor), NETWORK_STATUSNET);
204 if (count($contact)) {
205 logger("Found contact for url ".$actor, LOGGER_DEBUG);
206 $contact_id = $contact[0]["id"];
208 logger("No contact found for url ".$actor, LOGGER_DEBUG);
210 // Adding a global contact
211 // To-Do: Use this data for the post
212 $global_contact_id = get_contact($actor, 0);
214 logger("Global contact ".$global_contact_id." found for url ".$actor, LOGGER_DEBUG);
216 $contact_id = $parent["contact-id"];
220 $arr["network"] = NETWORK_OSTATUS;
221 $arr["uri"] = $single_conv->id;
222 $arr["plink"] = $plink;
223 $arr["uid"] = $message["uid"];
224 $arr["contact-id"] = $contact_id;
225 if ($parent["id"] != 0)
226 $arr["parent"] = $parent["id"];
227 $arr["parent-uri"] = $parent["uri"];
228 $arr["thr-parent"] = $parent_uri;
229 $arr["created"] = $single_conv->published;
230 $arr["edited"] = $single_conv->published;
231 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
232 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
233 if ($arr["owner-name"] == '')
234 $arr["owner-name"] = $single_conv->actor->portablecontacts_net->preferredUsername;
235 if ($arr["owner-name"] == '')
236 $arr["owner-name"] = $single_conv->actor->displayName;
238 $arr["owner-link"] = $actor;
239 $arr["owner-avatar"] = $single_conv->actor->image->url;
240 //$arr["author-name"] = $single_conv->actor->contact->displayName;
241 //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
242 $arr["author-name"] = $arr["owner-name"];
243 $arr["author-link"] = $actor;
244 $arr["author-avatar"] = $single_conv->actor->image->url;
245 $arr["body"] = html2bbcode($single_conv->content);
247 if (isset($single_conv->status_net->notice_info->source))
248 $arr["app"] = strip_tags($single_conv->status_net->notice_info->source);
249 elseif (isset($single_conv->statusnet->notice_info->source))
250 $arr["app"] = strip_tags($single_conv->statusnet->notice_info->source);
251 elseif (isset($single_conv->statusnet_notice_info->source))
252 $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
253 elseif (isset($single_conv->provider->displayName))
254 $arr["app"] = $single_conv->provider->displayName;
256 $arr["app"] = "OStatus";
258 $arr["verb"] = $parent["verb"];
259 $arr["visible"] = $parent["visible"];
260 $arr["location"] = $single_conv->location->displayName;
261 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
263 if ($arr["location"] == "")
264 unset($arr["location"]);
266 if ($arr["coord"] == "")
267 unset($arr["coord"]);
269 $newitem = item_store($arr);
271 logger('Stored new item '.$plink.' under id '.$newitem, LOGGER_DEBUG);
273 // Add the conversation entry (but don't fetch the whole conversation)
274 complete_conversation($newitem, $conversation_url, true);
276 // If the newly created item is the top item then change the parent settings of the thread
277 if ($newitem AND ($arr["uri"] == $first_id)) {
278 logger('setting new parent to id '.$newitem);
279 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
280 intval($message["uid"]), intval($newitem));
282 $parent = $new_parents[0];
283 logger('done changing parents to parent '.$newitem);