adding rss for user feed
authorAlexander Strizhakov <alex.strizhakov@gmail.com>
Fri, 13 Mar 2020 14:41:26 +0000 (17:41 +0300)
committerAlexander Strizhakov <alex.strizhakov@gmail.com>
Mon, 16 Mar 2020 09:45:31 +0000 (12:45 +0300)
lib/pleroma/web/feed/user_controller.ex
lib/pleroma/web/router.ex
lib/pleroma/web/templates/feed/feed/_activity.atom.eex [moved from lib/pleroma/web/templates/feed/feed/_activity.xml.eex with 100% similarity]
lib/pleroma/web/templates/feed/feed/_activity.rss.eex [new file with mode: 0644]
lib/pleroma/web/templates/feed/feed/_author.atom.eex [moved from lib/pleroma/web/templates/feed/feed/_author.xml.eex with 100% similarity]
lib/pleroma/web/templates/feed/feed/_author.rss.eex [new file with mode: 0644]
lib/pleroma/web/templates/feed/feed/user.atom.eex [moved from lib/pleroma/web/templates/feed/feed/user.xml.eex with 85% similarity]
lib/pleroma/web/templates/feed/feed/user.rss.eex [new file with mode: 0644]
test/web/feed/user_controller_test.exs

index 9ffb3b9be4eafa6cac7e824c062c4f8a371d14b3..e27f859299dbc9f3e47dc83601d2bba6b9df7ee6 100644 (file)
@@ -40,6 +40,15 @@ defmodule Pleroma.Web.Feed.UserController do
   end
 
   def feed(conn, %{"nickname" => nickname} = params) do
+    format = get_format(conn)
+
+    format =
+      if format in ["rss", "atom"] do
+        format
+      else
+        "atom"
+      end
+
     with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)} do
       activities =
         %{
@@ -50,9 +59,9 @@ defmodule Pleroma.Web.Feed.UserController do
         |> ActivityPub.fetch_public_activities()
 
       conn
-      |> put_resp_content_type("application/atom+xml")
+      |> put_resp_content_type("application/#{format}+xml")
       |> put_view(FeedView)
-      |> render("user.xml",
+      |> render("user.#{format}",
         user: user,
         activities: activities,
         feed_config: Pleroma.Config.get([:feed])
index e4e3ee704eac2705cfa3d1cdad6baa50455e3e1e..3f36f6c1a8d000b7e3f7de3f554a77dd02a2888d 100644 (file)
@@ -513,7 +513,7 @@ defmodule Pleroma.Web.Router do
   end
 
   pipeline :ostatus do
-    plug(:accepts, ["html", "xml", "atom", "activity+json", "json"])
+    plug(:accepts, ["html", "xml", "rss", "atom", "activity+json", "json"])
     plug(Pleroma.Plugs.StaticFEPlug)
   end
 
diff --git a/lib/pleroma/web/templates/feed/feed/_activity.rss.eex b/lib/pleroma/web/templates/feed/feed/_activity.rss.eex
new file mode 100644 (file)
index 0000000..a4dbed6
--- /dev/null
@@ -0,0 +1,49 @@
+<item>
+  <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
+  <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
+  <guid><%= @data["id"] %></guid>
+  <title><%= activity_title(@object, Keyword.get(@feed_config, :post_title, %{})) %></title>
+  <description><%= activity_content(@object) %></description>
+  <pubDate><%= @data["published"] %></pubDate>
+  <updated><%= @data["published"] %></updated>
+  <ostatus:conversation ref="<%= activity_context(@activity) %>">
+    <%= activity_context(@activity) %>
+  </ostatus:conversation>
+  <link rel="ostatus:conversation"><%= activity_context(@activity) %></link>
+
+  <%= if @data["summary"] do %>
+    <description><%= @data["summary"] %></description>
+  <% end %>
+
+  <%= if @activity.local do %>
+    <link><%= @data["id"] %></link>
+  <% else %>
+    <link><%= @data["external_url"] %></link>
+  <% end %>
+
+  <%= for tag <- @data["tag"] || [] do %>
+    <category term="<%= tag %>"></category>
+  <% end %>
+
+  <%= for attachment <- @data["attachment"] || [] do %>
+    <link type="<%= attachment_type(attachment) %>"><%= attachment_href(attachment) %></link>
+  <% end %>
+
+  <%= if @data["inReplyTo"] do %>
+    <thr:in-reply-to ref='<%= @data["inReplyTo"] %>' href='<%= get_href(@data["inReplyTo"]) %>'/>
+  <% end %>
+
+  <%= for id <- @activity.recipients 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>
+    <% else %>
+      <%= unless Regex.match?(~r/^#{Pleroma.Web.base_url()}.+followers$/, id) do %>
+        <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person"><%= id %></link>
+      <% end %>
+    <% end %>
+  <% end %>
+
+  <%= for {emoji, file} <- @data["emoji"] || %{} do %>
+    <link name="<%= emoji %>" rel="emoji"><%= file %></link>
+  <% end %>
+</item>
diff --git a/lib/pleroma/web/templates/feed/feed/_author.rss.eex b/lib/pleroma/web/templates/feed/feed/_author.rss.eex
new file mode 100644 (file)
index 0000000..526aedd
--- /dev/null
@@ -0,0 +1,17 @@
+<managingEditor>
+  <guid><%= @user.ap_id %></guid>
+  <activity:object>http://activitystrea.ms/schema/1.0/person</activity:object>
+  <uri><%= @user.ap_id %></uri>
+  <poco:preferredUsername><%= @user.nickname %></poco:preferredUsername>
+  <poco:displayName><%= @user.name %></poco:displayName>
+  <poco:note><%= escape(@user.bio) %></poco:note>
+  <description><%= escape(@user.bio) %></description>
+  <name><%= @user.nickname %></name>
+  <link rel="avatar"><%= User.avatar_url(@user) %></link>
+  <%= if User.banner_url(@user) do %>
+    <link rel="header"><%= User.banner_url(@user) %></link>
+  <% end %>
+  <%= if @user.local do %>
+    <ap_enabled>true</ap_enabled>
+  <% end %>
+</managingEditor>
similarity index 85%
rename from lib/pleroma/web/templates/feed/feed/user.xml.eex
rename to lib/pleroma/web/templates/feed/feed/user.atom.eex
index d274c08ae06d1b5082e2ff3bb3ad6215b3400ac4..c6acd848f2efd07907170b6e8aa7e320fbb9e16a 100644 (file)
   <logo><%= logo(@user) %></logo>
   <link rel="self" href="<%= '#{user_feed_url(@conn, :feed, @user.nickname)}.atom' %>" type="application/atom+xml"/>
 
-  <%= render @view_module, "_author.xml", assigns %>
+  <%= render @view_module, "_author.atom", assigns %>
 
   <%= if last_activity(@activities) do %>
     <link rel="next" href="<%= '#{user_feed_url(@conn, :feed, @user.nickname)}.atom?max_id=#{last_activity(@activities).id}' %>" type="application/atom+xml"/>
   <% end %>
 
   <%= for activity <- @activities do %>
-  <%= render @view_module, "_activity.xml", Map.merge(assigns, prepare_activity(activity)) %>
+  <%= render @view_module, "_activity.atom", Map.merge(assigns, prepare_activity(activity)) %>
   <% end %>
 </feed>
diff --git a/lib/pleroma/web/templates/feed/feed/user.rss.eex b/lib/pleroma/web/templates/feed/feed/user.rss.eex
new file mode 100644 (file)
index 0000000..d691204
--- /dev/null
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<rss version="2.0">
+  <channel>
+    <guid><%= user_feed_url(@conn, :feed, @user.nickname) <> ".rss" %></guid>
+    <title><%= @user.nickname <> "'s timeline" %></title>
+    <updated><%= most_recent_update(@activities, @user) %></updated>
+    <image><%= logo(@user) %></image>
+    <link><%= '#{user_feed_url(@conn, :feed, @user.nickname)}.rss' %></link>
+
+    <%= render @view_module, "_author.rss", assigns %>
+
+    <%= if last_activity(@activities) do %>
+      <link rel="next"><%= '#{user_feed_url(@conn, :feed, @user.nickname)}.rss?max_id=#{last_activity(@activities).id}' %></link>
+    <% end %>
+
+    <%= for activity <- @activities do %>
+    <%= render @view_module, "_activity.rss", Map.merge(assigns, prepare_activity(activity)) %>
+    <% end %>
+  </channel>
+</rss>
index fd59ca892fdff4b078757ba207ae2ad3d0c9ddfa..e3dfa88f19740a134f0ec2789699886841e1b179 100644 (file)
@@ -19,7 +19,7 @@ defmodule Pleroma.Web.Feed.UserControllerTest do
   describe "feed" do
     clear_config([:feed])
 
-    test "gets a feed", %{conn: conn} do
+    test "gets an atom feed", %{conn: conn} do
       Config.put(
         [:feed, :post_title],
         %{max_length: 10, omission: "..."}
@@ -59,7 +59,7 @@ defmodule Pleroma.Web.Feed.UserControllerTest do
 
       resp =
         conn
-        |> put_req_header("content-type", "application/atom+xml")
+        |> put_req_header("accept", "application/atom+xml")
         |> get(user_feed_path(conn, :feed, user.nickname))
         |> response(200)
 
@@ -73,7 +73,7 @@ defmodule Pleroma.Web.Feed.UserControllerTest do
 
       resp =
         conn
-        |> put_req_header("content-type", "application/atom+xml")
+        |> put_req_header("accept", "application/atom+xml")
         |> get("/users/#{user.nickname}/feed", %{"max_id" => note_activity2.id})
         |> response(200)
 
@@ -85,10 +85,76 @@ defmodule Pleroma.Web.Feed.UserControllerTest do
       assert activity_titles == ['This is...']
     end
 
+    test "gets a rss feed", %{conn: conn} do
+      Pleroma.Config.put(
+        [:feed, :post_title],
+        %{max_length: 10, omission: "..."}
+      )
+
+      activity = insert(:note_activity)
+
+      note =
+        insert(:note,
+          data: %{
+            "content" => "This is :moominmamma: note ",
+            "attachment" => [
+              %{
+                "url" => [
+                  %{"mediaType" => "image/png", "href" => "https://pleroma.gov/image.png"}
+                ]
+              }
+            ],
+            "inReplyTo" => activity.data["id"]
+          }
+        )
+
+      note_activity = insert(:note_activity, note: note)
+      user = User.get_cached_by_ap_id(note_activity.data["actor"])
+
+      note2 =
+        insert(:note,
+          user: user,
+          data: %{
+            "content" => "42 This is :moominmamma: note ",
+            "inReplyTo" => activity.data["id"]
+          }
+        )
+
+      note_activity2 = insert(:note_activity, note: note2)
+      object = Object.normalize(note_activity)
+
+      resp =
+        conn
+        |> put_req_header("accept", "application/rss+xml")
+        |> get("/users/#{user.nickname}/feed.rss")
+        |> response(200)
+
+      activity_titles =
+        resp
+        |> SweetXml.parse()
+        |> SweetXml.xpath(~x"//item/title/text()"l)
+
+      assert activity_titles == ['42 This...', 'This is...']
+      assert resp =~ object.data["content"]
+
+      resp =
+        conn
+        |> put_req_header("accept", "application/atom+xml")
+        |> get("/users/#{user.nickname}/feed.rss", %{"max_id" => note_activity2.id})
+        |> response(200)
+
+      activity_titles =
+        resp
+        |> SweetXml.parse()
+        |> SweetXml.xpath(~x"//item/title/text()"l)
+
+      assert activity_titles == ['This is...']
+    end
+
     test "returns 404 for a missing feed", %{conn: conn} do
       conn =
         conn
-        |> put_req_header("content-type", "application/atom+xml")
+        |> put_req_header("accept", "application/atom+xml")
         |> get(user_feed_path(conn, :feed, "nonexisting"))
 
       assert response(conn, 404)