From 35dc56c9c25f30823ff1cfbefe8ea9eab47cc6ee Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 27 Apr 2017 20:38:46 +0000 Subject: [PATCH] Salmons with Mastodon seem to work now --- boot.php | 1 + database.sql | 2 + include/dba.php | 24 ++++++++++- include/dbstructure.php | 3 +- include/dfrn.php | 46 +++++++++++++++++++-- include/diaspora.php | 10 +++-- include/items.php | 24 +++++------ include/ostatus.php | 90 ++++++++++++++++++++++++++++++----------- 8 files changed, 156 insertions(+), 44 deletions(-) diff --git a/boot.php b/boot.php index 480001475b..1851f84579 100644 --- a/boot.php +++ b/boot.php @@ -368,6 +368,7 @@ define ( 'NAMESPACE_FEED', 'http://schemas.google.com/g/2010#updates- define ( 'NAMESPACE_OSTATUS', 'http://ostatus.org/schema/1.0' ); define ( 'NAMESPACE_STATUSNET', 'http://status.net/schema/api/1/' ); define ( 'NAMESPACE_ATOM1', 'http://www.w3.org/2005/Atom' ); +define ( 'NAMESPACE_MASTODON', 'http://mastodon.social/schema/1.0' ); /* @}*/ /** diff --git a/database.sql b/database.sql index e9a4ee2e4c..c8010e1e20 100644 --- a/database.sql +++ b/database.sql @@ -211,8 +211,10 @@ CREATE TABLE IF NOT EXISTS `conversation` ( `item-uri` varbinary(255) NOT NULL, `reply-to-uri` varbinary(255) NOT NULL DEFAULT '', `conversation-uri` varbinary(255) NOT NULL DEFAULT '', + `conversation-href` varbinary(255) NOT NULL DEFAULT '', `protocol` tinyint(1) unsigned NOT NULL DEFAULT 0, `source` mediumtext, + `received` datetime NOT NULL DEFAULT '0001-01-01 00:00:00', PRIMARY KEY(`item-uri`), INDEX `conversation-uri` (`conversation-uri`) ) DEFAULT COLLATE utf8mb4_general_ci; diff --git a/include/dba.php b/include/dba.php index 5846d49484..7e9979cf9e 100644 --- a/include/dba.php +++ b/include/dba.php @@ -634,7 +634,29 @@ class dba { if (is_bool($stmt)) { $retval = $stmt; } else { - $retval = (self::rows($stmt) > 0); + $retval = (self::num_rows($stmt) > 0); + } + + self::close($stmt); + + return $retval; + } + + /** + * @brief Fetches the first row + * + * @param string $sql SQL statement + * @return array first row of query + */ + static public function fetch_first($sql) { + $args = func_get_args(); + + $stmt = call_user_func_array('self::p', $args); + + if (is_bool($stmt)) { + $retval = $stmt; + } else { + $retval = self::fetch($stmt); } self::close($stmt); diff --git a/include/dbstructure.php b/include/dbstructure.php index 5260a87a33..156d3eb645 100644 --- a/include/dbstructure.php +++ b/include/dbstructure.php @@ -814,12 +814,13 @@ function db_definition() { "item-uri" => array("type" => "varbinary(255)", "not null" => "1", "primary" => "1"), "reply-to-uri" => array("type" => "varbinary(255)", "not null" => "1", "default" => ""), "conversation-uri" => array("type" => "varbinary(255)", "not null" => "1", "default" => ""), + "conversation-href" => array("type" => "varbinary(255)", "not null" => "1", "default" => ""), "protocol" => array("type" => "tinyint(1) unsigned", "not null" => "1", "default" => "0"), "source" => array("type" => "mediumtext"), "received" => array("type" => "datetime", "not null" => "1", "default" => NULL_DATE), ), "indexes" => array( - "PRIMARY" => array("item-uri"), + "PRIMARY" => array("item-uri", "protocol"), "conversation-uri" => array("conversation-uri"), ) ); diff --git a/include/dfrn.php b/include/dfrn.php index ddebf1e9bf..5977ed9a0c 100644 --- a/include/dfrn.php +++ b/include/dfrn.php @@ -817,6 +817,28 @@ class dfrn { xml::add_element($doc, $entry, "thr:in-reply-to", "", $attributes); } + // Add conversation data. This is used for OStatus + $conversation_href = App::get_baseurl()."/display/".$owner["nick"]."/".$item["parent"]; + $conversation_uri = $conversation_href; + + if (isset($parent_item)) { + $r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $item['parent-uri']); + if (dbm::is_result($r)) { + if ($r['conversation-uri'] != '') { + $conversation_uri = $r['conversation-uri']; + } + if ($r['conversation-href'] != '') { + $conversation_href = $r['conversation-href']; + } + } + } + + $attributes = array( + "href" => $conversation_href, + "ref" => $conversation_uri); + + xml::add_element($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes); + xml::add_element($doc, $entry, "id", $item["uri"]); xml::add_element($doc, $entry, "title", $item["title"]); @@ -2209,12 +2231,16 @@ class dfrn { * @param array $importer Record of the importer user mixed with contact of the content * @todo Add type-hints */ - private static function process_entry($header, $xpath, $entry, $importer) { + private static function process_entry($header, $xpath, $entry, $importer, $xml) { logger("Processing entries"); $item = $header; + $item["protocol"] = PROTOCOL_DFRN; + + $item["source"] = $xml; + // Get the uri $item["uri"] = $xpath->query("atom:id/text()", $entry)->item(0)->nodeValue; @@ -2373,6 +2399,20 @@ class dfrn { self::parse_links($links, $item); } + $item['conversation-uri'] = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue; + + $conv = $xpath->query('ostatus:conversation', $entry); + if (is_object($conv->item(0))) { + foreach ($conv->item(0)->attributes AS $attributes) { + if ($attributes->name == "ref") { + $item['conversation-uri'] = $attributes->textContent; + } + if ($attributes->name == "href") { + $item['conversation-href'] = $attributes->textContent; + } + } + } + // Is it a reply or a top level posting? $item["parent-uri"] = $item["uri"]; @@ -2801,7 +2841,7 @@ class dfrn { if (!$sort_by_date) { $entries = $xpath->query("/atom:feed/atom:entry"); foreach ($entries AS $entry) { - self::process_entry($header, $xpath, $entry, $importer); + self::process_entry($header, $xpath, $entry, $importer, $xml); } } else { $newentries = array(); @@ -2815,7 +2855,7 @@ class dfrn { ksort($newentries); foreach ($newentries AS $entry) { - self::process_entry($header, $xpath, $entry, $importer); + self::process_entry($header, $xpath, $entry, $importer, $xml); } } logger("Import done for user " . $importer["uid"] . " from contact " . $importer["id"], LOGGER_DEBUG); diff --git a/include/diaspora.php b/include/diaspora.php index b8eff62b11..b926f21da9 100644 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -1308,7 +1308,9 @@ class Diaspora { } $datarray["object-type"] = ACTIVITY_OBJ_COMMENT; - $datarray["object"] = $xml; + + $datarray["protocol"] = PROTOCOL_DIASPORA; + $datarray["source"] = $xml; $datarray["changed"] = $datarray["created"] = $datarray["edited"] = $created_at; @@ -2278,7 +2280,8 @@ class Diaspora { $datarray["verb"] = ACTIVITY_POST; $datarray["gravity"] = GRAVITY_PARENT; - $datarray["object"] = $xml; + $datarray["protocol"] = PROTOCOL_DIASPORA; + $datarray["source"] = $xml; $prefix = share_header($original_item["author-name"], $original_item["author-link"], $original_item["author-avatar"], $original_item["guid"], $original_item["created"], $orig_url); @@ -2481,7 +2484,8 @@ class Diaspora { $datarray["verb"] = ACTIVITY_POST; $datarray["gravity"] = GRAVITY_PARENT; - $datarray["object"] = $xml; + $datarray["protocol"] = PROTOCOL_DIASPORA; + $datarray["source"] = $xml; $datarray["body"] = self::replace_people_guid($body, $contact["url"]); diff --git a/include/items.php b/include/items.php index 3bf0a001c6..dcc99e8bd9 100644 --- a/include/items.php +++ b/include/items.php @@ -693,39 +693,37 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f if (in_array($arr['network'], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) { $conversation = array('item-uri' => $arr['uri'], 'received' => dbm::date()); + if (isset($arr['thr-parent'])) { if ($arr['thr-parent'] != $arr['uri']) { $conversation['reply-to-uri'] = $arr['thr-parent']; } } + if (isset($arr['conversation-uri'])) { $conversation['conversation-uri'] = $arr['conversation-uri']; - unset($arr['conversation-uri']); } - if ($arr['network'] == NETWORK_DFRN) { - $conversation['protocol'] = PROTOCOL_DFRN; - } elseif ($arr['network'] == NETWORK_DIASPORA) { - $conversation['protocol'] = PROTOCOL_DIASPORA; + if (isset($arr['conversation-href'])) { + $conversation['conversation-href'] = $arr['conversation-href']; } if (isset($arr['protocol'])) { $conversation['protocol'] = $arr['protocol']; - unset($arr['protocol']); - } - if (isset($arr['object'])) { - $conversation['source'] = $arr['object']; - if (in_array($arr['network'], array(NETWORK_DIASPORA, NETWORK_OSTATUS))) { - unset($arr['object']); - } } + if (isset($arr['source'])) { $conversation['source'] = $arr['source']; - unset($arr['source']); } + dba::insert('conversation', $conversation); } + unset($arr['conversation-uri']); + unset($arr['conversation-href']); + unset($arr['protocol']); + unset($arr['source']); + if ($arr['parent-uri'] === $arr['uri']) { $parent_id = 0; $parent_deleted = 0; diff --git a/include/ostatus.php b/include/ostatus.php index 54d67d66ed..1800a9cc81 100644 --- a/include/ostatus.php +++ b/include/ostatus.php @@ -420,6 +420,19 @@ class ostatus { $item["created"] = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue; $item["edited"] = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue; $conversation = $xpath->query('ostatus:conversation/text()', $entry)->item(0)->nodeValue; + $item['conversation-uri'] = $conversation; + + $conv = $xpath->query('ostatus:conversation', $entry); + if (is_object($conv->item(0))) { + foreach ($conv->item(0)->attributes AS $attributes) { + if ($attributes->name == "ref") { + $item['conversation-uri'] = $attributes->textContent; + } + if ($attributes->name == "href") { + $item['conversation-href'] = $attributes->textContent; + } + } + } $related = ""; @@ -473,6 +486,10 @@ class ostatus { break; case "ostatus:conversation": $conversation = $attribute['href']; + $item['conversation-href'] = $conversation; + if (!isset($item['conversation-uri'])) { + $item['conversation-uri'] = $item['conversation-href']; + } break; case "enclosure": $enclosure = $attribute['href']; @@ -1162,6 +1179,10 @@ class ostatus { $arr["author-avatar"] = $arr["owner-avatar"]; $arr["body"] = add_page_info_to_body(html2bbcode($single_conv->content)); + if (isset($single_conv->status_net->conversation)) { + $arr['conversation-uri'] = $single_conv->status_net->conversation; + } + 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)) @@ -1408,6 +1429,7 @@ class ostatus { $root->setAttribute("xmlns:poco", NAMESPACE_POCO); $root->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS); $root->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET); + $root->setAttribute("xmlns:mastodon", NAMESPACE_MASTODON); $attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION); xml::add_element($doc, $root, "generator", FRIENDICA_PLATFORM, $attributes); @@ -1546,14 +1568,16 @@ class ostatus { */ private function add_author($doc, $owner) { - $r = q("SELECT `homepage` FROM `profile` WHERE `uid` = %d AND `is-default` LIMIT 1", intval($owner["uid"])); + $r = q("SELECT `homepage`, `publish` FROM `profile` WHERE `uid` = %d AND `is-default` LIMIT 1", intval($owner["uid"])); if ($r) $profile = $r[0]; $author = $doc->createElement("author"); + xml::add_element($doc, $author, "id", $owner["url"]); xml::add_element($doc, $author, "activity:object-type", ACTIVITY_OBJ_PERSON); xml::add_element($doc, $author, "uri", $owner["url"]); - xml::add_element($doc, $author, "name", $owner["name"]); + xml::add_element($doc, $author, "name", $owner["nick"]); + xml::add_element($doc, $author, "email", $owner["addr"]); xml::add_element($doc, $author, "summary", bbcode($owner["about"], false, false, 7)); $attributes = array("rel" => "alternate", "type" => "text/html", "href" => $owner["url"]); @@ -1600,6 +1624,9 @@ class ostatus { xml::add_element($doc, $author, "statusnet:profile_info", "", array("local_id" => $owner["uid"])); } + if ($profile["publish"]) { + xml::add_element($doc, $author, "mastodon:scope", "public"); + } return $author; } @@ -1773,7 +1800,7 @@ class ostatus { self::entry_content($doc, $as_object, $repeated_item, $owner, "", "", false); $author = self::add_author($doc, $contact); - $as_object->appendChild($author); + $as_object->appendChild($author); $as_object2 = $doc->createElement("activity:object"); @@ -1995,6 +2022,7 @@ class ostatus { $entry->setAttribute("xmlns:poco", NAMESPACE_POCO); $entry->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS); $entry->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET); + $entry->setAttribute("xmlns:mastodon", NAMESPACE_MASTODON); $author = self::add_author($doc, $owner); $entry->appendChild($author); @@ -2061,39 +2089,54 @@ class ostatus { $parent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `id` = %d", intval($item["parent"])); $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']); - $attributes = array( - "ref" => $parent_item, - "type" => "text/html", - "href" => App::get_baseurl()."/display/".$parent[0]["guid"]); - xml::add_element($doc, $entry, "thr:in-reply-to", "", $attributes); - - $attributes = array( - "rel" => "related", - "href" => App::get_baseurl()."/display/".$parent[0]["guid"]); - xml::add_element($doc, $entry, "link", "", $attributes); - - $mentioned[$parent[0]["author-link"]] = $parent[0]["author-link"]; - $mentioned[$parent[0]["owner-link"]] = $parent[0]["owner-link"]; - - $thrparent = q("SELECT `guid`, `author-link`, `owner-link` FROM `item` WHERE `uid` = %d AND `uri` = '%s'", + $thrparent = q("SELECT `guid`, `author-link`, `owner-link`, `plink` FROM `item` WHERE `uid` = %d AND `uri` = '%s'", intval($owner["uid"]), dbesc($parent_item)); if ($thrparent) { $mentioned[$thrparent[0]["author-link"]] = $thrparent[0]["author-link"]; $mentioned[$thrparent[0]["owner-link"]] = $thrparent[0]["owner-link"]; + $parent_plink = $thrparent[0]["plink"]; + } else { + $mentioned[$parent[0]["author-link"]] = $parent[0]["author-link"]; + $mentioned[$parent[0]["owner-link"]] = $parent[0]["owner-link"]; + $parent_plink = App::get_baseurl()."/display/".$parent[0]["guid"]; } + + $attributes = array( + "ref" => $parent_item, + "href" => $parent_plink); + xml::add_element($doc, $entry, "thr:in-reply-to", "", $attributes); + + $attributes = array( + "rel" => "related", + "href" => $parent_plink); + xml::add_element($doc, $entry, "link", "", $attributes); } if (intval($item["parent"]) > 0) { - $conversation = App::get_baseurl()."/display/".$owner["nick"]."/".$item["parent"]; - xml::add_element($doc, $entry, "link", "", array("rel" => "ostatus:conversation", "href" => $conversation)); + $conversation_href = App::get_baseurl()."/display/".$owner["nick"]."/".$item["parent"]; + $conversation_uri = $conversation_href; + + if (isset($parent_item)) { + $r = dba::fetch_first("SELECT `conversation-uri`, `conversation-href` FROM `conversation` WHERE `item-uri` = ?", $parent_item); + if (dbm::is_result($r)) { + if ($r['conversation-uri'] != '') { + $conversation_uri = $r['conversation-uri']; + } + if ($r['conversation-href'] != '') { + $conversation_href = $r['conversation-href']; + } + } + } + + xml::add_element($doc, $entry, "link", "", array("rel" => "ostatus:conversation", "href" => $conversation_href)); $attributes = array( - "href" => $conversation, + "href" => $conversation_href, "local_id" => $item["parent"], - "ref" => $conversation); + "ref" => $conversation_uri); - xml::add_element($doc, $entry, "ostatus:conversation", $conversation, $attributes); + xml::add_element($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes); } $tags = item_getfeedtags($item); @@ -2131,6 +2174,7 @@ class ostatus { xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned", "ostatus:object-type" => "http://activitystrea.ms/schema/1.0/collection", "href" => "http://activityschema.org/collection/public")); + xml::add_element($doc, $entry, "mastodon:scope", "public"); } if(count($tags))