Make feeds validate

This commit is contained in:
Michael 2020-07-11 18:54:08 +00:00
parent fb18325b6b
commit b107a4984e

View file

@ -1271,6 +1271,7 @@ class OStatus
$root = $doc->createElementNS(ActivityNamespace::ATOM1, 'feed'); $root = $doc->createElementNS(ActivityNamespace::ATOM1, 'feed');
$doc->appendChild($root); $doc->appendChild($root);
if (!$feed_mode) {
$root->setAttribute("xmlns:thr", ActivityNamespace::THREAD); $root->setAttribute("xmlns:thr", ActivityNamespace::THREAD);
$root->setAttribute("xmlns:georss", ActivityNamespace::GEORSS); $root->setAttribute("xmlns:georss", ActivityNamespace::GEORSS);
$root->setAttribute("xmlns:activity", ActivityNamespace::ACTIVITY); $root->setAttribute("xmlns:activity", ActivityNamespace::ACTIVITY);
@ -1279,6 +1280,7 @@ class OStatus
$root->setAttribute("xmlns:ostatus", ActivityNamespace::OSTATUS); $root->setAttribute("xmlns:ostatus", ActivityNamespace::OSTATUS);
$root->setAttribute("xmlns:statusnet", ActivityNamespace::STATUSNET); $root->setAttribute("xmlns:statusnet", ActivityNamespace::STATUSNET);
$root->setAttribute("xmlns:mastodon", ActivityNamespace::MASTODON); $root->setAttribute("xmlns:mastodon", ActivityNamespace::MASTODON);
}
$title = ''; $title = '';
$selfUri = '/feed/' . $owner["nick"] . '/'; $selfUri = '/feed/' . $owner["nick"] . '/';
@ -1308,7 +1310,7 @@ class OStatus
XML::addElement($doc, $root, "logo", $owner["photo"]); XML::addElement($doc, $root, "logo", $owner["photo"]);
XML::addElement($doc, $root, "updated", DateTimeFormat::utcNow(DateTimeFormat::ATOM)); XML::addElement($doc, $root, "updated", DateTimeFormat::utcNow(DateTimeFormat::ATOM));
$author = self::addAuthor($doc, $owner); $author = self::addAuthor($doc, $owner, true, $feed_mode);
$root->appendChild($author); $root->appendChild($author);
$attributes = ["href" => $owner["url"], "rel" => "alternate", "type" => "text/html"]; $attributes = ["href" => $owner["url"], "rel" => "alternate", "type" => "text/html"];
@ -1322,6 +1324,7 @@ class OStatus
self::hublinks($doc, $root, $owner["nick"]); self::hublinks($doc, $root, $owner["nick"]);
if (!$feed_mode) {
$attributes = ["href" => DI::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "salmon"]; $attributes = ["href" => DI::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "salmon"];
XML::addElement($doc, $root, "link", "", $attributes); XML::addElement($doc, $root, "link", "", $attributes);
@ -1330,6 +1333,7 @@ class OStatus
$attributes = ["href" => DI::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-mention"]; $attributes = ["href" => DI::baseUrl() . "/salmon/" . $owner["nick"], "rel" => "http://salmon-protocol.org/ns/salmon-mention"];
XML::addElement($doc, $root, "link", "", $attributes); XML::addElement($doc, $root, "link", "", $attributes);
}
$attributes = ["href" => DI::baseUrl() . $selfUri, "rel" => "self", "type" => "application/atom+xml"]; $attributes = ["href" => DI::baseUrl() . $selfUri, "rel" => "self", "type" => "application/atom+xml"];
XML::addElement($doc, $root, "link", "", $attributes); XML::addElement($doc, $root, "link", "", $attributes);
@ -1389,7 +1393,7 @@ class OStatus
$attributes = ["rel" => "enclosure", $attributes = ["rel" => "enclosure",
"href" => $siteinfo["url"], "href" => $siteinfo["url"],
"type" => "text/html; charset=UTF-8", "type" => "text/html; charset=UTF-8",
"length" => "", "length" => "0",
"title" => ($siteinfo["title"] ?? '') ?: $siteinfo["url"], "title" => ($siteinfo["title"] ?? '') ?: $siteinfo["url"],
]; ];
XML::addElement($doc, $root, "link", "", $attributes); XML::addElement($doc, $root, "link", "", $attributes);
@ -1438,27 +1442,31 @@ class OStatus
* @param DOMDocument $doc XML document * @param DOMDocument $doc XML document
* @param array $owner Contact data of the poster * @param array $owner Contact data of the poster
* @param bool $show_profile Whether to show profile * @param bool $show_profile Whether to show profile
* @param bool $feed_mode Behave like a regular feed for users if true
* *
* @return \DOMElement author element * @return \DOMElement author element
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
private static function addAuthor(DOMDocument $doc, array $owner, $show_profile = true) private static function addAuthor(DOMDocument $doc, array $owner, $show_profile = true, $feed_mode = false)
{ {
$profile = DBA::selectFirst('profile', ['homepage', 'publish'], ['uid' => $owner['uid']]); $profile = DBA::selectFirst('profile', ['homepage', 'publish'], ['uid' => $owner['uid']]);
$author = $doc->createElement("author"); $author = $doc->createElement("author");
if (!$feed_mode) {
XML::addElement($doc, $author, "id", $owner["url"]); XML::addElement($doc, $author, "id", $owner["url"]);
if ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) { if ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY) {
XML::addElement($doc, $author, "activity:object-type", Activity\ObjectType::GROUP); XML::addElement($doc, $author, "activity:object-type", Activity\ObjectType::GROUP);
} else { } else {
XML::addElement($doc, $author, "activity:object-type", Activity\ObjectType::PERSON); XML::addElement($doc, $author, "activity:object-type", Activity\ObjectType::PERSON);
} }
}
XML::addElement($doc, $author, "uri", $owner["url"]); XML::addElement($doc, $author, "uri", $owner["url"]);
XML::addElement($doc, $author, "name", $owner["nick"]); XML::addElement($doc, $author, "name", $owner["nick"]);
XML::addElement($doc, $author, "email", $owner["addr"]); XML::addElement($doc, $author, "email", $owner["addr"]);
if ($show_profile) { if ($show_profile && !$feed_mode) {
XML::addElement($doc, $author, "summary", BBCode::convert($owner["about"], false, BBCode::OSTATUS)); XML::addElement($doc, $author, "summary", BBCode::convert($owner["about"], false, BBCode::OSTATUS));
} }
if (!$feed_mode) {
$attributes = ["rel" => "alternate", "type" => "text/html", "href" => $owner["url"]]; $attributes = ["rel" => "alternate", "type" => "text/html", "href" => $owner["url"]];
XML::addElement($doc, $author, "link", "", $attributes); XML::addElement($doc, $author, "link", "", $attributes);
@ -1508,6 +1516,7 @@ class OStatus
XML::addElement($doc, $author, "mastodon:scope", "public"); XML::addElement($doc, $author, "mastodon:scope", "public");
} }
} }
}
return $author; return $author;
} }
@ -1569,7 +1578,7 @@ class OStatus
$repeated_guid = self::getResharedGuid($item); $repeated_guid = self::getResharedGuid($item);
if ($repeated_guid != "") { if ($repeated_guid != "") {
$xml = self::reshareEntry($doc, $item, $owner, $repeated_guid, $toplevel); $xml = self::reshareEntry($doc, $item, $owner, $repeated_guid, $toplevel, $feed_mode);
} }
if ($xml) { if ($xml) {
@ -1668,12 +1677,13 @@ class OStatus
* @param array $owner Contact data of the poster * @param array $owner Contact data of the poster
* @param string $repeated_guid guid * @param string $repeated_guid guid
* @param bool $toplevel Is it for en entry element (false) or a feed entry (true)? * @param bool $toplevel Is it for en entry element (false) or a feed entry (true)?
* @param bool $feed_mode Behave like a regular feed for users if true
* *
* @return bool Entry element * @return bool Entry element
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
private static function reshareEntry(DOMDocument $doc, array $item, array $owner, $repeated_guid, $toplevel) private static function reshareEntry(DOMDocument $doc, array $item, array $owner, $repeated_guid, $toplevel, $feed_mode = false)
{ {
if (($item['gravity'] != GRAVITY_PARENT) && (Strings::normaliseLink($item["author-link"]) != Strings::normaliseLink($owner["url"]))) { if (($item['gravity'] != GRAVITY_PARENT) && (Strings::normaliseLink($item["author-link"]) != Strings::normaliseLink($owner["url"]))) {
Logger::log("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", Logger::DEBUG); Logger::log("OStatus entry is from author ".$owner["url"]." - not from ".$item["author-link"].". Quitting.", Logger::DEBUG);
@ -1692,8 +1702,9 @@ class OStatus
$title = $owner["nick"]." repeated a notice by ".$contact["nick"]; $title = $owner["nick"]." repeated a notice by ".$contact["nick"];
self::entryContent($doc, $entry, $item, $owner, $title, Activity::SHARE, false); self::entryContent($doc, $entry, $item, $owner, $title, Activity::SHARE, false, $feed_mode);
if (!$feed_mode) {
$as_object = $doc->createElement("activity:object"); $as_object = $doc->createElement("activity:object");
XML::addElement($doc, $as_object, "activity:object-type", ActivityNamespace::ACTIVITY_SCHEMA . "activity"); XML::addElement($doc, $as_object, "activity:object-type", ActivityNamespace::ACTIVITY_SCHEMA . "activity");
@ -1720,8 +1731,9 @@ class OStatus
$as_object->appendChild($source); $as_object->appendChild($source);
$entry->appendChild($as_object); $entry->appendChild($as_object);
}
self::entryFooter($doc, $entry, $item, $owner); self::entryFooter($doc, $entry, $item, $owner, true, $feed_mode);
return $entry; return $entry;
} }
@ -1906,7 +1918,9 @@ class OStatus
$entry = self::entryHeader($doc, $owner, $item, $toplevel); $entry = self::entryHeader($doc, $owner, $item, $toplevel);
if (!$feed_mode) {
XML::addElement($doc, $entry, "activity:object-type", Activity\ObjectType::NOTE); XML::addElement($doc, $entry, "activity:object-type", Activity\ObjectType::NOTE);
}
self::entryContent($doc, $entry, $item, $owner, $title, '', true, $feed_mode); self::entryContent($doc, $entry, $item, $owner, $title, '', true, $feed_mode);
@ -2063,6 +2077,7 @@ class OStatus
} }
} }
if (!$feed_mode) {
XML::addElement($doc, $entry, "link", "", ["rel" => "ostatus:conversation", "href" => $conversation_href]); XML::addElement($doc, $entry, "link", "", ["rel" => "ostatus:conversation", "href" => $conversation_href]);
$attributes = [ $attributes = [
@ -2072,6 +2087,7 @@ class OStatus
XML::addElement($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes); XML::addElement($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes);
} }
}
// uri-id isn't present for follow entry pseudo-items // uri-id isn't present for follow entry pseudo-items
$tags = Tag::getByURIId($item['uri-id'] ?? 0); $tags = Tag::getByURIId($item['uri-id'] ?? 0);
@ -2147,9 +2163,11 @@ class OStatus
XML::addElement($doc, $entry, "georss:point", $item["coord"]); XML::addElement($doc, $entry, "georss:point", $item["coord"]);
} }
if (!$feed_mode) {
XML::addElement($doc, $entry, "statusnet:notice_info", "", $attributes); XML::addElement($doc, $entry, "statusnet:notice_info", "", $attributes);
} }
} }
}
/** /**
* Creates the XML feed for a given nickname * Creates the XML feed for a given nickname
@ -2238,6 +2256,10 @@ class OStatus
$item['body'] .= '🍼'; $item['body'] .= '🍼';
} }
if (in_array($item["verb"], [Activity::FOLLOW, Activity::O_UNFOLLOW, Activity::LIKE])) {
continue;
}
$entry = self::entry($doc, $item, $owner, false, $feed_mode); $entry = self::entry($doc, $item, $owner, false, $feed_mode);
$root->appendChild($entry); $root->appendChild($entry);