]> git.mxchange.org Git - friendica.git/blob - include/ostatus_conversation.php
New function to complete threads from ostatus postings
[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         $messages = q("SELECT `uid`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid));
21         if (!$messages)
22                 return;
23         $message = $messages[0];
24
25         // Get the parent
26         $parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1",
27                         intval($message["uid"]), intval($message["parent"]));
28         if (!$parents)
29                 return;
30         $parent = $parents[0];
31
32         // Store conversation url if not done before
33         $conversation = q("SELECT `url` FROM `term` WHERE `uid` = %d AND `oid` = %d AND `otype` = %d AND `type` = %d",
34                 intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION));
35
36         if (!$conversation)
37                 $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`) VALUES (%d, %d, %d, %d, '%s', '%s')",
38                         intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CONVERSATION), '', dbesc($conversation_url));
39
40         $conv = str_replace("/conversation/", "/api/statusnet/conversation/", $conversation_url).".as";
41
42         $conv_as = fetch_url($conv);
43
44         if ($conv_as) {
45                 $conv_as = str_replace(',"statusnet:notice_info":', ',"statusnet_notice_info":', $conv_as);
46                 $conv_as = json_decode($conv_as);
47
48                 $items = array_reverse($conv_as->items);
49
50                 foreach ($items as $single_conv) {
51                         //print_r($single_conv);
52
53                         if (isset($single_conv->context->inReplyTo->id))
54                                 $parent_uri = $single_conv->context->inReplyTo->id;
55                         else
56                                 $parent_uri = $parent["uri"];
57
58                         $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
59                                                 intval($message["uid"]), dbesc($single_conv->id));
60                         if ($message_exists) {
61                                 $existing_message = $message_exists[0];
62                                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d LIMIT 1",
63                                         intval($parent["id"]),
64                                         dbesc($parent["uri"]),
65                                         dbesc($parent_uri),
66                                         intval($existing_message["id"]));
67                                 continue;
68                         }
69
70                         $arr = array();
71                         $arr["uri"] = $single_conv->id;
72                         $arr["uid"] = $message["uid"];
73                         $arr["contact-id"] = $parent["contact-id"]; // To-Do
74                         $arr["parent"] = $parent["id"];
75                         $arr["parent-uri"] = $parent["uri"];
76                         $arr["thr-parent"] = $parent_uri;
77                         $arr["created"] = $single_conv->published;
78                         $arr["edited"] = $single_conv->published;
79                         $arr["owner-name"] = $single_conv->actor->contact->displayName;
80                         //$arr["owner-name"] = $single_conv->actor->contact->preferredUsername;
81                         $arr["owner-link"] = $single_conv->actor->id;
82                         $arr["owner-avatar"] = $single_conv->actor->image->url;
83                         $arr["author-name"] = $single_conv->actor->contact->displayName;
84                         //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
85                         $arr["author-link"] = $single_conv->actor->id;
86                         $arr["author-avatar"] = $single_conv->actor->image->url;
87                         $arr["body"] = html2bbcode($single_conv->content);
88                         $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
89                         if ($arr["app"] == "")
90                                 $arr["app"] = $single_conv->provider->displayName;
91                         $arr["verb"] = $parent["verb"];
92                         $arr["visible"] = $parent["visible"];
93                         $arr["location"] = $single_conv->location->displayName;
94                         $arr["coord"] = trim($single_conv->location->lat." ".$single_conv->location->lon);
95
96                         if ($arr["location"] == "")
97                                 unset($arr["location"]);
98
99                         if ($arr["coord"] == "")
100                                 unset($arr["coord"]);
101
102                         item_store($arr);
103                         //print_r($arr);
104                 }
105         }
106 }
107
108 $id = 282481;
109 $conversation = "http://identi.ca/conversation/98268580";
110
111 complete_conversation($id, $conversation);
112 ?>