2 function complete_conversation($itemid, $conversation_url) {
5 require_once('include/html2bbcode.php');
6 require_once('include/items.php');
8 //logger('complete_conversation: completing conversation url '.$conversation_url.' for id '.$itemid);
10 $messages = q("SELECT `uid`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
13 $message = $messages[0];
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"]));
20 $parent = $parents[0];
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));
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);
32 $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
34 logger('complete_conversation: fetching conversation url '.$conv.' for '.$itemid);
35 $conv_as = fetch_url($conv);
38 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
39 $conv_as = json_decode($conv_as);
42 $items = array_reverse($conv_as->items);
44 foreach ($items as $single_conv) {
45 if ($first_id == "") {
46 $first_id = $single_conv->id;
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));
51 $parent = $new_parents[0];
52 logger('complete_conversation: adopting new parent '.$parent["id"].' for '.$itemid);
55 $parent["uri"] = $first_id;
59 if (isset($single_conv->context->inReplyTo->id))
60 $parent_uri = $single_conv->context->inReplyTo->id;
62 $parent_uri = $parent["uri"];
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"]),
73 intval($existing_message["id"]));
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);
106 if ($arr["location"] == "")
107 unset($arr["location"]);
109 if ($arr["coord"] == "")
110 unset($arr["coord"]);
112 $newitem = item_store($arr);
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));
120 $parent = $new_parents[0];
121 logger('complete_conversation: done changing parents to parent '.$newitem);
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",
128 intval($parent["id"]));
129 logger('complete_conversation: done changing parents to parent '.$newitem.' '.print_r($r, true));*/