]> git.mxchange.org Git - friendica.git/blob - include/ostatus_conversation.php
only change parents when parents are changed.
[friendica.git] / include / ostatus_conversation.php
1 <?php
2 /*require_once("boot.php");
3 if(@is_null($a)) {
4         $a = new App;
5 }
6
7 if(is_null($db)) {
8         @include(".htconfig.php");
9         require_once("dba.php");
10         $db = new dba($db_host, $db_user, $db_pass, $db_data);
11         unset($db_host, $db_user, $db_pass, $db_data);
12 };*/
13
14 function complete_conversation($itemid, $conversation_url) {
15         global $a;
16
17         require_once('include/html2bbcode.php');
18         require_once('include/items.php');
19
20         //logger('complete_conversation: completing conversation url '.$conversation_url.' for id '.$itemid);
21
22         $messages = q("SELECT `uid`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
23         if (!$messages)
24                 return;
25         $message = $messages[0];
26
27         // Get the parent
28         $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
29                         intval($message["uid"]), intval($message["parent"]));
30         if (!$parents)
31                 return;
32         $parent = $parents[0];
33
34         // Store conversation url if not done before
35         $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
36                 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
37
38         if (!$conversation) {
39                 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`) VALUES (%d, %d, %d, %d, '%s', '%s')",
40                         intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), dbesc(datetime_convert()), dbesc($conversation_url));
41                 logger('complete_conversation: Storing conversation url '.$conversation_url.' for id '.$itemid);
42         }
43
44         $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
45
46         logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
47         $conv_as = fetch_url($conv);
48
49         if ($conv_as) {
50                 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
51                 $conv_as = json_decode($conv_as);
52
53                 $first_id = "";
54                 $items = array_reverse($conv_as->items);
55
56                 foreach ($items as $single_conv) {
57                         //print_r($single_conv);
58
59                         if ($first_id == "") {
60                                 $first_id = $single_conv->id;
61
62                                 $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
63                                         intval($message["uid"]), dbesc($first_id));
64                                 if ($new_parents AND ($itemid != $parent["id"])) {
65                                         $parent = $new_parents[0];
66                                         logger('complete_conversation: adopting new parent '.$parent["id"].' for '.$itemid);
67                                 }
68                         }
69
70                         if (isset($single_conv->context->inReplyTo->id))
71                                 $parent_uri = $single_conv->context->inReplyTo->id;
72                         else
73                                 $parent_uri = $parent["uri"];
74
75                         $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
76                                                 intval($message["uid"]), dbesc($single_conv->id));
77                         if ($message_exists) {
78                                 $existing_message = $message_exists[0];
79                                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d LIMIT 1",
80                                         intval($parent["id"]),
81                                         dbesc($parent["uri"]),
82                                         dbesc($parent_uri),
83                                         intval($existing_message["id"]));
84                                 continue;
85                         }
86
87                         $arr = array();
88                         $arr["uri"] = $single_conv->id;
89                         $arr["uid"] = $message["uid"];
90                         $arr["contact-id"] = $parent["contact-id"]; // To-Do
91                         $arr["parent"] = $parent["id"];
92                         $arr["parent-uri"] = $parent["uri"];
93                         $arr["thr-parent"] = $parent_uri;
94                         $arr["created"] = $single_conv->published;
95                         $arr["edited"] = $single_conv->published;
96                         $arr["owner-name"] = $single_conv->actor->contact->displayName;
97                         //$arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
98                         $arr["owner-link"] = $single_conv->actor->id;
99                         $arr["owner-avatar"] = $single_conv->actor->image->url;
100                         $arr["author-name"] = $single_conv->actor->contact->displayName;
101                         //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
102                         $arr["author-link"] = $single_conv->actor->id;
103                         $arr["author-avatar"] = $single_conv->actor->image->url;
104                         $arr["body"] = html2bbcode($single_conv->content);
105                         $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
106                         if ($arr["app"] == "")
107                                 $arr["app"] = $single_conv->provider->displayName;
108                         $arr["verb"] = $parent["verb"];
109                         $arr["visible"] = $parent["visible"];
110                         $arr["location"] = $single_conv->location->displayName;
111                         $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
112
113                         if ($arr["location"] == "")
114                                 unset($arr["location"]);
115
116                         if ($arr["coord"] == "")
117                                 unset($arr["coord"]);
118
119                         $newitem = item_store($arr);
120
121                         // If the newly created item is the top item then change the parent settings of the thread
122                         if ($newitem AND ($arr["uri"] == $first_id)) {
123                                 logger('complete_conversation: changing parents to parent '.$newitem.' old parent: '.$parent["id"].' new uri: '.$arr["uri"]);
124                                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s' WHERE `parent` = %d",
125                                         intval($newitem),
126                                         dbesc($arr["uri"]),
127                                         intval($parent["id"]));
128                                 logger('complete_conversation: done changing parents to parent '.$newitem);
129                         }
130                         //print_r($arr);
131                 }
132         }
133 }
134 /*
135 $id = 282481;
136 $conversation = "http://identi.ca/conversation/98268580";
137
138 complete_conversation($id, $conversation);
139 */
140 ?>