]> git.mxchange.org Git - friendica.git/blob - include/ostatus_conversation.php
OStatus: Attachment links are now added as enclosures
[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                         $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",
156                                 intval($message["uid"]), dbesc($first_id),
157                                 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
158                         if ($new_parents) {
159                                 $parent = $new_parents[0];
160                                 logger('adopting new parent '.$parent["id"].' for '.$itemid);
161                         } else {
162                                 $parent["id"] = 0;
163                                 $parent["uri"] = $first_id;
164                         }
165                 }
166
167                 if (isset($single_conv->context->inReplyTo->id))
168                         $parent_uri = $single_conv->context->inReplyTo->id;
169                 else
170                         $parent_uri = $parent["uri"];
171
172                 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `plink` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
173                                                         intval($message["uid"]), dbesc($plink),
174                                                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
175
176                 if (!$message_exists)
177                         $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
178                                                         intval($message["uid"]), dbesc($single_conv->id),
179                                                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
180
181                 if ($message_exists) {
182                         if ($parent["id"] != 0) {
183                                 $existing_message = $message_exists[0];
184
185                                 logger('updating id '.$existing_message["id"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG);
186
187                                 // This is partly bad, since the entry in the thread table isn't updated
188                                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
189                                         intval($parent["id"]),
190                                         dbesc($parent["uri"]),
191                                         dbesc($parent_uri),
192                                         intval($existing_message["id"]));
193                         }
194                         continue;
195                 }
196
197                 $actor = $single_conv->actor->id;
198                 if (isset($single_conv->actor->url))
199                         $actor = $single_conv->actor->url;
200
201                 $contact = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
202                                 $message["uid"], normalise_link($actor), NETWORK_STATUSNET);
203
204                 if (count($contact)) {
205                         logger("Found contact for url ".$actor, LOGGER_DEBUG);
206                         $contact_id = $contact[0]["id"];
207                 } else {
208                         logger("No contact found for url ".$actor, LOGGER_DEBUG);
209
210                         // Adding a global contact
211                         // To-Do: Use this data for the post
212                         $global_contact_id = get_contact($actor, 0);
213
214                         logger("Global contact ".$global_contact_id." found for url ".$actor, LOGGER_DEBUG);
215
216                         $contact_id = $parent["contact-id"];
217                 }
218
219                 $arr = array();
220                 $arr["network"] = NETWORK_OSTATUS;
221                 $arr["uri"] = $single_conv->id;
222                 $arr["plink"] = $plink;
223                 $arr["uid"] = $message["uid"];
224                 $arr["contact-id"] = $contact_id;
225                 if ($parent["id"] != 0)
226                         $arr["parent"] = $parent["id"];
227                 $arr["parent-uri"] = $parent["uri"];
228                 $arr["thr-parent"] = $parent_uri;
229                 $arr["created"] = $single_conv->published;
230                 $arr["edited"] = $single_conv->published;
231                 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
232                 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
233                 if ($arr["owner-name"] == '')
234                         $arr["owner-name"] = $single_conv->actor->portablecontacts_net->preferredUsername;
235                 if ($arr["owner-name"] == '')
236                         $arr["owner-name"] =  $single_conv->actor->displayName;
237
238                 $arr["owner-link"] = $actor;
239                 $arr["owner-avatar"] = $single_conv->actor->image->url;
240                 //$arr["author-name"] = $single_conv->actor->contact->displayName;
241                 //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
242                 $arr["author-name"] = $arr["owner-name"];
243                 $arr["author-link"] = $actor;
244                 $arr["author-avatar"] = $single_conv->actor->image->url;
245                 $arr["body"] = html2bbcode($single_conv->content);
246
247                 if (isset($single_conv->status_net->notice_info->source))
248                         $arr["app"] = strip_tags($single_conv->status_net->notice_info->source);
249                 elseif (isset($single_conv->statusnet->notice_info->source))
250                         $arr["app"] = strip_tags($single_conv->statusnet->notice_info->source);
251                 elseif (isset($single_conv->statusnet_notice_info->source))
252                         $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
253                 elseif (isset($single_conv->provider->displayName))
254                         $arr["app"] = $single_conv->provider->displayName;
255                 else
256                         $arr["app"] = "OStatus";
257
258                 $arr["verb"] = $parent["verb"];
259                 $arr["visible"] = $parent["visible"];
260                 $arr["location"] = $single_conv->location->displayName;
261                 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
262
263                 if ($arr["location"] == "")
264                         unset($arr["location"]);
265
266                 if ($arr["coord"] == "")
267                         unset($arr["coord"]);
268
269                 $newitem = item_store($arr);
270
271                 logger('Stored new item '.$plink.' under id '.$newitem, LOGGER_DEBUG);
272
273                 // Add the conversation entry (but don't fetch the whole conversation)
274                 complete_conversation($newitem, $conversation_url, true);
275
276                 // If the newly created item is the top item then change the parent settings of the thread
277                 if ($newitem AND ($arr["uri"] == $first_id)) {
278                         logger('setting new parent to id '.$newitem);
279                         $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
280                                 intval($message["uid"]), intval($newitem));
281                         if ($new_parents) {
282                                 $parent = $new_parents[0];
283                                 logger('done changing parents to parent '.$newitem);
284                         }
285                 }
286         }
287 }
288 ?>