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 * FROM `term` WHERE `type` = 7 AND `term` > '%s'",
58 foreach ($conversations AS $conversation) {
59 $id = $conversation['oid'];
60 $url = $conversation['url'];
61 complete_conversation($id, $url);
66 set_config('system','ostatus_last_poll', time());
69 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
72 $conversation_url = ostatus_convert_href($conversation_url);
74 if (intval(get_config('system','ostatus_poll_interval')) == -2)
77 if ($a->last_ostatus_conversation_url == $conversation_url)
80 $a->last_ostatus_conversation_url = $conversation_url;
82 $messages = q("SELECT `uid`, `parent`, `created`, `received`, `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
85 $message = $messages[0];
87 // Store conversation url if not done before
88 $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
89 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
92 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `created`, `received`, `guid`) VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s')",
93 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION),
94 dbesc($message["created"]), dbesc($conversation_url), dbesc($message["created"]), dbesc($message["received"]), dbesc($message["guid"]));
95 logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
98 if ($only_add_conversation)
102 $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
103 intval($message["uid"]), intval($message["parent"]));
106 $parent = $parents[0];
108 require_once('include/html2bbcode.php');
109 require_once('include/items.php');
111 $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
115 logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
118 $conv_as = fetch_url($conv."?page=".$pageno);
119 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
120 $conv_as = json_decode($conv_as);
122 if (@is_array($conv_as->items))
123 $items = array_merge($items, $conv_as->items);
134 $items = array_reverse($items);
136 foreach ($items as $single_conv) {
137 if (isset($single_conv->object->id))
138 $single_conv->id = $single_conv->object->id;
140 logger("Got id ".$single_conv->id, LOGGER_DEBUG);
142 $plink = ostatus_convert_href($single_conv->id);
143 if (isset($single_conv->object->url))
144 $plink = ostatus_convert_href($single_conv->object->url);
146 logger("Got url ".$plink, LOGGER_DEBUG);
148 if (@!$single_conv->id)
151 if ($first_id == "") {
152 $first_id = $single_conv->id;
154 $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",
155 intval($message["uid"]), dbesc($first_id),
156 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
158 $parent = $new_parents[0];
159 logger('adopting new parent '.$parent["id"].' for '.$itemid);
162 $parent["uri"] = $first_id;
166 if (isset($single_conv->context->inReplyTo->id))
167 $parent_uri = $single_conv->context->inReplyTo->id;
169 $parent_uri = $parent["uri"];
171 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `plink` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
172 intval($message["uid"]), dbesc($plink),
173 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
175 if (!$message_exists)
176 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
177 intval($message["uid"]), dbesc($single_conv->id),
178 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
180 if ($message_exists) {
181 if ($parent["id"] != 0) {
182 $existing_message = $message_exists[0];
184 logger('updating id '.$existing_message["id"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG);
186 // This is partly bad, since the entry in the thread table isn't updated
187 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
188 intval($parent["id"]),
189 dbesc($parent["uri"]),
191 intval($existing_message["id"]));
196 $actor = $single_conv->actor->id;
197 if (isset($single_conv->actor->url))
198 $actor = $single_conv->actor->url;
200 $contact = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
201 $message["uid"], normalise_link($actor), NETWORK_STATUSNET);
203 if (count($contact)) {
204 logger("Found contact for url ".$actor, LOGGER_DEBUG);
205 $contact_id = $contact[0]["id"];
207 logger("No contact found for url ".$actor, LOGGER_DEBUG);
209 // Adding a global contact
210 // To-Do: Use this data for the post
211 $global_contact_id = get_contact($actor, 0);
213 logger("Global contact ".$global_contact_id." found for url ".$actor, LOGGER_DEBUG);
215 $contact_id = $parent["contact-id"];
219 $arr["network"] = NETWORK_OSTATUS;
220 $arr["uri"] = $single_conv->id;
221 $arr["plink"] = $plink;
222 $arr["uid"] = $message["uid"];
223 $arr["contact-id"] = $contact_id;
224 if ($parent["id"] != 0)
225 $arr["parent"] = $parent["id"];
226 $arr["parent-uri"] = $parent["uri"];
227 $arr["thr-parent"] = $parent_uri;
228 $arr["created"] = $single_conv->published;
229 $arr["edited"] = $single_conv->published;
230 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
231 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
232 if ($arr["owner-name"] == '')
233 $arr["owner-name"] = $single_conv->actor->portablecontacts_net->preferredUsername;
234 if ($arr["owner-name"] == '')
235 $arr["owner-name"] = $single_conv->actor->displayName;
237 $arr["owner-link"] = $actor;
238 $arr["owner-avatar"] = $single_conv->actor->image->url;
239 //$arr["author-name"] = $single_conv->actor->contact->displayName;
240 //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
241 $arr["author-name"] = $arr["owner-name"];
242 $arr["author-link"] = $actor;
243 $arr["author-avatar"] = $single_conv->actor->image->url;
244 $arr["body"] = html2bbcode($single_conv->content);
246 if (isset($single_conv->status_net->notice_info->source))
247 $arr["app"] = strip_tags($single_conv->status_net->notice_info->source);
248 elseif (isset($single_conv->statusnet->notice_info->source))
249 $arr["app"] = strip_tags($single_conv->statusnet->notice_info->source);
250 elseif (isset($single_conv->statusnet_notice_info->source))
251 $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
252 elseif (isset($single_conv->provider->displayName))
253 $arr["app"] = $single_conv->provider->displayName;
255 $arr["app"] = "OStatus";
257 $arr["verb"] = $parent["verb"];
258 $arr["visible"] = $parent["visible"];
259 $arr["location"] = $single_conv->location->displayName;
260 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
262 if ($arr["location"] == "")
263 unset($arr["location"]);
265 if ($arr["coord"] == "")
266 unset($arr["coord"]);
268 $newitem = item_store($arr);
270 logger('Stored new item '.$plink.' under id '.$newitem, LOGGER_DEBUG);
272 // Add the conversation entry (but don't fetch the whole conversation)
273 complete_conversation($newitem, $conversation_url, true);
275 // If the newly created item is the top item then change the parent settings of the thread
276 if ($newitem AND ($arr["uri"] == $first_id)) {
277 logger('setting new parent to id '.$newitem);
278 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
279 intval($message["uid"]), intval($newitem));
281 $parent = $new_parents[0];
282 logger('done changing parents to parent '.$newitem);