]> git.mxchange.org Git - friendica.git/blob - include/ostatus_conversation.php
OStatus conversation are now checked every 30 minutes for new answers.
[friendica.git] / include / ostatus_conversation.php
1 <?php
2 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
3
4 function check_conversations() {
5         $last = get_config('system','ostatus_last_poll');
6
7         $poll_interval = intval(get_config('system','ostatus_poll_interval'));
8         if(! $poll_interval)
9                 $poll_interval = OSTATUS_DEFAULT_POLL_INTERVAL;
10
11         if($last) {
12                 $next = $last + ($poll_interval * 60);
13                 if($next > time()) {
14                         logger('complete_conversation: poll intervall not reached');
15                         return;
16                 }
17         }
18
19         logger('complete_conversation: cron_start');
20
21         $start = date("Y-m-d H:i:s", time() - 86400);
22         $conversations = q("SELECT * FROM `term` WHERE `type` = 7 AND `term` > '%s'", 
23                                 dbesc($start));
24         foreach ($conversations AS $conversation) {
25                 $id = $conversation['oid'];
26                 $url = $conversation['url'];
27                 complete_conversation($id, $url);
28         }
29
30         logger('complete_conversation: cron_end');
31
32         set_config('system','ostatus_last_poll', time());
33 }
34
35 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
36         global $a;
37
38         //logger('complete_conversation: completing conversation url '.$conversation_url.' for id '.$itemid);
39
40         $messages = q("SELECT `uid`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
41         if (!$messages)
42                 return;
43         $message = $messages[0];
44
45         // Store conversation url if not done before
46         $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
47                 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
48
49         if (!$conversation) {
50                 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`) VALUES (%d, %d, %d, %d, '%s', '%s')",
51                         intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc(datetime_convert()), dbesc($conversation_url));
52                 logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
53         }
54
55         if ($only_add_conversation)
56                 return;
57
58         // Get the parent
59         $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
60                         intval($message["uid"]), intval($message["parent"]));
61         if (!$parents)
62                 return;
63         $parent = $parents[0];
64
65         require_once('include/html2bbcode.php');
66         require_once('include/items.php');
67
68         $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
69
70         logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
71         $conv_as = fetch_url($conv);
72
73         if ($conv_as) {
74                 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
75                 $conv_as = json_decode($conv_as);
76
77                 $first_id = "";
78                 $items = array_reverse($conv_as->items);
79
80                 foreach ($items as $single_conv) {
81                         if ($first_id == "") {
82                                 $first_id = $single_conv->id;
83
84                                 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
85                                         intval($message["uid"]), dbesc($first_id));
86                                 if ($new_parents) {
87                                         $parent = $new_parents[0];
88                                         logger('complete_conversation: adopting new parent '.$parent["id"].' for '.$itemid);
89                                 } else {
90                                         $parent["id"] = 0;
91                                         $parent["uri"] = $first_id;
92                                 }
93                         }
94
95                         if (isset($single_conv->context->inReplyTo->id))
96                                 $parent_uri = $single_conv->context->inReplyTo->id;
97                         else
98                                 $parent_uri = $parent["uri"];
99
100                         if ($parent["id"] != 0) {
101                                 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
102                                                         intval($message["uid"]), dbesc($single_conv->id));
103                                 if ($message_exists) {
104                                         $existing_message = $message_exists[0];
105                                         $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d LIMIT 1",
106                                                 intval($parent["id"]),
107                                                 dbesc($parent["uri"]),
108                                                 dbesc($parent_uri),
109                                                 intval($existing_message["id"]));
110                                         continue;
111                                 }
112                         }
113
114                         $arr = array();
115                         $arr["uri"] = $single_conv->id;
116                         $arr["plink"] = $single_conv->id;
117                         $arr["uid"] = $message["uid"];
118                         $arr["contact-id"] = $parent["contact-id"]; // To-Do
119                         if ($parent["id"] != 0)
120                                 $arr["parent"] = $parent["id"];
121                         $arr["parent-uri"] = $parent["uri"];
122                         $arr["thr-parent"] = $parent_uri;
123                         $arr["created"] = $single_conv->published;
124                         $arr["edited"] = $single_conv->published;
125                         //$arr["owner-name"] = $single_conv->actor->contact->displayName;
126                         $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
127                         $arr["owner-link"] = $single_conv->actor->id;
128                         $arr["owner-avatar"] = $single_conv->actor->image->url;
129                         //$arr["author-name"] = $single_conv->actor->contact->displayName;
130                         $arr["author-name"] = $single_conv->actor->contact->preferredUsername;
131                         $arr["author-link"] = $single_conv->actor->id;
132                         $arr["author-avatar"] = $single_conv->actor->image->url;
133                         $arr["body"] = html2bbcode($single_conv->content);
134                         $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
135                         if ($arr["app"] == "")
136                                 $arr["app"] = $single_conv->provider->displayName;
137                         $arr["verb"] = $parent["verb"];
138                         $arr["visible"] = $parent["visible"];
139                         $arr["location"] = $single_conv->location->displayName;
140                         $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
141
142                         if ($arr["location"] == "")
143                                 unset($arr["location"]);
144
145                         if ($arr["coord"] == "")
146                                 unset($arr["coord"]);
147
148                         $newitem = item_store($arr);
149
150                         // Add the conversation entry (but don't fetch the whole conversation)
151                         complete_conversation($newitem, $conversation_url, true);
152
153                         // If the newly created item is the top item then change the parent settings of the thread
154                         if ($newitem AND ($arr["uri"] == $first_id)) {
155                                 logger('complete_conversation: setting new parent to id '.$newitem);
156                                 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
157                                         intval($message["uid"]), intval($newitem));
158                                 if ($new_parents) {
159                                         $parent = $new_parents[0];
160                                         logger('complete_conversation: done changing parents to parent '.$newitem);
161                                 }
162
163                                 /*logger('complete_conversation: changing parents to parent '.$newitem.' old parent: '.$parent["id"].' new uri: '.$arr["uri"]);
164                                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `parent` = %d",
165                                         intval($newitem),
166                                         dbesc($arr["uri"]),
167                                         intval($parent["id"]));
168                                 logger('complete_conversation: done changing parents to parent '.$newitem.' '.print_r($r, true));*/
169                         }
170                 }
171         }
172 }
173 ?>