passes w3c validator

This commit is contained in:
Peter Zingg 2022-12-12 18:18:13 -08:00
parent 8e5a88edf7
commit 4cafb8a89a
12 changed files with 110 additions and 66 deletions

View file

@ -14,14 +14,14 @@ defmodule Pleroma.Web.Feed.FeedView do
require Pleroma.Constants require Pleroma.Constants
@spec pub_date(String.t() | DateTime.t()) :: String.t() @spec pub_date(String.t() | DateTime.t() | NaiveDateTime.t(), String.t()) :: String.t()
def pub_date(date) when is_binary(date) do def pub_date(date, format) when is_binary(date) do
date date
|> Timex.parse!("{ISO:Extended}") |> Timex.parse!("{ISO:Extended}")
|> pub_date |> pub_date(format)
end end
def pub_date(%DateTime{} = date), do: Timex.format!(date, "{RFC822}") def pub_date(date, format), do: Timex.format!(date, format)
def prepare_activity(activity, opts \\ []) do def prepare_activity(activity, opts \\ []) do
object = Object.normalize(activity, fetch: false) object = Object.normalize(activity, fetch: false)
@ -39,15 +39,14 @@ defmodule Pleroma.Web.Feed.FeedView do
} }
end end
def most_recent_update(activities) do def most_recent_update(activities, format) do
with %{updated_at: updated_at} <- List.first(activities) do with %{updated_at: updated_at} <- List.first(activities) do
NaiveDateTime.to_iso8601(updated_at) Timex.format!(updated_at, format)
end end
end end
def most_recent_update(activities, user) do def most_recent_update(activities, user, format) do
(List.first(activities) || user).updated_at (List.first(activities) || user).updated_at |> Timex.format!(format)
|> NaiveDateTime.to_iso8601()
end end
def feed_logo do def feed_logo do
@ -107,9 +106,36 @@ defmodule Pleroma.Web.Feed.FeedView do
end end
end end
def escape(html) do def escape(str) do
html str
|> html_escape() |> html_escape()
|> safe_to_string() |> safe_to_string()
|> xml_escape()
end end
# Encoding newlines, e.g., per XML spec
def xml_escape(nil), do: ""
def xml_escape(str) when is_binary(str) do
str
|> xml_escape_string()
|> to_string()
end
defp xml_escape_string(""), do: ""
defp xml_escape_string(<<"&"::utf8, rest::binary>>), do: xml_escape_entity(rest)
defp xml_escape_string(<<"<"::utf8, rest::binary>>), do: ["&lt;" | xml_escape_string(rest)]
defp xml_escape_string(<<">"::utf8, rest::binary>>), do: ["&gt;" | xml_escape_string(rest)]
defp xml_escape_string(<<"\""::utf8, rest::binary>>), do: ["&quot;" | xml_escape_string(rest)]
defp xml_escape_string(<<"'"::utf8, rest::binary>>), do: ["&apos;" | xml_escape_string(rest)]
defp xml_escape_string(<<"\t"::utf8, rest::binary>>), do: ["&#9;" | xml_escape_string(rest)]
defp xml_escape_string(<<"\n"::utf8, rest::binary>>), do: ["&#10;" | xml_escape_string(rest)]
defp xml_escape_string(<<"\r\n"::utf8, rest::binary>>), do: ["&#10;" | xml_escape_string(rest)]
defp xml_escape_string(<<"\r"::utf8, rest::binary>>), do: ["&#13;" | xml_escape_string(rest)]
defp xml_escape_string(<<c::utf8, rest::binary>>), do: [c | xml_escape_string(rest)]
defp xml_escape_entity(<<"amp;"::utf8, rest::binary>>), do: ["&amp;" | xml_escape_string(rest)]
defp xml_escape_entity(<<"lt;"::utf8, rest::binary>>), do: ["&lt;" | xml_escape_string(rest)]
defp xml_escape_entity(<<"gt;"::utf8, rest::binary>>), do: ["&gt;" | xml_escape_string(rest)]
defp xml_escape_entity(<<"quot;"::utf8, rest::binary>>), do: ["&quot;" | xml_escape_string(rest)]
defp xml_escape_entity(<<"apos;"::utf8, rest::binary>>), do: ["&apos;" | xml_escape_string(rest)]
defp xml_escape_entity(rest), do: ["&amp;" | xml_escape_string(rest)]
end end

View file

@ -4,8 +4,8 @@
<id><%= @data["id"] %></id> <id><%= @data["id"] %></id>
<title><%= activity_title(@data, Keyword.get(@feed_config, :post_title, %{})) %></title> <title><%= activity_title(@data, Keyword.get(@feed_config, :post_title, %{})) %></title>
<content type="html"><%= activity_content(@data) %></content> <content type="html"><%= activity_content(@data) %></content>
<published><%= @activity.data["published"] %></published> <published><%= pub_date(@activity.data["published"], "{RFC3339z}") %></published>
<updated><%= @activity.data["published"] %></updated> <updated><%= pub_date(@activity.data["published"], "{RFC3339z}") %></updated>
<ostatus:conversation ref="<%= activity_context(@activity) %>"> <ostatus:conversation ref="<%= activity_context(@activity) %>">
<%= activity_context(@activity) %> <%= activity_context(@activity) %>
</ostatus:conversation> </ostatus:conversation>
@ -16,10 +16,10 @@
<% end %> <% end %>
<%= if @activity.local do %> <%= if @activity.local do %>
<link type="application/atom+xml" href='<%= @data["id"] %>' rel="self"/> <link type="application/atom+xml" href="<%= @data["id"] %>" rel="self"/>
<link type="text/html" href='<%= @data["id"] %>' rel="alternate"/> <link type="text/html" href="<%= @data["id"] %>" rel="alternate"/>
<% else %> <% else %>
<link type="text/html" href='<%= @data["external_url"] %>' rel="alternate"/> <link type="text/html" href="<%= @data["external_url"] %>" rel="alternate"/>
<% end %> <% end %>
<%= for tag <- Pleroma.Object.hashtags(@object) do %> <%= for tag <- Pleroma.Object.hashtags(@object) do %>
@ -31,7 +31,7 @@
<% end %> <% end %>
<%= if @data["inReplyTo"] do %> <%= if @data["inReplyTo"] do %>
<thr:in-reply-to ref='<%= @data["inReplyTo"] %>' href='<%= get_href(@data["inReplyTo"]) %>'/> <thr:in-reply-to ref="<%= @data["inReplyTo"] %>" href="<%= get_href(@data["inReplyTo"]) %>"/>
<% end %> <% end %>
<%= for id <- @activity.recipients do %> <%= for id <- @activity.recipients do %>

View file

@ -4,14 +4,17 @@
<guid><%= @data["id"] %></guid> <guid><%= @data["id"] %></guid>
<title><%= activity_title(@data, Keyword.get(@feed_config, :post_title, %{})) %></title> <title><%= activity_title(@data, Keyword.get(@feed_config, :post_title, %{})) %></title>
<description><%= activity_content(@data) %></description> <description><%= activity_content(@data) %></description>
<pubDate><%= @activity.data["published"] %></pubDate> <pubDate><%= pub_date(@activity.data["published"], "{RFC822z}") %></pubDate>
<updated><%= @activity.data["published"] %></updated>
<ostatus:conversation ref="<%= activity_context(@activity) %>"> <ostatus:conversation ref="<%= activity_context(@activity) %>">
<%= activity_context(@activity) %> <%= activity_context(@activity) %>
</ostatus:conversation> </ostatus:conversation>
<%= if @data["summary"] do %> <%= if @data["summary"] do %>
<description><%= escape(@data["summary"]) %></description> <atom:summary><%= escape(@data["summary"]) %></atom:summary>
<% end %>
<%= if get_in(@data, ["source", "mediaType"]) == "text/markdown" do %>
<source:markdown><%= escape(get_in(@data, ["source", "content"])) %></source:markdown>
<% end %> <% end %>
<%= if @activity.local do %> <%= if @activity.local do %>
@ -20,26 +23,26 @@
<link><%= @data["external_url"] %></link> <link><%= @data["external_url"] %></link>
<% end %> <% end %>
<link rel="ostatus:conversation"><%= activity_context(@activity) %></link> <atom:link rel="ostatus:conversation" href="<%= activity_context(@activity) %>"/>
<%= for tag <- Pleroma.Object.hashtags(@object) do %> <%= for tag <- Pleroma.Object.hashtags(@object) do %>
<category term="<%= tag %>"></category> <category term="<%= tag %>"></category>
<% end %> <% end %>
<%= for attachment <- @data["attachment"] || [] do %> <%= for attachment <- @data["attachment"] || [] do %>
<link type="<%= attachment_type(attachment) %>"><%= attachment_href(attachment) %></link> <atom:link type="<%= attachment_type(attachment) %>" href="<%= attachment_href(attachment) %>"/>
<% end %> <% end %>
<%= if @data["inReplyTo"] do %> <%= if @data["inReplyTo"] do %>
<thr:in-reply-to ref='<%= @data["inReplyTo"] %>' href='<%= get_href(@data["inReplyTo"]) %>'/> <thr:in-reply-to ref="<%= @data["inReplyTo"] %>" href="<%= get_href(@data["inReplyTo"]) %>"/>
<% end %> <% end %>
<%= for id <- @activity.recipients do %> <%= for id <- @activity.recipients do %>
<%= if id == Pleroma.Constants.as_public() do %> <%= if id == Pleroma.Constants.as_public() do %>
<link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection">http://activityschema.org/collection/public</link> <atom:link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
<% else %> <% else %>
<%= unless Regex.match?(~r/^#{Pleroma.Web.Endpoint.url()}.+followers$/, id) do %> <%= unless Regex.match?(~r/^#{Pleroma.Web.Endpoint.url()}.+followers$/, id) do %>
<link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person"><%= id %></link> <atom:link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="<%= id %>"/>
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>

View file

@ -1,17 +1,18 @@
<author> <author>
<id><%= @user.ap_id %></id> <name><%= @user.nickname %></name>
<activity:object>http://activitystrea.ms/schema/1.0/person</activity:object>
<uri><%= @user.ap_id %></uri> <uri><%= @user.ap_id %></uri>
<email><%= @user.email %></email>
<activity:object>http://activitystrea.ms/schema/1.0/person</activity:object>
<poco:preferredUsername><%= @user.nickname %></poco:preferredUsername> <poco:preferredUsername><%= @user.nickname %></poco:preferredUsername>
<poco:displayName><%= @user.name %></poco:displayName> <poco:displayName><%= @user.name %></poco:displayName>
<poco:note><%= escape(@user.bio) %></poco:note> <%= if @user.bio do %>
<summary><%= escape(@user.bio) %></summary> <poco:note><%= escape(@user.bio) %></poco:note>
<name><%= @user.nickname %></name> <% end %>
<link rel="avatar" href="<%= User.avatar_url(@user) %>"/> <local:link rel="avatar" href="<%= User.avatar_url(@user) %>"/>
<%= if User.banner_url(@user) do %> <%= if User.banner_url(@user) do %>
<link rel="header" href="<%= User.banner_url(@user) %>"/> <local:link rel="header" href="<%= User.banner_url(@user) %>"/>
<% end %> <% end %>
<%= if @user.local do %> <%= if @user.local do %>
<ap_enabled>true</ap_enabled> <ap:ap_enabled>true</ap:ap_enabled>
<% end %> <% end %>
</author> </author>

View file

@ -1,17 +1,19 @@
<managingEditor> <managingEditor><%= @user.email %></managingEditor>
<guid><%= @user.ap_id %></guid> <atom:author>
<atom:name><%= @user.nickname %></atom:name>
<atom:uri><%= @user.ap_id %></atom:uri>
<atom:email><%= @user.email %></atom:email>
<activity:object>http://activitystrea.ms/schema/1.0/person</activity:object> <activity:object>http://activitystrea.ms/schema/1.0/person</activity:object>
<uri><%= @user.ap_id %></uri>
<poco:preferredUsername><%= @user.nickname %></poco:preferredUsername> <poco:preferredUsername><%= @user.nickname %></poco:preferredUsername>
<poco:displayName><%= @user.name %></poco:displayName> <poco:displayName><%= @user.name %></poco:displayName>
<poco:note><%= escape(@user.bio) %></poco:note> <%= if @user.bio do %>
<description><%= escape(@user.bio) %></description> <poco:note><%= escape(@user.bio) %></poco:note>
<name><%= @user.nickname %></name> <% end %>
<link rel="avatar"><%= User.avatar_url(@user) %></link> <atom:link rel="avatar" href="<%= User.avatar_url(@user) %>"/>
<%= if User.banner_url(@user) do %> <%= if User.banner_url(@user) do %>
<link rel="header"><%= User.banner_url(@user) %></link> <atom:link rel="header" href="<%= User.banner_url(@user) %>"/>
<% end %> <% end %>
<%= if @user.local do %> <%= if @user.local do %>
<ap_enabled>true</ap_enabled> <ap:ap_enabled>true</ap:ap_enabled>
<% end %> <% end %>
</managingEditor> </atom:author>

View file

@ -9,14 +9,14 @@
<content type="html"><%= activity_content(@data) %></content> <content type="html"><%= activity_content(@data) %></content>
<%= if @activity.local do %> <%= if @activity.local do %>
<link type="application/atom+xml" href='<%= @data["id"] %>' rel="self"/> <link type="application/atom+xml" href="<%= @data["id"] %>" rel="self"/>
<link type="text/html" href='<%= @data["id"] %>' rel="alternate"/> <link type="text/html" href="<%= @data["id"] %>" rel="alternate"/>
<% else %> <% else %>
<link type="text/html" href='<%= @data["external_url"] %>' rel="alternate"/> <link type="text/html" href="<%= @data["external_url"] %>" rel="alternate"/>
<% end %> <% end %>
<published><%= @activity.data["published"] %></published> <published><%= pub_date(@activity.data["published"], "{RFC3339z}") %></published>
<updated><%= @activity.data["published"] %></updated> <updated><%= pub_date(@activity.data["published"], "{RFC3339z}") %></updated>
<ostatus:conversation ref="<%= activity_context(@activity) %>"> <ostatus:conversation ref="<%= activity_context(@activity) %>">
<%= activity_context(@activity) %> <%= activity_context(@activity) %>

View file

@ -4,7 +4,7 @@
<guid isPermalink="true"><%= activity_context(@activity) %></guid> <guid isPermalink="true"><%= activity_context(@activity) %></guid>
<link><%= activity_context(@activity) %></link> <link><%= activity_context(@activity) %></link>
<pubDate><%= pub_date(@activity.data["published"]) %></pubDate> <pubDate><%= pub_date(@activity.data["published"], "{RFC822z}") %></pubDate>
<description><%= activity_content(@data) %></description> <description><%= activity_content(@data) %></description>
<%= for attachment <- @data["attachment"] || [] do %> <%= for attachment <- @data["attachment"] || [] do %>

View file

@ -9,13 +9,13 @@
xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0"
xmlns:statusnet="http://status.net/schema/api/1/"> xmlns:statusnet="http://status.net/schema/api/1/">
<id><%= '#{Routes.tag_feed_url(@conn, :feed, @tag)}.rss' %></id> <id><%= Routes.tag_feed_url(@conn, :feed, @tag) <> ".atom" %></id>
<title>#<%= @tag %></title> <title>#<%= @tag %></title>
<subtitle><%= Gettext.dpgettext("static_pages", "tag feed description", "These are public toots tagged with #%{tag}. You can interact with them if you have an account anywhere in the fediverse.", tag: @tag) %></subtitle> <subtitle><%= Gettext.dpgettext("static_pages", "tag feed description", "These are public toots tagged with #%{tag}. You can interact with them if you have an account anywhere in the fediverse.", tag: @tag) %></subtitle>
<logo><%= feed_logo() %></logo> <logo><%= feed_logo() %></logo>
<updated><%= most_recent_update(@activities) %></updated> <updated><%= most_recent_update(@activities, "{RFC3339z}") %></updated>
<link rel="self" href="<%= '#{Routes.tag_feed_url(@conn, :feed, @tag)}.atom' %>" type="application/atom+xml"/> <link rel="self" href="<%= Routes.tag_feed_url(@conn, :feed, @tag) <> ".atom" %>" type="application/atom+xml"/>
<%= for activity <- @activities do %> <%= for activity <- @activities do %>
<%= render @view_module, "_tag_activity.atom", Map.merge(assigns, prepare_activity(activity, actor: true)) %> <%= render @view_module, "_tag_activity.atom", Map.merge(assigns, prepare_activity(activity, actor: true)) %>
<% end %> <% end %>

View file

@ -5,7 +5,7 @@
<title>#<%= @tag %></title> <title>#<%= @tag %></title>
<description><%= Gettext.dpgettext("static_pages", "tag feed description", "These are public toots tagged with #%{tag}. You can interact with them if you have an account anywhere in the fediverse.", tag: @tag) %></description> <description><%= Gettext.dpgettext("static_pages", "tag feed description", "These are public toots tagged with #%{tag}. You can interact with them if you have an account anywhere in the fediverse.", tag: @tag) %></description>
<link><%= '#{Routes.tag_feed_url(@conn, :feed, @tag)}.rss' %></link> <link><%= "#{Routes.tag_feed_url(@conn, :feed, @tag)}.rss" %></link>
<webfeeds:logo><%= feed_logo() %></webfeeds:logo> <webfeeds:logo><%= feed_logo() %></webfeeds:logo>
<webfeeds:accentColor>2b90d9</webfeeds:accentColor> <webfeeds:accentColor>2b90d9</webfeeds:accentColor>
<%= for activity <- @activities do %> <%= for activity <- @activities do %>

View file

@ -1,21 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<feed <feed
xmlns="http://www.w3.org/2005/Atom" xmlns="http://www.w3.org/2005/Atom"
xmlns:rss="https://www.rssboard.org/rss-specification"
xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:thr="http://purl.org/syndication/thread/1.0"
xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:activity="http://activitystrea.ms/spec/1.0/"
xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:poco="http://portablecontacts.net/spec/1.0"
xmlns:ostatus="http://ostatus.org/schema/1.0"> xmlns:ostatus="http://ostatus.org/schema/1.0"
xmlns:ap="https://www.w3.org/TR/activitypub/">
<id><%= Routes.user_feed_url(@conn, :feed, @user.nickname) <> ".atom" %></id> <id><%= Routes.user_feed_url(@conn, :feed, @user.nickname) <> ".atom" %></id>
<title><%= @user.nickname <> "'s timeline" %></title> <title><%= @user.nickname <> "'s timeline" %></title>
<updated><%= most_recent_update(@activities, @user) %></updated> <updated><%= most_recent_update(@activities, @user, "{RFC3339z}") %></updated>
<logo><%= logo(@user) %></logo> <logo><%= logo(@user) %></logo>
<link rel="self" href="<%= '#{Routes.user_feed_url(@conn, :feed, @user.nickname)}.atom' %>" type="application/atom+xml"/> <link rel="self" href="<%= Routes.user_feed_url(@conn, :feed, @user.nickname) <> ".atom" %>" type="application/atom+xml"/>
<%= render @view_module, "_author.atom", assigns %> <%= render @view_module, "_author.atom", assigns %>
<%= if last_activity(@activities) do %> <%= if last_activity(@activities) do %>
<link rel="next" href="<%= '#{Routes.user_feed_url(@conn, :feed, @user.nickname)}.atom?max_id=#{last_activity(@activities).id}' %>" type="application/atom+xml"/> <link rel="next" href="<%= Routes.user_feed_url(@conn, :feed, @user.nickname) <> ".atom?max_id=#{last_activity(@activities).id}" %>" type="application/atom+xml"/>
<% end %> <% end %>
<%= for activity <- @activities do %> <%= for activity <- @activities do %>

View file

@ -1,18 +1,28 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"> <rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:thr="http://purl.org/syndication/thread/1.0"
xmlns:activity="http://activitystrea.ms/spec/1.0/"
xmlns:poco="http://portablecontacts.net/spec/1.0"
xmlns:ostatus="http://ostatus.org/schema/1.0"
xmlns:source="https://github.com/scripting/tweetFeedSupport"
xmlns:ap="https://www.w3.org/TR/activitypub/">
<channel> <channel>
<guid><%= Routes.user_feed_url(@conn, :feed, @user.nickname) <> ".rss" %></guid>
<title><%= @user.nickname <> "'s timeline" %></title> <title><%= @user.nickname <> "'s timeline" %></title>
<updated><%= most_recent_update(@activities, @user) %></updated> <description>Publicly visible items published by <%= @user.nickname %></description>
<image><%= logo(@user) %></image> <link><%= Routes.user_feed_url(@conn, :feed, @user.nickname) <> ".rss" %></link>
<link><%= '#{Routes.user_feed_url(@conn, :feed, @user.nickname)}.rss' %></link> <lastBuildDate><%= most_recent_update(@activities, @user, "{RFC822z}") %></lastBuildDate>
<image>
<link><%= logo(@user) %></link>
<title><%= @user.nickname <> "'s avatar" %></title>
<url><%= logo(@user) %></url>
</image>
<%= render @view_module, "_author.rss", assigns %> <%= render @view_module, "_author.rss", assigns %>
<%= if last_activity(@activities) do %> <%= if last_activity(@activities) do %>
<link rel="next"><%= '#{Routes.user_feed_url(@conn, :feed, @user.nickname)}.rss?max_id=#{last_activity(@activities).id}' %></link> <atom:link rel="next" href="<%= Routes.user_feed_url(@conn, :feed, @user.nickname) <> ".rss?max_id=#{last_activity(@activities).id}" %>"/>
<% end %> <% end %>
<%= for activity <- @activities do %> <%= for activity <- @activities do %>
<%= render @view_module, "_activity.rss", Map.merge(assigns, prepare_activity(activity)) %> <%= render @view_module, "_activity.rss", Map.merge(assigns, prepare_activity(activity)) %>
<% end %> <% end %>

View file

@ -138,8 +138,8 @@ defmodule Pleroma.Web.Feed.TagControllerTest do
] ]
assert xpath(xml, ~x"//channel/item/pubDate/text()"sl) == [ assert xpath(xml, ~x"//channel/item/pubDate/text()"sl) == [
FeedView.pub_date(activity2.data["published"]), FeedView.pub_date(activity2.data["published"], "{RFC822z}"),
FeedView.pub_date(activity1.data["published"]) FeedView.pub_date(activity1.data["published"], "{RFC822z}")
] ]
assert xpath(xml, ~x"//channel/item/enclosure/@url"sl) == [ assert xpath(xml, ~x"//channel/item/enclosure/@url"sl) == [