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