2 /*require_once("boot.php");
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);
14 function complete_conversation($itemid, $conversation_url) {
17 require_once('include/html2bbcode.php');
18 require_once('include/items.php');
20 logger('complete_conversation: completing conversation url '.$conversation_url.' for id '.$itemid);
22 $messages = q("SELECT `uid`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
25 $message = $messages[0];
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"]));
32 $parent = $parents[0];
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));
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);
44 $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
46 logger('complete_conversation: fetching conversation url '.$conversation_url.' for '.$itemid);
47 $conv_as = fetch_url($conv);
50 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
51 $conv_as = json_decode($conv_as);
54 $items = array_reverse($conv_as->items);
56 foreach ($items as $single_conv) {
57 //print_r($single_conv);
59 if ($first_id == "") {
60 $first_id = $single_conv->id;
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));
65 $parent = $new_parents[0];
67 logger('complete_conversation: adopting new parent '.$parent["id"].' for '.$itemid);
70 if (isset($single_conv->context->inReplyTo->id))
71 $parent_uri = $single_conv->context->inReplyTo->id;
73 $parent_uri = $parent["uri"];
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"]),
83 intval($existing_message["id"]));
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);
113 if ($arr["location"] == "")
114 unset($arr["location"]);
116 if ($arr["coord"] == "")
117 unset($arr["coord"]);
119 $newitem = item_store($arr);
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",
127 intval($parent["id"]));
128 logger('complete_conversation: done changing parents to parent '.$newitem);
136 $conversation = "http://identi.ca/conversation/98268580";
138 complete_conversation($id, $conversation);