CommonAPI.Utils.make_note_data/1 --> ActivityPub.Builder.note/1
authorAlex Gleason <alex@alexgleason.me>
Sat, 14 Aug 2021 16:01:06 +0000 (11:01 -0500)
committerAlex Gleason <alex@alexgleason.me>
Sat, 14 Aug 2021 16:01:06 +0000 (11:01 -0500)
lib/pleroma/web/activity_pub/builder.ex
lib/pleroma/web/common_api/activity_draft.ex
lib/pleroma/web/common_api/utils.ex
test/pleroma/web/activity_pub/builder_test.exs [new file with mode: 0644]
test/pleroma/web/common_api/utils_test.exs

index cde4777103d3330aa3356f604698c1f6fe79345b..3b1fa4dba1d88774b41bee2660d86fe0ead76de8 100644 (file)
@@ -15,6 +15,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do
   alias Pleroma.Web.ActivityPub.Relay
   alias Pleroma.Web.ActivityPub.Utils
   alias Pleroma.Web.ActivityPub.Visibility
+  alias Pleroma.Web.CommonAPI.ActivityDraft
 
   require Pleroma.Constants
 
@@ -125,6 +126,33 @@ defmodule Pleroma.Web.ActivityPub.Builder do
      |> Pleroma.Maps.put_if_present("context", context), []}
   end
 
+  def note(%ActivityDraft{} = draft) do
+    %{
+      "type" => "Note",
+      "to" => draft.to,
+      "cc" => draft.cc,
+      "content" => draft.content_html,
+      "summary" => draft.summary,
+      "sensitive" => draft.sensitive,
+      "context" => draft.context,
+      "attachment" => draft.attachments,
+      "actor" => draft.user.ap_id,
+      "tag" => Keyword.values(draft.tags) |> Enum.uniq()
+    }
+    |> add_in_reply_to(draft.in_reply_to)
+    |> Map.merge(draft.extra)
+  end
+
+  defp add_in_reply_to(object, nil), do: object
+
+  defp add_in_reply_to(object, in_reply_to) do
+    with %Object{} = in_reply_to_object <- Object.normalize(in_reply_to, fetch: false) do
+      Map.put(object, "inReplyTo", in_reply_to_object.data["id"])
+    else
+      _ -> object
+    end
+  end
+
   def chat_message(actor, recipient, content, opts \\ []) do
     basic = %{
       "id" => Utils.generate_object_id(),
index c691d71d28ff6ca32db11abad6dc71d7813a31a6..a5cfb3403f0f6b085acc5d37b2545cf1094b78ce 100644 (file)
@@ -6,6 +6,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
   alias Pleroma.Activity
   alias Pleroma.Conversation.Participation
   alias Pleroma.Object
+  alias Pleroma.Web.ActivityPub.Builder
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.CommonAPI.Utils
 
@@ -214,7 +215,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
     emoji = Map.merge(emoji, summary_emoji)
 
     object =
-      Utils.make_note_data(draft)
+      Builder.note(draft)
       |> Map.put("emoji", emoji)
       |> Map.put("source", draft.status)
       |> Map.put("generator", draft.params[:generator])
index 33639e6954fb54f51473fb9926c02c2f56b56239..9f8a42606056d280a2811e90a11bde3daf69480a 100644 (file)
@@ -291,33 +291,6 @@ defmodule Pleroma.Web.CommonAPI.Utils do
     |> Formatter.html_escape("text/html")
   end
 
-  def make_note_data(%ActivityDraft{} = draft) do
-    %{
-      "type" => "Note",
-      "to" => draft.to,
-      "cc" => draft.cc,
-      "content" => draft.content_html,
-      "summary" => draft.summary,
-      "sensitive" => draft.sensitive,
-      "context" => draft.context,
-      "attachment" => draft.attachments,
-      "actor" => draft.user.ap_id,
-      "tag" => Keyword.values(draft.tags) |> Enum.uniq()
-    }
-    |> add_in_reply_to(draft.in_reply_to)
-    |> Map.merge(draft.extra)
-  end
-
-  defp add_in_reply_to(object, nil), do: object
-
-  defp add_in_reply_to(object, in_reply_to) do
-    with %Object{} = in_reply_to_object <- Object.normalize(in_reply_to, fetch: false) do
-      Map.put(object, "inReplyTo", in_reply_to_object.data["id"])
-    else
-      _ -> object
-    end
-  end
-
   def format_naive_asctime(date) do
     date |> DateTime.from_naive!("Etc/UTC") |> format_asctime
   end
diff --git a/test/pleroma/web/activity_pub/builder_test.exs b/test/pleroma/web/activity_pub/builder_test.exs
new file mode 100644 (file)
index 0000000..103521c
--- /dev/null
@@ -0,0 +1,46 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ActivityPub.BuilderTest do
+  alias Pleroma.Web.ActivityPub.Builder
+  alias Pleroma.Web.CommonAPI.ActivityDraft
+  use Pleroma.DataCase
+
+  import Pleroma.Factory
+
+  describe "note/1" do
+    test "returns note data" do
+      user = insert(:user)
+      note = insert(:note)
+      user2 = insert(:user)
+      user3 = insert(:user)
+
+      draft = %ActivityDraft{
+        user: user,
+        to: [user2.ap_id],
+        context: "2hu",
+        content_html: "<h1>This is :moominmamma: note</h1>",
+        in_reply_to: note.id,
+        tags: [name: "jimm"],
+        summary: "test summary",
+        cc: [user3.ap_id],
+        extra: %{"custom_tag" => "test"}
+      }
+
+      assert Builder.note(draft) == %{
+               "actor" => user.ap_id,
+               "attachment" => [],
+               "cc" => [user3.ap_id],
+               "content" => "<h1>This is :moominmamma: note</h1>",
+               "context" => "2hu",
+               "sensitive" => false,
+               "summary" => "test summary",
+               "tag" => ["jimm"],
+               "to" => [user2.ap_id],
+               "type" => "Note",
+               "custom_tag" => "test"
+             }
+    end
+  end
+end
index b0e567ff0b250acf99905623d62847414cc50758..d8fec35208bd47d41703a66a3e39f35936ccc0ff 100644 (file)
@@ -681,41 +681,6 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
     end
   end
 
-  describe "make_note_data/1" do
-    test "returns note data" do
-      user = insert(:user)
-      note = insert(:note)
-      user2 = insert(:user)
-      user3 = insert(:user)
-
-      draft = %ActivityDraft{
-        user: user,
-        to: [user2.ap_id],
-        context: "2hu",
-        content_html: "<h1>This is :moominmamma: note</h1>",
-        in_reply_to: note.id,
-        tags: [name: "jimm"],
-        summary: "test summary",
-        cc: [user3.ap_id],
-        extra: %{"custom_tag" => "test"}
-      }
-
-      assert Utils.make_note_data(draft) == %{
-               "actor" => user.ap_id,
-               "attachment" => [],
-               "cc" => [user3.ap_id],
-               "content" => "<h1>This is :moominmamma: note</h1>",
-               "context" => "2hu",
-               "sensitive" => false,
-               "summary" => "test summary",
-               "tag" => ["jimm"],
-               "to" => [user2.ap_id],
-               "type" => "Note",
-               "custom_tag" => "test"
-             }
-    end
-  end
-
   describe "maybe_add_attachments/3" do
     test "returns parsed results when attachment_links is false" do
       assert Utils.maybe_add_attachments(