]> git.mxchange.org Git - friendica.git/blob - include/ostatus_conversation.php
restore jotplugins
[friendica.git] / include / ostatus_conversation.php
1 <?php
2 require_once("include/Contact.php");
3
4 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
5 define('OSTATUS_DEFAULT_POLL_TIMEFRAME', 1440); // given in minutes
6
7 function ostatus_convert_href($href) {
8         $elements = explode(":",$href);
9
10         if ((count($elements) <= 2) OR ($elements[0] != "tag"))
11                 return $href;
12
13         $server = explode(",", $elements[1]);
14         $conversation = explode("=", $elements[2]);
15
16         if ((count($elements) == 4) AND ($elements[2] == "post"))
17                 return "http://".$server[0]."/notice/".$elements[3];
18
19         if ((count($conversation) != 2) OR ($conversation[1] ==""))
20                 return $href;
21
22         if ($elements[3] == "objectType=thread")
23                 return "http://".$server[0]."/conversation/".$conversation[1];
24         else
25                 return "http://".$server[0]."/notice/".$conversation[1];
26
27         return $href;
28 }
29
30 function check_conversations($override = false) {
31         $last = get_config('system','ostatus_last_poll');
32
33         $poll_interval = intval(get_config('system','ostatus_poll_interval'));
34         if(! $poll_interval)
35                 $poll_interval = OSTATUS_DEFAULT_POLL_INTERVAL;
36
37         // Don't poll if the interval is set negative
38         if (($poll_interval < 0) AND !$override)
39                 return;
40
41         $poll_timeframe = intval(get_config('system','ostatus_poll_timeframe'));
42         if (!$poll_timeframe)
43                 $poll_timeframe = OSTATUS_DEFAULT_POLL_TIMEFRAME;
44
45         if ($last AND !$override) {
46                 $next = $last + ($poll_interval * 60);
47                 if ($next > time()) {
48                         logger('poll interval not reached');
49                         return;
50                 }
51         }
52
53         logger('cron_start');
54
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",
57                                 dbesc($start));
58
59         foreach ($conversations AS $conversation) {
60                 $id = $conversation['oid'];
61                 $url = $conversation['url'];
62                 complete_conversation($id, $url);
63         }
64
65         logger('cron_end');
66
67         set_config('system','ostatus_last_poll', time());
68 }
69
70 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
71         global $a;
72
73         $conversation_url = ostatus_convert_href($conversation_url);
74
75         if (intval(get_config('system','ostatus_poll_interval')) == -2)
76                 return;
77
78         if ($a->last_ostatus_conversation_url == $conversation_url)
79                 return;
80
81         $a->last_ostatus_conversation_url = $conversation_url;
82
83         $messages = q("SELECT `uid`, `parent`, `created`, `received`, `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
84         if (!$messages)
85                 return;
86         $message = $messages[0];
87
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));
91
92         if (!$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);
97         }
98
99         if ($only_add_conversation)
100                 return;
101
102         // Get the parent
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"]));
105         if (!$parents)
106                 return;
107         $parent = $parents[0];
108
109         require_once('include/html2bbcode.php');
110         require_once('include/items.php');
111
112         $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
113         $pageno = 1;
114         $items = array();
115
116         logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
117
118         do {
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);
122
123                 if (@is_array($conv_as->items))
124                         $items = array_merge($items, $conv_as->items);
125                 else
126                         break;
127
128                 $pageno++;
129
130         } while (true);
131
132         if (!sizeof($items))
133                 return;
134
135         $items = array_reverse($items);
136
137         foreach ($items as $single_conv) {
138                 if (isset($single_conv->object->id))
139                         $single_conv->id = $single_conv->object->id;
140
141                 //logger("Got id ".$single_conv->id, LOGGER_DEBUG);
142
143                 $plink = ostatus_convert_href($single_conv->id);
144                 if (isset($single_conv->object->url))
145                         $plink = ostatus_convert_href($single_conv->object->url);
146
147                 //logger("Got url ".$plink, LOGGER_DEBUG);
148
149                 if (@!$single_conv->id)
150                         continue;
151
152                 if ($first_id == "") {
153                         $first_id = $single_conv->id;
154
155                         // To-Do:
156                         // Only fetch a new parent if the new one doesn't have parents
157                         // It can happen that OStatus servers have incomplete threads.
158                         $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",
159                                 intval($message["uid"]), dbesc($first_id),
160                                 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
161                         if ($new_parents) {
162                                 $parent = $new_parents[0];
163                                 if ($parent["id"] != $message["parent"])
164                                         logger('Fetch new parent id '.$parent["id"].' for '.$itemid.' Old parent: '.$message["parent"]);
165                         } else {
166                                 $parent["id"] = 0;
167                                 $parent["uri"] = $first_id;
168                         }
169                 }
170
171                 if (isset($single_conv->context->inReplyTo->id))
172                         $parent_uri = $single_conv->context->inReplyTo->id;
173                 else
174                         $parent_uri = $parent["uri"];
175
176                 $message_exists = q("SELECT `id`, `parent` FROM `item` WHERE `uid` = %d AND `plink` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
177                                                         intval($message["uid"]), dbesc($plink),
178                                                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
179
180                 if (!$message_exists)
181                         $message_exists = q("SELECT `id`, `parent` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
182                                                         intval($message["uid"]), dbesc($single_conv->id),
183                                                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
184
185                 if ($message_exists) {
186                         if ($parent["id"] != 0) {
187                                 $existing_message = $message_exists[0];
188
189                                 // Normally this shouldn't happen anymore, since we improved the way we fetch OStatus messages
190                                 if ($existing_message["parent"] != $parent["id"]) {
191                                         logger('updating id '.$existing_message["id"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG);
192
193                                         // This is partly bad, since the entry in the thread table isn't updated
194                                         $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
195                                                 intval($parent["id"]),
196                                                 dbesc($parent["uri"]),
197                                                 dbesc($parent_uri),
198                                                 intval($existing_message["id"]));
199                                 }
200                         }
201                         continue;
202                 }
203
204                 $actor = $single_conv->actor->id;
205                 if (isset($single_conv->actor->url))
206                         $actor = $single_conv->actor->url;
207
208                 $contact = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
209                                 $message["uid"], normalise_link($actor), NETWORK_STATUSNET);
210
211                 if (count($contact)) {
212                         logger("Found contact for url ".$actor, LOGGER_DEBUG);
213                         $contact_id = $contact[0]["id"];
214                 } else {
215                         logger("No contact found for url ".$actor, LOGGER_DEBUG);
216
217                         // Adding a global contact
218                         // To-Do: Use this data for the post
219                         $global_contact_id = get_contact($actor, 0);
220
221                         logger("Global contact ".$global_contact_id." found for url ".$actor, LOGGER_DEBUG);
222
223                         $contact_id = $parent["contact-id"];
224                 }
225
226                 $arr = array();
227                 $arr["network"] = NETWORK_OSTATUS;
228                 $arr["uri"] = $single_conv->id;
229                 $arr["plink"] = $plink;
230                 $arr["uid"] = $message["uid"];
231                 $arr["contact-id"] = $contact_id;
232                 if ($parent["id"] != 0)
233                         $arr["parent"] = $parent["id"];
234                 $arr["parent-uri"] = $parent["uri"];
235                 $arr["thr-parent"] = $parent_uri;
236                 $arr["created"] = $single_conv->published;
237                 $arr["edited"] = $single_conv->published;
238                 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
239                 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
240                 if ($arr["owner-name"] == '')
241                         $arr["owner-name"] = $single_conv->actor->portablecontacts_net->preferredUsername;
242                 if ($arr["owner-name"] == '')
243                         $arr["owner-name"] =  $single_conv->actor->displayName;
244
245                 $arr["owner-link"] = $actor;
246                 $arr["owner-avatar"] = $single_conv->actor->image->url;
247                 //$arr["author-name"] = $single_conv->actor->contact->displayName;
248                 //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
249                 $arr["author-name"] = $arr["owner-name"];
250                 $arr["author-link"] = $actor;
251                 $arr["author-avatar"] = $single_conv->actor->image->url;
252                 $arr["body"] = add_page_info_to_body(html2bbcode($single_conv->content));
253
254                 if (isset($single_conv->status_net->notice_info->source))
255                         $arr["app"] = strip_tags($single_conv->status_net->notice_info->source);
256                 elseif (isset($single_conv->statusnet->notice_info->source))
257                         $arr["app"] = strip_tags($single_conv->statusnet->notice_info->source);
258                 elseif (isset($single_conv->statusnet_notice_info->source))
259                         $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
260                 elseif (isset($single_conv->provider->displayName))
261                         $arr["app"] = $single_conv->provider->displayName;
262                 else
263                         $arr["app"] = "OStatus";
264
265                 $arr["verb"] = $parent["verb"];
266                 $arr["visible"] = $parent["visible"];
267                 $arr["location"] = $single_conv->location->displayName;
268                 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
269
270                 if ($arr["location"] == "")
271                         unset($arr["location"]);
272
273                 if ($arr["coord"] == "")
274                         unset($arr["coord"]);
275
276                 $newitem = item_store($arr);
277
278                 logger('Stored new item '.$plink.' under id '.$newitem, LOGGER_DEBUG);
279
280                 // Add the conversation entry (but don't fetch the whole conversation)
281                 complete_conversation($newitem, $conversation_url, true);
282
283                 // If the newly created item is the top item then change the parent settings of the thread
284                 // This shouldn't happen anymore. This could is supposed to be absolote.
285                 if ($newitem AND ($arr["uri"] == $first_id)) {
286                         logger('setting new parent to id '.$newitem);
287                         $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
288                                 intval($message["uid"]), intval($newitem));
289                         if ($new_parents) {
290                                 $parent = $new_parents[0];
291                                 logger('done changing parents to parent '.$newitem);
292                         }
293                 }
294         }
295 }
296 ?>