]> git.mxchange.org Git - friendica.git/blob - include/ostatus_conversation.php
html to section_title.tpl
[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 * FROM `term` WHERE `type` = 7 AND `term` > '%s'",
57                                 dbesc($start));
58         foreach ($conversations AS $conversation) {
59                 $id = $conversation['oid'];
60                 $url = $conversation['url'];
61                 complete_conversation($id, $url);
62         }
63
64         logger('cron_end');
65
66         set_config('system','ostatus_last_poll', time());
67 }
68
69 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
70         global $a;
71
72         $conversation_url = ostatus_convert_href($conversation_url);
73
74         if (intval(get_config('system','ostatus_poll_interval')) == -2)
75                 return;
76
77         if ($a->last_ostatus_conversation_url == $conversation_url)
78                 return;
79
80         $a->last_ostatus_conversation_url = $conversation_url;
81
82         $messages = q("SELECT `uid`, `parent`, `created`, `received`, `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
83         if (!$messages)
84                 return;
85         $message = $messages[0];
86
87         // Store conversation url if not done before
88         $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
89                 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
90
91         if (!$conversation) {
92                 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `created`, `received`, `guid`) VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s')",
93                         intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION),
94                         dbesc($message["created"]), dbesc($conversation_url), dbesc($message["created"]), dbesc($message["received"]), dbesc($message["guid"]));
95                 logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
96         }
97
98         if ($only_add_conversation)
99                 return;
100
101         // Get the parent
102         $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
103                         intval($message["uid"]), intval($message["parent"]));
104         if (!$parents)
105                 return;
106         $parent = $parents[0];
107
108         require_once('include/html2bbcode.php');
109         require_once('include/items.php');
110
111         $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
112         $pageno = 1;
113         $items = array();
114
115         logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
116
117         do {
118                 $conv_as = fetch_url($conv."?page=".$pageno);
119                 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
120                 $conv_as = json_decode($conv_as);
121
122                 if (@is_array($conv_as->items))
123                         $items = array_merge($items, $conv_as->items);
124                 else
125                         break;
126
127                 $pageno++;
128
129         } while (true);
130
131         if (!sizeof($items))
132                 return;
133
134         $items = array_reverse($items);
135
136         foreach ($items as $single_conv) {
137                 if (isset($single_conv->object->id))
138                         $single_conv->id = $single_conv->object->id;
139
140                 logger("Got id ".$single_conv->id, LOGGER_DEBUG);
141
142                 $plink = ostatus_convert_href($single_conv->id);
143                 if (isset($single_conv->object->url))
144                         $plink = ostatus_convert_href($single_conv->object->url);
145
146                 logger("Got url ".$plink, LOGGER_DEBUG);
147
148                 if (@!$single_conv->id)
149                         continue;
150
151                 if ($first_id == "") {
152                         $first_id = $single_conv->id;
153
154                         $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",
155                                 intval($message["uid"]), dbesc($first_id),
156                                 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
157                         if ($new_parents) {
158                                 $parent = $new_parents[0];
159                                 logger('adopting new parent '.$parent["id"].' for '.$itemid);
160                         } else {
161                                 $parent["id"] = 0;
162                                 $parent["uri"] = $first_id;
163                         }
164                 }
165
166                 if (isset($single_conv->context->inReplyTo->id))
167                         $parent_uri = $single_conv->context->inReplyTo->id;
168                 else
169                         $parent_uri = $parent["uri"];
170
171                 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `plink` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
172                                                         intval($message["uid"]), dbesc($plink),
173                                                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
174
175                 if (!$message_exists)
176                         $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
177                                                         intval($message["uid"]), dbesc($single_conv->id),
178                                                         dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
179
180                 if ($message_exists) {
181                         if ($parent["id"] != 0) {
182                                 $existing_message = $message_exists[0];
183
184                                 logger('updating id '.$existing_message["id"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG);
185
186                                 // This is partly bad, since the entry in the thread table isn't updated
187                                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
188                                         intval($parent["id"]),
189                                         dbesc($parent["uri"]),
190                                         dbesc($parent_uri),
191                                         intval($existing_message["id"]));
192                         }
193                         continue;
194                 }
195
196                 $actor = $single_conv->actor->id;
197                 if (isset($single_conv->actor->url))
198                         $actor = $single_conv->actor->url;
199
200                 $contact = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
201                                 $message["uid"], normalise_link($actor), NETWORK_STATUSNET);
202
203                 if (count($contact)) {
204                         logger("Found contact for url ".$actor, LOGGER_DEBUG);
205                         $contact_id = $contact[0]["id"];
206                 } else {
207                         logger("No contact found for url ".$actor, LOGGER_DEBUG);
208
209                         // Adding a global contact
210                         // To-Do: Use this data for the post
211                         $global_contact_id = get_contact($actor, 0);
212
213                         logger("Global contact ".$global_contact_id." found for url ".$actor, LOGGER_DEBUG);
214
215                         $contact_id = $parent["contact-id"];
216                 }
217
218                 $arr = array();
219                 $arr["network"] = NETWORK_OSTATUS;
220                 $arr["uri"] = $single_conv->id;
221                 $arr["plink"] = $plink;
222                 $arr["uid"] = $message["uid"];
223                 $arr["contact-id"] = $contact_id;
224                 if ($parent["id"] != 0)
225                         $arr["parent"] = $parent["id"];
226                 $arr["parent-uri"] = $parent["uri"];
227                 $arr["thr-parent"] = $parent_uri;
228                 $arr["created"] = $single_conv->published;
229                 $arr["edited"] = $single_conv->published;
230                 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
231                 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
232                 if ($arr["owner-name"] == '')
233                         $arr["owner-name"] = $single_conv->actor->portablecontacts_net->preferredUsername;
234                 if ($arr["owner-name"] == '')
235                         $arr["owner-name"] =  $single_conv->actor->displayName;
236
237                 $arr["owner-link"] = $actor;
238                 $arr["owner-avatar"] = $single_conv->actor->image->url;
239                 //$arr["author-name"] = $single_conv->actor->contact->displayName;
240                 //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
241                 $arr["author-name"] = $arr["owner-name"];
242                 $arr["author-link"] = $actor;
243                 $arr["author-avatar"] = $single_conv->actor->image->url;
244                 $arr["body"] = html2bbcode($single_conv->content);
245
246                 if (isset($single_conv->status_net->notice_info->source))
247                         $arr["app"] = strip_tags($single_conv->status_net->notice_info->source);
248                 elseif (isset($single_conv->statusnet->notice_info->source))
249                         $arr["app"] = strip_tags($single_conv->statusnet->notice_info->source);
250                 elseif (isset($single_conv->statusnet_notice_info->source))
251                         $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
252                 elseif (isset($single_conv->provider->displayName))
253                         $arr["app"] = $single_conv->provider->displayName;
254                 else
255                         $arr["app"] = "OStatus";
256
257                 $arr["verb"] = $parent["verb"];
258                 $arr["visible"] = $parent["visible"];
259                 $arr["location"] = $single_conv->location->displayName;
260                 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
261
262                 if ($arr["location"] == "")
263                         unset($arr["location"]);
264
265                 if ($arr["coord"] == "")
266                         unset($arr["coord"]);
267
268                 $newitem = item_store($arr);
269
270                 logger('Stored new item '.$plink.' under id '.$newitem, LOGGER_DEBUG);
271
272                 // Add the conversation entry (but don't fetch the whole conversation)
273                 complete_conversation($newitem, $conversation_url, true);
274
275                 // If the newly created item is the top item then change the parent settings of the thread
276                 if ($newitem AND ($arr["uri"] == $first_id)) {
277                         logger('setting new parent to id '.$newitem);
278                         $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
279                                 intval($message["uid"]), intval($newitem));
280                         if ($new_parents) {
281                                 $parent = $new_parents[0];
282                                 logger('done changing parents to parent '.$newitem);
283                         }
284                 }
285         }
286 }
287 ?>