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('complete_conversation: poll interval not reached');
28 logger('complete_conversation: cron_start');
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);
39 logger('complete_conversation: cron_end');
41 set_config('system','ostatus_last_poll', time());
44 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
47 //logger('complete_conversation: completing conversation url '.$conversation_url.' for id '.$itemid);
49 $messages = q("SELECT `uid`, `parent`, `created` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
52 $message = $messages[0];
54 // Store conversation url if not done before
55 $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
56 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
59 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`) VALUES (%d, %d, %d, %d, '%s', '%s')",
60 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc($message["created"]), dbesc($conversation_url));
61 logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
64 if ($only_add_conversation)
68 $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
69 intval($message["uid"]), intval($message["parent"]));
72 $parent = $parents[0];
74 require_once('include/html2bbcode.php');
75 require_once('include/items.php');
77 $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
81 logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
84 $conv_as = fetch_url($conv."?page=".$pageno);
85 //$conv_as = fetch_url($conv."?page=".$pageno, false, 0, 10);
86 //$conv_as = file_get_contents($conv."?page=".$pageno);
87 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
88 $conv_as = json_decode($conv_as);
90 if (@is_array($conv_as->items))
91 $items = array_merge($items, $conv_as->items);
102 $items = array_reverse($items);
104 foreach ($items as $single_conv) {
105 // identi.ca just changed the format of the activity streams. This is a quick fix.
106 if (@is_string($single_conv->object->id))
107 $single_conv->id = $single_conv->object->id;
109 if (@!$single_conv->id AND $single_conv->provider->url AND $single_conv->statusnet_notice_info->local_id)
110 $single_conv->id = $single_conv->provider->url."notice/".$single_conv->statusnet_notice_info->local_id;
112 if (@!$single_conv->id)
115 if ($first_id == "") {
116 $first_id = $single_conv->id;
118 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
119 intval($message["uid"]), dbesc($first_id));
121 $parent = $new_parents[0];
122 logger('complete_conversation: adopting new parent '.$parent["id"].' for '.$itemid);
125 $parent["uri"] = $first_id;
129 if (isset($single_conv->context->inReplyTo->id))
130 $parent_uri = $single_conv->context->inReplyTo->id;
132 $parent_uri = $parent["uri"];
134 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
135 intval($message["uid"]), dbesc($single_conv->id));
136 if ($message_exists) {
137 if ($parent["id"] != 0) {
138 $existing_message = $message_exists[0];
139 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d LIMIT 1",
140 intval($parent["id"]),
141 dbesc($parent["uri"]),
143 intval($existing_message["id"]));
149 $arr["uri"] = $single_conv->id;
150 $arr["plink"] = $single_conv->id;
151 $arr["uid"] = $message["uid"];
152 $arr["contact-id"] = $parent["contact-id"]; // To-Do
153 if ($parent["id"] != 0)
154 $arr["parent"] = $parent["id"];
155 $arr["parent-uri"] = $parent["uri"];
156 $arr["thr-parent"] = $parent_uri;
157 $arr["created"] = $single_conv->published;
158 $arr["edited"] = $single_conv->published;
159 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
160 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
161 if ($arr["owner-name"] == '')
162 $arr["owner-name"] = $single_conv->actor->portablecontacts_net->preferredUsername;
163 if ($arr["owner-name"] == '')
164 $arr["owner-name"] = $single_conv->actor->displayName;
166 $arr["owner-link"] = $single_conv->actor->id;
167 $arr["owner-avatar"] = $single_conv->actor->image->url;
168 //$arr["author-name"] = $single_conv->actor->contact->displayName;
169 //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
170 $arr["author-name"] = $arr["owner-name"];
171 $arr["author-link"] = $single_conv->actor->id;
172 $arr["author-avatar"] = $single_conv->actor->image->url;
173 $arr["body"] = html2bbcode($single_conv->content);
174 $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
175 if ($arr["app"] == "")
176 $arr["app"] = $single_conv->provider->displayName;
177 $arr["verb"] = $parent["verb"];
178 $arr["visible"] = $parent["visible"];
179 $arr["location"] = $single_conv->location->displayName;
180 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
182 if ($arr["location"] == "")
183 unset($arr["location"]);
185 if ($arr["coord"] == "")
186 unset($arr["coord"]);
188 $newitem = item_store($arr);
190 // Add the conversation entry (but don't fetch the whole conversation)
191 complete_conversation($newitem, $conversation_url, true);
193 // If the newly created item is the top item then change the parent settings of the thread
194 if ($newitem AND ($arr["uri"] == $first_id)) {
195 logger('complete_conversation: setting new parent to id '.$newitem);
196 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
197 intval($message["uid"]), intval($newitem));
199 $parent = $new_parents[0];
200 logger('complete_conversation: done changing parents to parent '.$newitem);
203 /*logger('complete_conversation: changing parents to parent '.$newitem.' old parent: '.$parent["id"].' new uri: '.$arr["uri"]);
204 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `parent` = %d",
207 intval($parent["id"]));
208 logger('complete_conversation: done changing parents to parent '.$newitem.' '.print_r($r, true));*/