]> git.mxchange.org Git - friendica.git/blob - include/ostatus_conversation.php
Merge remote-tracking branch 'upstream/develop' into 1501-better-ostatus
[friendica.git] / include / ostatus_conversation.php
1 <?php
2 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
3 define('OSTATUS_DEFAULT_POLL_TIMEFRAME', 1440); // given in minutes
4
5 function check_conversations() {
6         $last = get_config('system','ostatus_last_poll');
7
8         $poll_interval = intval(get_config('system','ostatus_poll_interval'));
9         if(! $poll_interval)
10                 $poll_interval = OSTATUS_DEFAULT_POLL_INTERVAL;
11
12         // Don't poll if the interval is set negative
13         if ($poll_interval < 0)
14                 return;
15
16         $poll_timeframe = intval(get_config('system','ostatus_poll_timeframe'));
17         if(! $poll_timeframe)
18                 $poll_timeframe = OSTATUS_DEFAULT_POLL_TIMEFRAME;
19
20         if($last) {
21                 $next = $last + ($poll_interval * 60);
22                 if($next > time()) {
23                         logger('poll interval not reached');
24                         return;
25                 }
26         }
27
28         logger('cron_start');
29
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'",
32                                 dbesc($start));
33         foreach ($conversations AS $conversation) {
34                 $id = $conversation['oid'];
35                 $url = $conversation['url'];
36                 complete_conversation($id, $url);
37         }
38
39         logger(' cron_end');
40
41         set_config('system','ostatus_last_poll', time());
42 }
43
44 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
45         global $a;
46
47         if (intval(get_config('system','ostatus_poll_interval')) == -2)
48                 return;
49
50         if ($a->last_ostatus_conversation_url == $conversation_url)
51                 return;
52
53         $a->last_ostatus_conversation_url = $conversation_url;
54
55         $messages = q("SELECT `uid`, `parent`, `created` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
56         if (!$messages)
57                 return;
58         $message = $messages[0];
59
60         // Store conversation url if not done before
61         $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
62                 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
63
64         if (!$conversation) {
65                 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`) VALUES (%d, %d, %d, %d, '%s', '%s')",
66                         intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc($message["created"]), dbesc($conversation_url));
67                 logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
68         }
69
70         if ($only_add_conversation)
71                 return;
72
73         // Get the parent
74         $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
75                         intval($message["uid"]), intval($message["parent"]));
76         if (!$parents)
77                 return;
78         $parent = $parents[0];
79
80         require_once('include/html2bbcode.php');
81         require_once('include/items.php');
82
83         $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
84         $pageno = 1;
85         $items = array();
86
87         logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
88
89         do {
90                 $conv_as = fetch_url($conv."?page=".$pageno);
91                 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
92                 $conv_as = json_decode($conv_as);
93
94                 if (@is_array($conv_as->items))
95                         $items = array_merge($items, $conv_as->items);
96                 else
97                         break;
98
99                 $pageno++;
100
101         } while (true);
102
103         if (!sizeof($items))
104                 return;
105
106         $items = array_reverse($items);
107
108         foreach ($items as $single_conv) {
109                 // status.net changed the format of the activity streams. This is a quick fix.
110                 if (@is_string($single_conv->object->id))
111                         $single_conv->id = $single_conv->object->id;
112
113                 if (@!$single_conv->id AND $single_conv->provider->url AND $single_conv->statusnet_notice_info->local_id)
114                         $single_conv->id = $single_conv->provider->url."notice/".$single_conv->statusnet_notice_info->local_id;
115
116                 if (@!$single_conv->id)
117                         continue;
118
119                 if ($first_id == "") {
120                         $first_id = $single_conv->id;
121
122                         $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
123                                 intval($message["uid"]), dbesc($first_id));
124                         if ($new_parents) {
125                                 $parent = $new_parents[0];
126                                 logger('adopting new parent '.$parent["id"].' for '.$itemid);
127                         } else {
128                                 $parent["id"] = 0;
129                                 $parent["uri"] = $first_id;
130                         }
131                 }
132
133                 if (isset($single_conv->context->inReplyTo->id))
134                         $parent_uri = $single_conv->context->inReplyTo->id;
135                 else
136                         $parent_uri = $parent["uri"];
137
138                 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
139                                                         intval($message["uid"]), dbesc($single_conv->id));
140                 if ($message_exists) {
141                         if ($parent["id"] != 0) {
142                                 $existing_message = $message_exists[0];
143
144                                 // This is partly bad, since the entry in the thread table isn't updated
145                                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
146                                         intval($parent["id"]),
147                                         dbesc($parent["uri"]),
148                                         dbesc($parent_uri),
149                                         intval($existing_message["id"]));
150                         }
151                         continue;
152                 }
153
154                 $contact = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
155                                 $message["uid"], normalise_link($single_conv->actor->id), NETWORK_STATUSNET);
156
157                 if (count($contact)) {
158                         logger("Found contact for url ".$single_conv->actor->id, LOGGER_DEBUG);
159                         $contact_id = $contact[0]["id"];
160                 } else {
161                         logger("No contact found for url ".$single_conv->actor->id, LOGGER_DEBUG);
162                         $contact_id = $parent["contact-id"];
163                 }
164
165                 $arr = array();
166                 $arr["network"] = NETWORK_OSTATUS;
167                 $arr["uri"] = $single_conv->id;
168                 $arr["plink"] = $single_conv->id;
169                 $arr["uid"] = $message["uid"];
170                 $arr["contact-id"] = $contact_id;
171                 if ($parent["id"] != 0)
172                         $arr["parent"] = $parent["id"];
173                 $arr["parent-uri"] = $parent["uri"];
174                 $arr["thr-parent"] = $parent_uri;
175                 $arr["created"] = $single_conv->published;
176                 $arr["edited"] = $single_conv->published;
177                 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
178                 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
179                 if ($arr["owner-name"] == '')
180                         $arr["owner-name"] = $single_conv->actor->portablecontacts_net->preferredUsername;
181                 if ($arr["owner-name"] == '')
182                         $arr["owner-name"] =  $single_conv->actor->displayName;
183
184                 $arr["owner-link"] = $single_conv->actor->id;
185                 $arr["owner-avatar"] = $single_conv->actor->image->url;
186                 //$arr["author-name"] = $single_conv->actor->contact->displayName;
187                 //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
188                 $arr["author-name"] = $arr["owner-name"];
189                 $arr["author-link"] = $single_conv->actor->id;
190                 $arr["author-avatar"] = $single_conv->actor->image->url;
191                 $arr["body"] = html2bbcode($single_conv->content);
192                 $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
193                 if ($arr["app"] == "")
194                         $arr["app"] = $single_conv->provider->displayName;
195                 $arr["verb"] = $parent["verb"];
196                 $arr["visible"] = $parent["visible"];
197                 $arr["location"] = $single_conv->location->displayName;
198                 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
199
200                 if ($arr["location"] == "")
201                         unset($arr["location"]);
202
203                 if ($arr["coord"] == "")
204                         unset($arr["coord"]);
205
206                 $newitem = item_store($arr);
207
208                 // Add the conversation entry (but don't fetch the whole conversation)
209                 complete_conversation($newitem, $conversation_url, true);
210
211                 // If the newly created item is the top item then change the parent settings of the thread
212                 if ($newitem AND ($arr["uri"] == $first_id)) {
213                         logger('setting new parent to id '.$newitem);
214                         $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
215                                 intval($message["uid"]), intval($newitem));
216                         if ($new_parents) {
217                                 $parent = $new_parents[0];
218                                 logger('done changing parents to parent '.$newitem);
219                         }
220                 }
221         }
222 }
223 ?>