add subject to atom feed
authorMaksim Pechnikov <parallel588@gmail.com>
Fri, 8 Nov 2019 06:23:24 +0000 (09:23 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Sun, 10 Nov 2019 08:10:20 +0000 (11:10 +0300)
CHANGELOG.md
config/config.exs
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/feed/feed_controller.ex
lib/pleroma/web/feed/feed_view.ex
lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex
lib/pleroma/web/templates/feed/feed/_activity.xml.eex
lib/pleroma/web/templates/feed/feed/feed.xml.eex
test/web/activity_pub/activity_pub_test.exs
test/web/feed/feed_controller_test.exs

index b33d618193c755fe054ed98b65fe20d8a2e7cd01..1eb7d14cde2dcc10d0f25b79a89fa08a325fdfcb 100644 (file)
@@ -58,6 +58,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Pleroma API: `POST /api/v1/pleroma/conversations/read` to mark all conversations as read
 - Mastodon API: Add `/api/v1/markers` for managing timeline read markers
 - Mastodon API: Add the `recipients` parameter to `GET /api/v1/conversations`
+- Configuration: `feed` option for user atom feed.
 </details>
 
 ### Fixed
index 787809b271dc82435e14c167d89b468e23e0a577..7e1fe2a81f8856fc3ea163248c350c625bc5a5a4 100644 (file)
@@ -276,6 +276,12 @@ config :pleroma, :instance,
   external_user_synchronization: true,
   extended_nickname_format: false
 
+config :pleroma, :feed,
+  post_title: %{
+    max_length: 100,
+    omission: "..."
+  }
+
 config :pleroma, :markup,
   # XXX - unfortunately, inline images must be enabled by default right now, because
   # of custom emoji.  Issue #275 discusses defanging that somehow.
index 51a9c61691b8471972ec306292c8c5064a6faac1..65dd251f3ea3ddb62ab6b5fa2460e41db288e162 100644 (file)
@@ -568,7 +568,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     |> fetch_activities_query(opts)
     |> restrict_unlisted()
     |> Pagination.fetch_paginated(opts, pagination)
-    |> Enum.reverse()
   end
 
   @valid_visibilities ~w[direct unlisted public private]
index d91ecef9c4c1d827536323d3dacf2efb534fa6d3..d0e23007df7610544587136b0a86a69aa130f6f3 100644 (file)
@@ -33,21 +33,22 @@ defmodule Pleroma.Web.Feed.FeedController do
 
   def feed(conn, %{"nickname" => nickname} = params) do
     with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)} do
-      query_params =
-        params
-        |> Map.take(["max_id"])
-        |> Map.put("type", ["Create"])
-        |> Map.put("whole_db", true)
-        |> Map.put("actor_id", user.ap_id)
-
       activities =
-        query_params
+        %{
+          "type" => ["Create"],
+          "whole_db" => true,
+          "actor_id" => user.ap_id
+        }
+        |> Map.merge(Map.take(params, ["max_id"]))
         |> ActivityPub.fetch_public_activities()
-        |> Enum.reverse()
 
       conn
       |> put_resp_content_type("application/atom+xml")
-      |> render("feed.xml", user: user, activities: activities)
+      |> render("feed.xml",
+        user: user,
+        activities: activities,
+        feed_config: Pleroma.Config.get([:feed])
+      )
     end
   end
 
index 5eef1e7574478d1a8629874f41f2ee1e79d5acb4..bb1332fd306be715e2bb8f1073f833a0320f5de0 100644 (file)
@@ -6,12 +6,23 @@ defmodule Pleroma.Web.Feed.FeedView do
   use Phoenix.HTML
   use Pleroma.Web, :view
 
+  alias Pleroma.Formatter
   alias Pleroma.Object
   alias Pleroma.User
   alias Pleroma.Web.MediaProxy
 
   require Pleroma.Constants
 
+  def prepare_activity(activity) do
+    object = activity_object(activity)
+
+    %{
+      activity: activity,
+      data: Map.get(object, :data),
+      object: object
+    }
+  end
+
   def most_recent_update(activities, user) do
     (List.first(activities) || user).updated_at
     |> NaiveDateTime.to_iso8601()
@@ -23,31 +34,23 @@ defmodule Pleroma.Web.Feed.FeedView do
     |> MediaProxy.url()
   end
 
-  def last_activity(activities) do
-    List.last(activities)
-  end
+  def last_activity(activities), do: List.last(activities)
 
-  def activity_object(activity) do
-    Object.normalize(activity)
-  end
+  def activity_object(activity), do: Object.normalize(activity)
 
-  def activity_object_data(activity) do
-    activity
-    |> activity_object()
-    |> Map.get(:data)
+  def activity_title(%{data: %{"content" => content}}, opts \\ %{}) do
+    content
+    |> Formatter.truncate(opts[:max_length], opts[:omission])
+    |> escape()
   end
 
-  def activity_content(activity) do
-    content = activity_object_data(activity)["content"]
-
+  def activity_content(%{data: %{"content" => content}}) do
     content
     |> String.replace(~r/[\n\r]/, "")
     |> escape()
   end
 
-  def activity_context(activity) do
-    activity.data["context"]
-  end
+  def activity_context(activity), do: activity.data["context"]
 
   def attachment_href(attachment) do
     attachment["url"]
index f2d2d3ccb2587e7c22320beb8cdee1434c8678db..384159336c5a46e9572f95e4cce6cb8bce25e5f5 100644 (file)
@@ -71,7 +71,6 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
       |> Map.put("blocking_user", user)
       |> Map.put("muting_user", user)
       |> ActivityPub.fetch_public_activities()
-      |> Enum.reverse()
 
     conn
     |> add_link_headers(activities, %{"local" => local_only})
@@ -110,7 +109,6 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
       |> Map.put("tag_all", tag_all)
       |> Map.put("tag_reject", tag_reject)
       |> ActivityPub.fetch_public_activities()
-      |> Enum.reverse()
 
     conn
     |> add_link_headers(activities, %{"local" => local_only})
index d1f5e903ccf54cffb4f3afe297e4045186d9704a..514eacaed807c3bc94d6ae184890d6920dde55df 100644 (file)
@@ -2,11 +2,13 @@
   <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
   <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
   <id><%= @data["id"] %></id>
-  <title><%= "New note by #{@user.nickname}" %></title>
-  <content type="html"><%= activity_content(@activity) %></content>
+  <title><%= activity_title(@object, Keyword.get(@feed_config, :post_title, %{})) %></title>
+  <content type="html"><%= activity_content(@object) %></content>
   <published><%= @data["published"] %></published>
   <updated><%= @data["published"] %></updated>
-  <ostatus:conversation ref="<%= activity_context(@activity) %>"><%= activity_context(@activity) %></ostatus:conversation>
+  <ostatus:conversation ref="<%= activity_context(@activity) %>">
+    <%= activity_context(@activity) %>
+  </ostatus:conversation>
   <link ref="<%= activity_context(@activity) %>" rel="ostatus:conversation"/>
 
   <%= if @data["summary"] do %>
index 45df9dc09a20594a721d7020e835df2cb0098ba4..5ae36d345d79bc88e663eeeab60d3dc634fc9082 100644 (file)
@@ -19,6 +19,6 @@
   <% end %>
 
   <%= for activity <- @activities do %>
-    <%= render @view_module, "_activity.xml", Map.merge(assigns, %{activity: activity, data: activity_object_data(activity)}) %>
+  <%= render @view_module, "_activity.xml", Map.merge(assigns, prepare_activity(activity)) %>
   <% end %>
 </feed>
index f29b8cc74985ade822aab429fd3d67a2772f4a35..0d0281fafeeb4ee333f5b9b42ed2878bc5c360f3 100644 (file)
@@ -734,56 +734,54 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
 
     test "retrieves a maximum of 20 activities" do
-      activities = ActivityBuilder.insert_list(30)
-      last_expected = List.last(activities)
+      ActivityBuilder.insert_list(10)
+      expected_activities = ActivityBuilder.insert_list(20)
 
       activities = ActivityPub.fetch_public_activities()
-      last = List.last(activities)
 
+      assert collect_ids(activities) == collect_ids(expected_activities)
       assert length(activities) == 20
-      assert last == last_expected
     end
 
     test "retrieves ids starting from a since_id" do
       activities = ActivityBuilder.insert_list(30)
-      later_activities = ActivityBuilder.insert_list(10)
+      expected_activities = ActivityBuilder.insert_list(10)
       since_id = List.last(activities).id
-      last_expected = List.last(later_activities)
 
       activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
-      last = List.last(activities)
 
+      assert collect_ids(activities) == collect_ids(expected_activities)
       assert length(activities) == 10
-      assert last == last_expected
     end
 
     test "retrieves ids up to max_id" do
-      _first_activities = ActivityBuilder.insert_list(10)
-      activities = ActivityBuilder.insert_list(20)
-      later_activities = ActivityBuilder.insert_list(10)
-      max_id = List.first(later_activities).id
-      last_expected = List.last(activities)
+      ActivityBuilder.insert_list(10)
+      expected_activities = ActivityBuilder.insert_list(20)
+
+      %{id: max_id} =
+        10
+        |> ActivityBuilder.insert_list()
+        |> List.first()
 
       activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
-      last = List.last(activities)
 
       assert length(activities) == 20
-      assert last == last_expected
+      assert collect_ids(activities) == collect_ids(expected_activities)
     end
 
     test "paginates via offset/limit" do
-      _first_activities = ActivityBuilder.insert_list(10)
-      activities = ActivityBuilder.insert_list(10)
-      _later_activities = ActivityBuilder.insert_list(10)
-      first_expected = List.first(activities)
+      _first_part_activities = ActivityBuilder.insert_list(10)
+      second_part_activities = ActivityBuilder.insert_list(10)
+
+      later_activities = ActivityBuilder.insert_list(10)
 
       activities =
         ActivityPub.fetch_public_activities(%{"page" => "2", "page_size" => "20"}, :offset)
 
-      first = List.first(activities)
-
       assert length(activities) == 20
-      assert first == first_expected
+
+      assert collect_ids(activities) ==
+               collect_ids(second_part_activities) ++ collect_ids(later_activities)
     end
 
     test "doesn't return reblogs for users for whom reblogs have been muted" do
index 1f44eae20bdec2d4507cd33973adb5c7e23219ba..6f61acf43b2d4b6303a1f673ca0074d20e650d06 100644 (file)
@@ -6,16 +6,25 @@ defmodule Pleroma.Web.Feed.FeedControllerTest do
   use Pleroma.Web.ConnCase
 
   import Pleroma.Factory
+  import SweetXml
 
   alias Pleroma.Object
   alias Pleroma.User
 
+  clear_config([:feed])
+
   test "gets a 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"}]
@@ -26,15 +35,30 @@ defmodule Pleroma.Web.Feed.FeedControllerTest do
       )
 
     note_activity = insert(:note_activity, note: note)
-    object = Object.normalize(note_activity)
     user = User.get_cached_by_ap_id(note_activity.data["actor"])
 
-    conn =
+    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("content-type", "application/atom+xml")
       |> get("/users/#{user.nickname}/feed.atom")
+      |> response(200)
+
+    activity_titles =
+      resp
+      |> SweetXml.parse()
+      |> SweetXml.xpath(~x"//entry/title/text()"l)
 
-    assert response(conn, 200) =~ object.data["content"]
+    assert activity_titles == ['42 This...', 'This is...']
+    assert resp =~ object.data["content"]
   end
 
   test "returns 404 for a missing feed", %{conn: conn} do