]> git.mxchange.org Git - friendica.git/blob - include/ostatus_conversation.php
added translation instructions to the help pages
[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('complete_conversation: poll interval not reached');
24                         return;
25                 }
26         }
27
28         logger('complete_conversation: 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('complete_conversation: 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         //logger('complete_conversation: completing conversation url '.$conversation_url.' for id '.$itemid);
56
57         $messages = q("SELECT `uid`, `parent`, `created` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
58         if (!$messages)
59                 return;
60         $message = $messages[0];
61
62         // Store conversation url if not done before
63         $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
64                 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
65
66         if (!$conversation) {
67                 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`) VALUES (%d, %d, %d, %d, '%s', '%s')",
68                         intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc($message["created"]), dbesc($conversation_url));
69                 logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
70         }
71
72         if ($only_add_conversation)
73                 return;
74
75         // Get the parent
76         $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
77                         intval($message["uid"]), intval($message["parent"]));
78         if (!$parents)
79                 return;
80         $parent = $parents[0];
81
82         require_once('include/html2bbcode.php');
83         require_once('include/items.php');
84
85         $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
86         $pageno = 1;
87         $items = array();
88
89         logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
90
91         do {
92                 $conv_as = fetch_url($conv."?page=".$pageno);
93                 //$conv_as = fetch_url($conv."?page=".$pageno, false, 0, 10);
94                 //$conv_as = file_get_contents($conv."?page=".$pageno);
95                 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
96                 $conv_as = json_decode($conv_as);
97
98                 if (@is_array($conv_as->items))
99                         $items = array_merge($items, $conv_as->items);
100                 else
101                         break;
102
103                 $pageno++;
104
105         } while (true);
106
107         if (!sizeof($items))
108                 return;
109
110         $items = array_reverse($items);
111
112         foreach ($items as $single_conv) {
113                 // identi.ca just changed the format of the activity streams. This is a quick fix.
114                 if (@is_string($single_conv->object->id))
115                         $single_conv->id = $single_conv->object->id;
116
117                 if (@!$single_conv->id AND $single_conv->provider->url AND $single_conv->statusnet_notice_info->local_id)
118                         $single_conv->id = $single_conv->provider->url."notice/".$single_conv->statusnet_notice_info->local_id;
119
120                 if (@!$single_conv->id)
121                         continue;
122
123                 if ($first_id == "") {
124                         $first_id = $single_conv->id;
125
126                         $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
127                                 intval($message["uid"]), dbesc($first_id));
128                         if ($new_parents) {
129                                 $parent = $new_parents[0];
130                                 logger('complete_conversation: adopting new parent '.$parent["id"].' for '.$itemid);
131                         } else {
132                                 $parent["id"] = 0;
133                                 $parent["uri"] = $first_id;
134                         }
135                 }
136
137                 if (isset($single_conv->context->inReplyTo->id))
138                         $parent_uri = $single_conv->context->inReplyTo->id;
139                 else
140                         $parent_uri = $parent["uri"];
141
142                 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
143                                                         intval($message["uid"]), dbesc($single_conv->id));
144                 if ($message_exists) {
145                         if ($parent["id"] != 0) {
146                                 $existing_message = $message_exists[0];
147                                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
148                                         intval($parent["id"]),
149                                         dbesc($parent["uri"]),
150                                         dbesc($parent_uri),
151                                         intval($existing_message["id"]));
152                         }
153                         continue;
154                 }
155
156                 $arr = array();
157                 $arr["network"] = NETWORK_OSTATUS;
158                 $arr["uri"] = $single_conv->id;
159                 $arr["plink"] = $single_conv->id;
160                 $arr["uid"] = $message["uid"];
161                 $arr["contact-id"] = $parent["contact-id"]; // To-Do
162                 if ($parent["id"] != 0)
163                         $arr["parent"] = $parent["id"];
164                 $arr["parent-uri"] = $parent["uri"];
165                 $arr["thr-parent"] = $parent_uri;
166                 $arr["created"] = $single_conv->published;
167                 $arr["edited"] = $single_conv->published;
168                 //$arr["owner-name"] = $single_conv->actor->contact->displayName;
169                 $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
170                 if ($arr["owner-name"] == '')
171                         $arr["owner-name"] = $single_conv->actor->portablecontacts_net->preferredUsername;
172                 if ($arr["owner-name"] == '')
173                         $arr["owner-name"] =  $single_conv->actor->displayName;
174
175                 $arr["owner-link"] = $single_conv->actor->id;
176                 $arr["owner-avatar"] = $single_conv->actor->image->url;
177                 //$arr["author-name"] = $single_conv->actor->contact->displayName;
178                 //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
179                 $arr["author-name"] = $arr["owner-name"];
180                 $arr["author-link"] = $single_conv->actor->id;
181                 $arr["author-avatar"] = $single_conv->actor->image->url;
182                 $arr["body"] = html2bbcode($single_conv->content);
183                 $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
184                 if ($arr["app"] == "")
185                         $arr["app"] = $single_conv->provider->displayName;
186                 $arr["verb"] = $parent["verb"];
187                 $arr["visible"] = $parent["visible"];
188                 $arr["location"] = $single_conv->location->displayName;
189                 $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
190
191                 if ($arr["location"] == "")
192                         unset($arr["location"]);
193
194                 if ($arr["coord"] == "")
195                         unset($arr["coord"]);
196
197                 $newitem = item_store($arr);
198
199                 // Add the conversation entry (but don't fetch the whole conversation)
200                 complete_conversation($newitem, $conversation_url, true);
201
202                 // If the newly created item is the top item then change the parent settings of the thread
203                 if ($newitem AND ($arr["uri"] == $first_id)) {
204                         logger('complete_conversation: setting new parent to id '.$newitem);
205                         $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
206                                 intval($message["uid"]), intval($newitem));
207                         if ($new_parents) {
208                                 $parent = $new_parents[0];
209                                 logger('complete_conversation: done changing parents to parent '.$newitem);
210                         }
211
212                         /*logger('complete_conversation: changing parents to parent '.$newitem.' old parent: '.$parent["id"].' new uri: '.$arr["uri"]);
213                         $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `parent` = %d",
214                                 intval($newitem),
215                                 dbesc($arr["uri"]),
216                                 intval($parent["id"]));
217                         logger('complete_conversation: done changing parents to parent '.$newitem.' '.print_r($r, true));*/
218                 }
219         }
220 }
221 ?>