]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #1617 from annando/1505-diaspora-objecttype
authorfabrixxm <fabrix.xm@gmail.com>
Fri, 29 May 2015 06:40:58 +0000 (08:40 +0200)
committerfabrixxm <fabrix.xm@gmail.com>
Fri, 29 May 2015 06:40:58 +0000 (08:40 +0200)
OStatus and Diaspora improvements

include/diaspora.php
include/items.php
include/ostatus_conversation.php

index 04588dffe6bee58661a06e19f57bf79210cbcaf7..b70cffdc39efebd8cf0073887301a8891653a387 100755 (executable)
@@ -785,12 +785,21 @@ function diaspora_is_redmatrix($url) {
 }
 
 function diaspora_plink($addr, $guid) {
-       $r = q("SELECT `url`, `nick` FROM `fcontact` WHERE `addr`='%s' LIMIT 1", $addr);
+       $r = q("SELECT `url`, `nick`, `network` FROM `fcontact` WHERE `addr`='%s' LIMIT 1", $addr);
 
        // Fallback
        if (!$r)
                return 'https://'.substr($addr,strpos($addr,'@')+1).'/posts/'.$guid;
 
+       // Friendica contacts are often detected as Diaspora contacts in the "fcontact" table
+       // So we try another way as well.
+       $s = q("SELECT `network` FROM `gcontact` WHERE `nurl`='%s' LIMIT 1", dbesc(normalise_link($r[0]["url"])));
+       if ($s)
+               $r[0]["network"] = $s[0]["network"];
+
+       if ($r[0]["network"] == NETWORK_DFRN)
+               return(str_replace("/profile/".$r[0]["nick"]."/", "/display/".$guid, $r[0]["url"]."/"));
+
        if (diaspora_is_redmatrix($r[0]["url"]))
                return $r[0]["url"]."/?f=&mid=".$guid;
 
@@ -833,12 +842,19 @@ function diaspora_post($importer,$xml,$msg) {
 
        $body = diaspora2bb($xml->raw_message);
 
-       // Add OEmbed and other information to the body
-       if (!diaspora_is_redmatrix($contact['url']))
-               $body = add_page_info_to_body($body, false, true);
-
        $datarray = array();
 
+       $datarray["object"] = json_encode($xml);
+
+       if($xml->photo->remote_photo_path AND $xml->photo->remote_photo_name)
+               $datarray["object-type"] = ACTIVITY_OBJ_PHOTO;
+       else {
+               $datarray['object-type'] = ACTIVITY_OBJ_NOTE;
+               // Add OEmbed and other information to the body
+               if (!diaspora_is_redmatrix($contact['url']))
+                       $body = add_page_info_to_body($body, false, true);
+       }
+
        $str_tags = '';
 
        $cnt = preg_match_all('/@\[url=(.*?)\[\/url\]/ism',$body,$matches,PREG_SET_ORDER);
@@ -872,7 +888,10 @@ function diaspora_post($importer,$xml,$msg) {
        $datarray['author-avatar'] = $contact['thumb'];
        $datarray['body'] = $body;
        $datarray['tag'] = $str_tags;
-       $datarray['app']  = 'Diaspora';
+       if ($xml->provider_display_name)
+                $datarray["app"] = unxmlify($xml->provider_display_name);
+       else
+               $datarray['app']  = 'Diaspora';
 
        // if empty content it might be a photo that hasn't arrived yet. If a photo arrives, we'll make it visible.
 
@@ -921,6 +940,8 @@ function diaspora_store_by_guid($guid, $server, $uid = 0) {
        $author = $item["author"];
        $guid = $item["guid"];
        $private = $item["private"];
+       $object = $item["object"];
+       $objecttype = $item["object-type"];
 
        $message_id = $author.':'.$guid;
        $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1",
@@ -954,6 +975,8 @@ function diaspora_store_by_guid($guid, $server, $uid = 0) {
        $datarray['tag'] = $str_tags;
        $datarray['app']  = $app;
        $datarray['visible'] = ((strlen($body)) ? 1 : 0);
+       $datarray['object'] = $object;
+       $datarray['object-type'] = $objecttype;
 
        if ($datarray['contact-id'] == 0)
                return false;
@@ -1001,11 +1024,14 @@ function diaspora_fetch_message($guid, $server, $level = 0) {
                $item["guid"] = unxmlify($source_xml->post->status_message->guid);
 
        $item["private"] = (unxmlify($source_xml->post->status_message->public) == 'false');
+       $item["object"] = json_encode($source_xml->post);
 
        if(strlen($source_xml->post->asphoto->objectId) && ($source_xml->post->asphoto->objectId != 0) && ($source_xml->post->asphoto->image_url)) {
+               $item["object-type"] = ACTIVITY_OBJ_PHOTO;
                $body = '[url=' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '][img]' . notags(unxmlify($source_xml->post->asphoto->objectId)) . '[/img][/url]' . "\n";
                $body = scale_external_images($body,false);
        } elseif($source_xml->post->asphoto->image_url) {
+               $item["object-type"] = ACTIVITY_OBJ_PHOTO;
                $body = '[img]' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '[/img]' . "\n";
                $body = scale_external_images($body);
        } elseif($source_xml->post->status_message) {
@@ -1015,18 +1041,25 @@ function diaspora_fetch_message($guid, $server, $level = 0) {
                if($source_xml->post->status_message->photo->remote_photo_path AND
                        $source_xml->post->status_message->photo->remote_photo_name) {
 
+                       $item["object-type"] = ACTIVITY_OBJ_PHOTO;
+
                        $remote_photo_path = notags(unxmlify($source_xml->post->status_message->photo->remote_photo_path));
                        $remote_photo_name = notags(unxmlify($source_xml->post->status_message->photo->remote_photo_name));
 
                        $body = '[img]'.$remote_photo_path.$remote_photo_name.'[/img]'."\n".$body;
 
                        logger('embedded picture link found: '.$body, LOGGER_DEBUG);
-               }
+               } else
+                       $item["object-type"] = ACTIVITY_OBJ_NOTE;
 
                $body = scale_external_images($body);
 
                // Add OEmbed and other information to the body
-               $body = add_page_info_to_body($body, false, true);
+               // To-Do: It could be a repeated redmatrix item
+               // Then we shouldn't add further data to it
+               if ($item["object-type"] == ACTIVITY_OBJ_NOTE)
+                       $body = add_page_info_to_body($body, false, true);
+
        } elseif($source_xml->post->reshare) {
                // Reshare of a reshare
                return diaspora_fetch_message($source_xml->post->reshare->root_guid, $server, ++$level);
@@ -1081,16 +1114,17 @@ function diaspora_reshare($importer,$xml,$msg) {
 
        $orig_author = notags(unxmlify($xml->root_diaspora_id));
        $orig_guid = notags(unxmlify($xml->root_guid));
+       $orig_url = $a->get_baseurl()."/display/".$orig_guid;
 
        $create_original_post = false;
 
        // Do we already have this item?
-       $r = q("SELECT `body`, `tag`, `app`, `created`, `author-link`, `plink` FROM `item` WHERE `guid` = '%s' AND `visible` AND NOT `deleted` AND `body` != '' LIMIT 1",
+       $r = q("SELECT `body`, `tag`, `app`, `created`, `plink`, `object`, `object-type`, `uri` FROM `item` WHERE `guid` = '%s' AND `visible` AND NOT `deleted` AND `body` != '' LIMIT 1",
                dbesc($orig_guid),
                dbesc(NETWORK_DIASPORA)
        );
        if(count($r)) {
-               logger('reshared message '.$orig_guid." reshared by ".$guid.' already exists on system: '.$orig_url);
+               logger('reshared message '.$orig_guid." reshared by ".$guid.' already exists on system.');
 
                // Maybe it is already a reshared item?
                // Then refetch the content, since there can be many side effects with reshared posts from other networks or reshares from reshares
@@ -1102,9 +1136,10 @@ function diaspora_reshare($importer,$xml,$msg) {
                        $str_tags = $r[0]["tag"];
                        $app = $r[0]["app"];
                        $orig_created = $r[0]["created"];
-                       $orig_author = $r[0]["author-link"];
-                       $create_original_post = ($body != "");
-                       $orig_url = $a->get_baseurl()."/display/".$orig_guid;
+                       $orig_plink = $r[0]["plink"];
+                       $orig_uri = $r[0]["uri"];
+                       $object = $r[0]["object"];
+                       $objecttype = $r[0]["object-type"];
                }
        }
 
@@ -1113,8 +1148,6 @@ function diaspora_reshare($importer,$xml,$msg) {
                $str_tags = "";
                $app = "";
 
-               $orig_url = 'https://'.substr($orig_author,strpos($orig_author,'@')+1).'/posts/'.$orig_guid;
-
                $server = 'https://'.substr($orig_author,strpos($orig_author,'@')+1);
                logger('1st try: reshared message '.$orig_guid." reshared by ".$guid.' will be fetched from original server: '.$server);
                $item = diaspora_fetch_message($orig_guid, $server);
@@ -1142,11 +1175,16 @@ function diaspora_reshare($importer,$xml,$msg) {
                        $orig_created = $item["created"];
                        $orig_author = $item["author"];
                        $orig_guid = $item["guid"];
+                       $orig_plink = diaspora_plink($orig_author, $orig_guid);
+                       $orig_uri = $orig_author.':'.$orig_guid;
                        $create_original_post = ($body != "");
-                       $orig_url = $a->get_baseurl()."/display/".$orig_guid;
+                       $object = $item["object"];
+                       $objecttype = $item["object-type"];
                }
        }
 
+       $plink = diaspora_plink($diaspora_handle, $guid);
+
        $person = find_diaspora_person_by_handle($orig_author);
 
        $created = unxmlify($xml->created_at);
@@ -1154,8 +1192,6 @@ function diaspora_reshare($importer,$xml,$msg) {
 
        $datarray = array();
 
-       $plink = diaspora_plink($diaspora_handle, $guid);
-
        $datarray['uid'] = $importer['uid'];
        $datarray['contact-id'] = $contact['id'];
        $datarray['wall'] = 0;
@@ -1184,6 +1220,9 @@ function diaspora_reshare($importer,$xml,$msg) {
                $datarray['body'] = $body;
        }
 
+       $datarray["object"] = json_encode($xml);
+       $datarray['object-type'] = $objecttype;
+
        $datarray['tag'] = $str_tags;
        $datarray['app']  = $app;
 
@@ -1199,9 +1238,10 @@ function diaspora_reshare($importer,$xml,$msg) {
                $datarray2['uid'] = 0;
                $datarray2['contact-id'] = get_contact($person['url'], 0);
                $datarray2['guid'] = $orig_guid;
-               $datarray2['uri'] = $datarray2['parent-uri'] = $orig_author.':'.$orig_guid;
+               $datarray2['uri'] = $datarray2['parent-uri'] = $orig_uri;
                $datarray2['changed'] = $datarray2['created'] = $datarray2['edited'] = $datarray2['commented'] = $datarray2['received'] = datetime_convert('UTC','UTC',$orig_created);
-               $datarray2['plink'] = diaspora_plink($orig_author, $orig_guid);
+               $datarray2['parent'] = 0;
+               $datarray2['plink'] = $orig_plink;
 
                $datarray2['author-name'] = $person['name'];
                $datarray2['author-link'] = $person['url'];
@@ -1210,6 +1250,7 @@ function diaspora_reshare($importer,$xml,$msg) {
                $datarray2['owner-link'] = $datarray2['author-link'];
                $datarray2['owner-avatar'] = $datarray2['author-avatar'];
                $datarray2['body'] = $body;
+               $datarray2["object"] = $object;
 
                DiasporaFetchGuid($datarray2);
                $message_id = item_store($datarray2);
@@ -1295,6 +1336,8 @@ function diaspora_asphoto($importer,$xml,$msg) {
        $datarray['author-link'] = $contact['url'];
        $datarray['author-avatar'] = $contact['thumb'];
        $datarray['body'] = $body;
+       $datarray["object"] = json_encode($xml);
+       $datarray['object-type'] = ACTIVITY_OBJ_PHOTO;
 
        $datarray['app']  = 'Diaspora/Cubbi.es';
 
@@ -1464,6 +1507,8 @@ function diaspora_comment($importer,$xml,$msg) {
        $datarray['author-link'] = $person['url'];
        $datarray['author-avatar'] = ((x($person,'thumb')) ? $person['thumb'] : $person['photo']);
        $datarray['body'] = $body;
+       $datarray["object"] = json_encode($xml);
+       $datarray["object-type"] = ACTIVITY_OBJ_COMMENT;
 
        // We can't be certain what the original app is if the message is relayed.
        if(($parent_item['origin']) && (! $parent_author_signature))
@@ -1790,7 +1835,7 @@ function diaspora_message($importer,$xml,$msg) {
                dbesc($person['name']),
                dbesc($person['photo']),
                dbesc($person['url']),
-               intval($contact['id']),  
+               intval($contact['id']),
                dbesc($conversation['subject']),
                dbesc($body),
                0,
@@ -2059,7 +2104,7 @@ function diaspora_like($importer,$xml,$msg) {
 
        $activity = ACTIVITY_LIKE;
        $post_type = (($parent_item['resource-id']) ? t('photo') : t('status'));
-       $objtype = (($parent_item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE ); 
+       $objtype = (($parent_item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
        $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $parent_item['id'] . '" />' . "\n") ;
        $body = $parent_item['body'];
 
@@ -2878,7 +2923,7 @@ function diaspora_send_retraction($item,$owner,$contact,$public_batch = false) {
                $target_type = (($item['verb'] === ACTIVITY_LIKE) ? 'Like' : 'Comment');
        }
        else {
-               
+
                $tpl = get_markup_template('diaspora_signed_retract.tpl');
                $target_type = 'StatusMessage';
        }
@@ -2924,7 +2969,7 @@ function diaspora_send_mail($item,$owner,$contact) {
 
        $body = bb2diaspora($item['body']);
        $created = datetime_convert('UTC','UTC',$item['created'],'Y-m-d H:i:s \U\T\C');
+
        $signed_text =  $item['guid'] . ';' . $cnv['guid'] . ';' . $body .  ';' 
                . $created . ';' . $myaddr . ';' . $cnv['guid'];
 
index 66307573d964a5fb5a672bed33ec37590c13f5e9..b463e6ed17b7e835bdc18eb0ef6a51e3fba7e92d 100644 (file)
@@ -464,12 +464,27 @@ function get_atom_elements($feed, $item, $contact = array()) {
 
        // look for a photo. We should check media size and find the best one,
        // but for now let's just find any author photo
+       // Additionally we look for an alternate author link. On OStatus this one is the one we want.
+
+       // Search for ostatus conversation url
+       $authorlinks = $item->feed->data["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["feed"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["author"][0]["child"]["http://www.w3.org/2005/Atom"]["link"];
+       if (is_array($authorlinks)) {
+               foreach ($authorlinks as $link) {
+                       $linkdata = array_shift($link["attribs"]);
+
+                       if ($linkdata["rel"] == "alternate")
+                               $res["author-link"] = $linkdata["href"];
+               };
+       }
 
        $rawauthor = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
 
        if($rawauthor && $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link']) {
                $base = $rawauthor[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
                foreach($base as $link) {
+                       if($link['attribs']['']['rel'] === 'alternate')
+                               $res['author-link'] = unxmlify($link['attribs']['']['href']);
+
                        if(!x($res, 'author-avatar') || !$res['author-avatar']) {
                                if($link['attribs']['']['rel'] === 'photo' || $link['attribs']['']['rel'] === 'avatar')
                                        $res['author-avatar'] = unxmlify($link['attribs']['']['href']);
@@ -828,7 +843,7 @@ function get_atom_elements($feed, $item, $contact = array()) {
                logger('get_atom_elements: Looking for status.net repeated message');
 
                $message = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["content"][0]["data"];
-               $orig_uri = $child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["id"][0]["data"];
+               $orig_id = ostatus_convert_href($child["http://activitystrea.ms/spec/1.0/"]["object"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10]["id"][0]["data"]);
                $author = $child[SIMPLEPIE_NAMESPACE_ATOM_10]["author"][0]["child"][SIMPLEPIE_NAMESPACE_ATOM_10];
                $uri = $author["uri"][0]["data"];
                $name = $author["name"][0]["data"];
@@ -836,10 +851,10 @@ function get_atom_elements($feed, $item, $contact = array()) {
                $avatar = $avatar["href"];
 
                if (($name != "") and ($uri != "") and ($avatar != "") and ($message != "")) {
-                       logger('get_atom_elements: fixing sender of repeated message.');
+                       logger('get_atom_elements: fixing sender of repeated message. '.$orig_id, LOGGER_DEBUG);
 
                        if (!intval(get_config('system','wall-to-wall_share'))) {
-                               $prefix = share_header($name, $uri, $avatar, "", "", $orig_uri);
+                               $prefix = share_header($name, $uri, $avatar, "", "", $orig_link);
 
                                $res["body"] = $prefix.html2bbcode($message)."[/share]";
                        } else {
@@ -864,8 +879,11 @@ function get_atom_elements($feed, $item, $contact = array()) {
                        $conversation = array_shift($link["attribs"]);
 
                        if ($conversation["rel"] == "ostatus:conversation") {
-                               $res["ostatus_conversation"] = $conversation["href"];
+                               $res["ostatus_conversation"] = ostatus_convert_href($conversation["href"]);
                                logger('get_atom_elements: found conversation url '.$res["ostatus_conversation"]);
+                       } elseif ($conversation["rel"] == "alternate") {
+                               $res["plink"] = $conversation["href"];
+                               logger('get_atom_elements: found plink '.$res["plink"]);
                        }
                };
        }
@@ -1090,6 +1108,14 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
                unset($arr['dsprsig']);
        }
 
+       // Converting the plink
+       if ($arr['network'] == NETWORK_OSTATUS) {
+               if (isset($arr['plink']))
+                       $arr['plink'] = ostatus_convert_href($arr['plink']);
+               elseif (isset($arr['uri']))
+                       $arr['plink'] = ostatus_convert_href($arr['uri']);
+       }
+
        // if an OStatus conversation url was passed in, it is stored and then
        // removed from the array.
        $ostatus_conversation = null;
@@ -1115,7 +1141,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
 
        /* check for create  date and expire time */
        $uid = intval($arr['uid']);
-       $r = q("SELECT expire FROM user WHERE uid = %d", $uid);
+       $r = q("SELECT expire FROM user WHERE uid = %d", intval($uid));
        if(count($r)) {
                $expire_interval = $r[0]['expire'];
                if ($expire_interval>0) {
@@ -1141,6 +1167,19 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
                }
        }
 
+       // If there is no guid then take the same guid that was taken before for the same plink
+       if ((trim($arr['guid']) == "") AND (trim($arr['plink']) != "")) {
+               logger('item_store: checking for an existing guid for plink '.$arr['plink'], LOGGER_DEBUG);
+               $r = q("SELECT `guid` FROM `item` WHERE `plink` = '%s' AND `guid` != '' LIMIT 1",
+                       dbesc(trim($arr['plink']))
+               );
+
+               if(count($r)) {
+                       $arr['guid'] = $r[0]["guid"];
+                       logger('item_store: found guid '.$arr['guid'].' for plink '.$arr['plink'], LOGGER_DEBUG);
+               }
+       }
+
        // Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
        // Deactivated, since the bbcode parser can handle with it - and it destroys posts with some smileys that contain "<"
        //if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
@@ -1347,12 +1386,36 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
                }
        }
 
-       $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+       $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
                dbesc($arr['uri']),
+               dbesc($arr['network']),
+               intval($arr['uid'])
+       );
+       if($r && count($r)) {
+               logger('duplicated item with the same uri found. ' . print_r($arr,true));
+               return 0;
+       }
+
+       $r = q("SELECT `id` FROM `item` WHERE `plink` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
+               dbesc($arr['plink']),
+               dbesc($arr['network']),
                intval($arr['uid'])
        );
        if($r && count($r)) {
-               logger('item-store: duplicate item ignored. ' . print_r($arr,true));
+               logger('duplicated item with the same plink found. ' . print_r($arr,true));
+               return 0;
+       }
+
+       // Check for an existing post with the same content. There seems to be a problem with OStatus.
+       $r = q("SELECT `id` FROM `item` WHERE `body` = '%s' AND `network` = '%s' AND `created` = '%s' AND `contact-id` = %d AND `uid` = %d LIMIT 1",
+               dbesc($arr['body']),
+               dbesc($arr['network']),
+               dbesc($arr['created']),
+               intval($arr['contact-id']),
+               intval($arr['uid'])
+       );
+       if($r && count($r)) {
+               logger('duplicated item with the same body found. ' . print_r($arr,true));
                return 0;
        }
 
@@ -2167,6 +2230,12 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
                return;
        }
 
+       // Test - remove before flight
+//     if ($contact['network'] === NETWORK_OSTATUS) {
+//             $tempfile = tempnam(get_temppath(), "ostatus");
+//             file_put_contents($tempfile, $xml);
+//     }
+
        $feed = new SimplePie();
        $feed->set_raw_data($xml);
        if($datedir)
index 667f7dde427bdfd4d0c81a7c7bcf241fbdc36617..f40c4827b94759b8e416ce7181c9a6ce8a19b159 100644 (file)
@@ -1,8 +1,33 @@
 <?php
+require_once("include/Contact.php");
+
 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
 define('OSTATUS_DEFAULT_POLL_TIMEFRAME', 1440); // given in minutes
 
-function check_conversations() {
+function ostatus_convert_href($href) {
+       $elements = explode(":",$href);
+
+       if ((count($elements) <= 2) OR ($elements[0] != "tag"))
+               return $href;
+
+       $server = explode(",", $elements[1]);
+       $conversation = explode("=", $elements[2]);
+
+       if ((count($elements) == 4) AND ($elements[2] == "post"))
+               return "http://".$server[0]."/notice/".$elements[3];
+
+       if ((count($conversation) != 2) OR ($conversation[1] ==""))
+               return $href;
+
+       if ($elements[3] == "objectType=thread")
+               return "http://".$server[0]."/conversation/".$conversation[1];
+       else
+               return "http://".$server[0]."/notice/".$conversation[1];
+
+       return $href;
+}
+
+function check_conversations($override = false) {
         $last = get_config('system','ostatus_last_poll');
 
         $poll_interval = intval(get_config('system','ostatus_poll_interval'));
@@ -10,16 +35,16 @@ function check_conversations() {
                 $poll_interval = OSTATUS_DEFAULT_POLL_INTERVAL;
 
        // Don't poll if the interval is set negative
-       if ($poll_interval < 0)
+       if (($poll_interval < 0) AND !$override)
                return;
 
         $poll_timeframe = intval(get_config('system','ostatus_poll_timeframe'));
-        if(! $poll_timeframe)
+        if (!$poll_timeframe)
                 $poll_timeframe = OSTATUS_DEFAULT_POLL_TIMEFRAME;
 
-        if($last) {
+        if ($last AND !$override) {
                 $next = $last + ($poll_interval * 60);
-                if($next > time()) {
+                if ($next > time()) {
                         logger('poll interval not reached');
                         return;
                 }
@@ -36,7 +61,7 @@ function check_conversations() {
                 complete_conversation($id, $url);
         }
 
-        logger(' cron_end');
+        logger('cron_end');
 
         set_config('system','ostatus_last_poll', time());
 }
@@ -44,6 +69,8 @@ function check_conversations() {
 function complete_conversation($itemid, $conversation_url, $only_add_conversation = false) {
        global $a;
 
+       $conversation_url = ostatus_convert_href($conversation_url);
+
        if (intval(get_config('system','ostatus_poll_interval')) == -2)
                return;
 
@@ -107,12 +134,16 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
        $items = array_reverse($items);
 
        foreach ($items as $single_conv) {
-               // status.net changed the format of the activity streams. This is a quick fix.
-               if (@is_string($single_conv->object->id))
+               if (isset($single_conv->object->id))
                        $single_conv->id = $single_conv->object->id;
 
-               if (@!$single_conv->id AND $single_conv->provider->url AND $single_conv->statusnet_notice_info->local_id)
-                       $single_conv->id = $single_conv->provider->url."notice/".$single_conv->statusnet_notice_info->local_id;
+               logger("Got id ".$single_conv->id, LOGGER_DEBUG);
+
+               $plink = ostatus_convert_href($single_conv->id);
+               if (isset($single_conv->object->url))
+                       $plink = ostatus_convert_href($single_conv->object->url);
+
+               logger("Got url ".$plink, LOGGER_DEBUG);
 
                if (@!$single_conv->id)
                        continue;
@@ -120,8 +151,9 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                if ($first_id == "") {
                        $first_id = $single_conv->id;
 
-                       $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
-                               intval($message["uid"]), dbesc($first_id));
+                       $new_parents = q("SELECT `id`, `uri`, `contact-id`, `type`, `verb`, `visible` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
+                               intval($message["uid"]), dbesc($first_id),
+                               dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
                        if ($new_parents) {
                                $parent = $new_parents[0];
                                logger('adopting new parent '.$parent["id"].' for '.$itemid);
@@ -136,12 +168,21 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                else
                        $parent_uri = $parent["uri"];
 
-               $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
-                                                       intval($message["uid"]), dbesc($single_conv->id));
+               $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `plink` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
+                                                       intval($message["uid"]), dbesc($plink),
+                                                       dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
+
+               if (!$message_exists)
+                       $message_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
+                                                       intval($message["uid"]), dbesc($single_conv->id),
+                                                       dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
+
                if ($message_exists) {
                        if ($parent["id"] != 0) {
                                $existing_message = $message_exists[0];
 
+                               logger('updating id '.$existing_message["id"].' to parent '.$parent["id"].' uri '.$parent["uri"].' thread '.$parent_uri, LOGGER_DEBUG);
+
                                // This is partly bad, since the entry in the thread table isn't updated
                                $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `thr-parent` = '%s' WHERE `id` = %d",
                                        intval($parent["id"]),
@@ -152,21 +193,32 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                        continue;
                }
 
+               $actor = $single_conv->actor->id;
+               if (isset($single_conv->actor->url))
+                       $actor = $single_conv->actor->url;
+
                $contact = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` != '%s'",
-                               $message["uid"], normalise_link($single_conv->actor->id), NETWORK_STATUSNET);
+                               $message["uid"], normalise_link($actor), NETWORK_STATUSNET);
 
                if (count($contact)) {
-                       logger("Found contact for url ".$single_conv->actor->id, LOGGER_DEBUG);
+                       logger("Found contact for url ".$actor, LOGGER_DEBUG);
                        $contact_id = $contact[0]["id"];
                } else {
-                       logger("No contact found for url ".$single_conv->actor->id, LOGGER_DEBUG);
+                       logger("No contact found for url ".$actor, LOGGER_DEBUG);
+
+                       // Adding a global contact
+                       // To-Do: Use this data for the post
+                       $global_contact_id = get_contact($actor, 0);
+
+                       logger("Global contact ".$global_contact_id." found for url ".$actor, LOGGER_DEBUG);
+
                        $contact_id = $parent["contact-id"];
                }
 
                $arr = array();
                $arr["network"] = NETWORK_OSTATUS;
                $arr["uri"] = $single_conv->id;
-               $arr["plink"] = $single_conv->id;
+               $arr["plink"] = $plink;
                $arr["uid"] = $message["uid"];
                $arr["contact-id"] = $contact_id;
                if ($parent["id"] != 0)
@@ -182,17 +234,26 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
                if ($arr["owner-name"] == '')
                        $arr["owner-name"] =  $single_conv->actor->displayName;
 
-               $arr["owner-link"] = $single_conv->actor->id;
+               $arr["owner-link"] = $actor;
                $arr["owner-avatar"] = $single_conv->actor->image->url;
                //$arr["author-name"] = $single_conv->actor->contact->displayName;
                //$arr["author-name"] = $single_conv->actor->contact->preferredUsername;
                $arr["author-name"] = $arr["owner-name"];
-               $arr["author-link"] = $single_conv->actor->id;
+               $arr["author-link"] = $actor;
                $arr["author-avatar"] = $single_conv->actor->image->url;
                $arr["body"] = html2bbcode($single_conv->content);
-               $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
-               if ($arr["app"] == "")
+
+               if (isset($single_conv->status_net->notice_info->source))
+                       $arr["app"] = strip_tags($single_conv->status_net->notice_info->source);
+               elseif (isset($single_conv->statusnet->notice_info->source))
+                       $arr["app"] = strip_tags($single_conv->statusnet->notice_info->source);
+               elseif (isset($single_conv->statusnet_notice_info->source))
+                       $arr["app"] = strip_tags($single_conv->statusnet_notice_info->source);
+               elseif (isset($single_conv->provider->displayName))
                        $arr["app"] = $single_conv->provider->displayName;
+               else
+                       $arr["app"] = "OStatus";
+
                $arr["verb"] = $parent["verb"];
                $arr["visible"] = $parent["visible"];
                $arr["location"] = $single_conv->location->displayName;
@@ -206,6 +267,8 @@ function complete_conversation($itemid, $conversation_url, $only_add_conversatio
 
                $newitem = item_store($arr);
 
+               logger('Stored new item '.$plink.' under id '.$newitem, LOGGER_DEBUG);
+
                // Add the conversation entry (but don't fetch the whole conversation)
                complete_conversation($newitem, $conversation_url, true);