]> git.mxchange.org Git - friendica.git/blob - include/ostatus_conversation.php
make sure conv_as->items is an array or don't proceed
[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
79                 if (!is_array($conv_as->items))
80                     return;
81                 $items = array_reverse($conv_as->items);
82
83                 foreach ($items as $single_conv) {
84                         if ($first_id == "") {
85                                 $first_id = $single_conv->id;
86
87                                 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
88                                         intval($message["uid"]), dbesc($first_id));
89                                 if ($new_parents) {
90                                         $parent = $new_parents[0];
91                                         logger('complete_conversation: adopting new parent '.$parent["id"].' for '.$itemid);
92                                 } else {
93                                         $parent["id"] = 0;
94                                         $parent["uri"] = $first_id;
95                                 }
96                         }
97
98                         if (isset($single_conv->context->inReplyTo->id))
99                                 $parent_uri = $single_conv->context->inReplyTo->id;
100                         else
101                                 $parent_uri = $parent["uri"];
102
103                         if ($parent["id"] != 0) {
104                                 $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
105                                                         intval($message["uid"]), dbesc($single_conv->id));
106                                 if ($message_exists) {
107                                         $existing_message = $message_exists[0];
108                                         $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d LIMIT 1",
109                                                 intval($parent["id"]),
110                                                 dbesc($parent["uri"]),
111                                                 dbesc($parent_uri),
112                                                 intval($existing_message["id"]));
113                                         continue;
114                                 }
115                         }
116
117                         $arr = array();
118                         $arr["uri"] = $single_conv->id;
119                         $arr["plink"] = $single_conv->id;
120                         $arr["uid"] = $message["uid"];
121                         $arr["contact-id"] = $parent["contact-id"]; // To-Do
122                         if ($parent["id"] != 0)
123                                 $arr["parent"] = $parent["id"];
124                         $arr["parent-uri"] = $parent["uri"];
125                         $arr["thr-parent"] = $parent_uri;
126                         $arr["created"] = $single_conv->published;
127                         $arr["edited"] = $single_conv->published;
128                         //$arr["owner-name"] = $single_conv->actor->contact->displayName;
129                         $arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
130                         $arr["owner-link"] = $single_conv->actor->id;
131                         $arr["owner-avatar"] = $single_conv->actor->image->url;
132                         //$arr["author-name"] = $single_conv->actor->contact->displayName;
133                         $arr["author-name"] = $single_conv->actor->contact->preferredUsername;
134                         $arr["author-link"] = $single_conv->actor->id;
135                         $arr["author-avatar"] = $single_conv->actor->image->url;
136                         $arr["body"] = html2bbcode($single_conv->content);
137                         $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
138                         if ($arr["app"] == "")
139                                 $arr["app"] = $single_conv->provider->displayName;
140                         $arr["verb"] = $parent["verb"];
141                         $arr["visible"] = $parent["visible"];
142                         $arr["location"] = $single_conv->location->displayName;
143                         $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
144
145                         if ($arr["location"] == "")
146                                 unset($arr["location"]);
147
148                         if ($arr["coord"] == "")
149                                 unset($arr["coord"]);
150
151                         $newitem = item_store($arr);
152
153                         // Add the conversation entry (but don't fetch the whole conversation)
154                         complete_conversation($newitem, $conversation_url, true);
155
156                         // If the newly created item is the top item then change the parent settings of the thread
157                         if ($newitem AND ($arr["uri"] == $first_id)) {
158                                 logger('complete_conversation: setting new parent to id '.$newitem);
159                                 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
160                                         intval($message["uid"]), intval($newitem));
161                                 if ($new_parents) {
162                                         $parent = $new_parents[0];
163                                         logger('complete_conversation: done changing parents to parent '.$newitem);
164                                 }
165
166                                 /*logger('complete_conversation: changing parents to parent '.$newitem.' old parent: '.$parent["id"].' new uri: '.$arr["uri"]);
167                                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `parent` = %d",
168                                         intval($newitem),
169                                         dbesc($arr["uri"]),
170                                         intval($parent["id"]));
171                                 logger('complete_conversation: done changing parents to parent '.$newitem.' '.print_r($r, true));*/
172                         }
173                 }
174         }
175 }
176 ?>