2 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
3 define('OSTATUS_DEFAULT_POLL_TIMEFRAME', 1440); // given in minutes
5 function check_conversations() {
6 $last = get_config('system','ostatus_last_poll');
8 $poll_interval = intval(get_config('system','ostatus_poll_interval'));
10 $poll_interval = OSTATUS_DEFAULT_POLL_INTERVAL;
12 // Don't poll if the interval is set negative
13 if ($poll_interval < 0)
16 $poll_timeframe = intval(get_config('system','ostatus_poll_timeframe'));
18 $poll_timeframe = OSTATUS_DEFAULT_POLL_TIMEFRAME;
21 $next = $last + ($poll_interval * 60);
23 logger('poll interval not reached');
30 $start = date("Y-m-d H:i:s", time() - ($poll_timeframe * 60));
31 $conversations = q("SELECT * FROM `term` WHERE `type` = 7 AND `term` > '%s'",
33 foreach ($conversations AS $conversation) {
34 $id = $conversation['oid'];
35 $url = $conversation['url'];
36 complete_conversation($id, $url);
41 set_config('system','ostatus_last_poll', time());
44 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
47 if (intval(get_config('system','ostatus_poll_interval')) == -2)
50 if ($a->last_ostatus_conversation_url == $conversation_url)
53 $a->last_ostatus_conversation_url = $conversation_url;
55 $messages = q("SELECT `uid`, `parent`, `created`, `received`, `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
58 $message = $messages[0];
60 // Store conversation url if not done before
61 $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
62 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
65 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `created`, `received`, `guid`) VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s')",
66 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION),
67 dbesc($message["created"]), dbesc($conversation_url), dbesc($message["created"]), dbesc($message["received"]), dbesc($message["guid"]));
68 logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
71 if ($only_add_conversation)
75 $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
76 intval($message["uid"]), intval($message["parent"]));
79 $parent = $parents[0];
81 require_once('include/html2bbcode.php');
82 require_once('include/items.php');
84 $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
88 logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
91 $conv_as = fetch_url($conv."?page=".$pageno);
92 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
93 $conv_as = json_decode($conv_as);
95 if (@is_array($conv_as->items))
96 $items = array_merge($items, $conv_as->items);
107 $items = array_reverse($items);
109 foreach ($items as $single_conv) {
110 // status.net changed the format of the activity streams. This is a quick fix.
111 if (@is_string($single_conv->object->id))
112 $single_conv->id = $single_conv->object->id;
114 if (@!$single_conv->id AND $single_conv->provider->url AND $single_conv->statusnet_notice_info->local_id)
115 $single_conv->id = $single_conv->provider->url."notice/".$single_conv->statusnet_notice_info->local_id;
117 if (@!$single_conv->id)
120 if ($first_id == "") {
121 $first_id = $single_conv->id;
123 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
124 intval($message["uid"]), dbesc($first_id));
126 $parent = $new_parents[0];
127 logger('adopting new parent '.$parent["id"].' for '.$itemid);
130 $parent["uri"] = $first_id;
134 if (isset($single_conv->context->inReplyTo->id))
135 $parent_uri = $single_conv->context->inReplyTo->id;
137 $parent_uri = $parent["uri"];
139 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
140 intval($message["uid"]), dbesc($single_conv->id));
141 if ($message_exists) {
142 if ($parent["id"] != 0) {
143 $existing_message = $message_exists[0];
145 // This is partly bad, since the entry in the thread table isn't updated
146 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
147 intval($parent["id"]),
148 dbesc($parent["uri"]),
150 intval($existing_message["id"]));
155 $contact = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
156 $message["uid"], normalise_link($single_conv->actor->id), NETWORK_STATUSNET);
158 if (count($contact)) {
159 logger("Found contact for url ".$single_conv->actor->id, LOGGER_DEBUG);
160 $contact_id = $contact[0]["id"];
162 logger("No contact found for url ".$single_conv->actor->id, LOGGER_DEBUG);
163 $contact_id = $parent["contact-id"];
167 $arr["network"] = NETWORK_OSTATUS;
168 $arr["uri"] = $single_conv->id;
169 $arr["plink"] = $single_conv->id;
170 $arr["uid"] = $message["uid"];
171 $arr["contact-id"] = $contact_id;
172 if ($parent["id"] != 0)
173 $arr["parent"] = $parent["id"];
174 $arr["parent-uri"] = $parent["uri"];
175 $arr["thr-parent"] = $parent_uri;
176 $arr["created"] = $single_conv->published;
177 $arr["edited"] = $single_conv->published;
178 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
179 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
180 if ($arr["owner-name"] == '')
181 $arr["owner-name"] = $single_conv->actor->portablecontacts_net->preferredUsername;
182 if ($arr["owner-name"] == '')
183 $arr["owner-name"] = $single_conv->actor->displayName;
185 $arr["owner-link"] = $single_conv->actor->id;
186 $arr["owner-avatar"] = $single_conv->actor->image->url;
187 //$arr["author-name"] = $single_conv->actor->contact->displayName;
188 //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
189 $arr["author-name"] = $arr["owner-name"];
190 $arr["author-link"] = $single_conv->actor->id;
191 $arr["author-avatar"] = $single_conv->actor->image->url;
192 $arr["body"] = html2bbcode($single_conv->content);
193 $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
194 if ($arr["app"] == "")
195 $arr["app"] = $single_conv->provider->displayName;
196 $arr["verb"] = $parent["verb"];
197 $arr["visible"] = $parent["visible"];
198 $arr["location"] = $single_conv->location->displayName;
199 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
201 if ($arr["location"] == "")
202 unset($arr["location"]);
204 if ($arr["coord"] == "")
205 unset($arr["coord"]);
207 $newitem = item_store($arr);
209 // Add the conversation entry (but don't fetch the whole conversation)
210 complete_conversation($newitem, $conversation_url, true);
212 // If the newly created item is the top item then change the parent settings of the thread
213 if ($newitem AND ($arr["uri"] == $first_id)) {
214 logger('setting new parent to id '.$newitem);
215 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
216 intval($message["uid"]), intval($newitem));
218 $parent = $new_parents[0];
219 logger('done changing parents to parent '.$newitem);