2 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
4 function check_conversations() {
5 $last = get_config('system','ostatus_last_poll');
7 $poll_interval = intval(get_config('system','ostatus_poll_interval'));
9 $poll_interval = OSTATUS_DEFAULT_POLL_INTERVAL;
12 $next = $last + ($poll_interval * 60);
14 logger('complete_conversation: poll intervall not reached');
19 logger('complete_conversation: cron_start');
21 $start = date("Y-m-d H:i:s", time() - 86400);
22 $conversations = q("SELECT * FROM `term` WHERE `type` = 7 AND `term` > '%s'",
24 foreach ($conversations AS $conversation) {
25 $id = $conversation['oid'];
26 $url = $conversation['url'];
27 complete_conversation($id, $url);
30 logger('complete_conversation: cron_end');
32 set_config('system','ostatus_last_poll', time());
35 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
38 //logger('complete_conversation: completing conversation url '.$conversation_url.' for id '.$itemid);
40 $messages = q("SELECT `uid`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
43 $message = $messages[0];
45 // Store conversation url if not done before
46 $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
47 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
50 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`) VALUES (%d, %d, %d, %d, '%s', '%s')",
51 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc(datetime_convert()), dbesc($conversation_url));
52 logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
55 if ($only_add_conversation)
59 $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
60 intval($message["uid"]), intval($message["parent"]));
63 $parent = $parents[0];
65 require_once('include/html2bbcode.php');
66 require_once('include/items.php');
68 $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
70 logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
71 $conv_as = fetch_url($conv);
74 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
75 $conv_as = json_decode($conv_as);
78 $items = array_reverse($conv_as->items);
80 foreach ($items as $single_conv) {
81 if ($first_id == "") {
82 $first_id = $single_conv->id;
84 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
85 intval($message["uid"]), dbesc($first_id));
87 $parent = $new_parents[0];
88 logger('complete_conversation: adopting new parent '.$parent["id"].' for '.$itemid);
91 $parent["uri"] = $first_id;
95 if (isset($single_conv->context->inReplyTo->id))
96 $parent_uri = $single_conv->context->inReplyTo->id;
98 $parent_uri = $parent["uri"];
100 if ($parent["id"] != 0) {
101 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
102 intval($message["uid"]), dbesc($single_conv->id));
103 if ($message_exists) {
104 $existing_message = $message_exists[0];
105 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d LIMIT 1",
106 intval($parent["id"]),
107 dbesc($parent["uri"]),
109 intval($existing_message["id"]));
115 $arr["uri"] = $single_conv->id;
116 $arr["plink"] = $single_conv->id;
117 $arr["uid"] = $message["uid"];
118 $arr["contact-id"] = $parent["contact-id"]; // To-Do
119 if ($parent["id"] != 0)
120 $arr["parent"] = $parent["id"];
121 $arr["parent-uri"] = $parent["uri"];
122 $arr["thr-parent"] = $parent_uri;
123 $arr["created"] = $single_conv->published;
124 $arr["edited"] = $single_conv->published;
125 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
126 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
127 $arr["owner-link"] = $single_conv->actor->id;
128 $arr["owner-avatar"] = $single_conv->actor->image->url;
129 //$arr["author-name"] = $single_conv->actor->contact->displayName;
130 $arr["author-name"] = $single_conv->actor->contact->preferredUsername;
131 $arr["author-link"] = $single_conv->actor->id;
132 $arr["author-avatar"] = $single_conv->actor->image->url;
133 $arr["body"] = html2bbcode($single_conv->content);
134 $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
135 if ($arr["app"] == "")
136 $arr["app"] = $single_conv->provider->displayName;
137 $arr["verb"] = $parent["verb"];
138 $arr["visible"] = $parent["visible"];
139 $arr["location"] = $single_conv->location->displayName;
140 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
142 if ($arr["location"] == "")
143 unset($arr["location"]);
145 if ($arr["coord"] == "")
146 unset($arr["coord"]);
148 $newitem = item_store($arr);
150 // Add the conversation entry (but don't fetch the whole conversation)
151 complete_conversation($newitem, $conversation_url, true);
153 // If the newly created item is the top item then change the parent settings of the thread
154 if ($newitem AND ($arr["uri"] == $first_id)) {
155 logger('complete_conversation: setting new parent to id '.$newitem);
156 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
157 intval($message["uid"]), intval($newitem));
159 $parent = $new_parents[0];
160 logger('complete_conversation: done changing parents to parent '.$newitem);
163 /*logger('complete_conversation: changing parents to parent '.$newitem.' old parent: '.$parent["id"].' new uri: '.$arr["uri"]);
164 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `parent` = %d",
167 intval($parent["id"]));
168 logger('complete_conversation: done changing parents to parent '.$newitem.' '.print_r($r, true));*/