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