Add ActivityDraft.with_valid/2
authorEgor Kislitsyn <egor@kislitsyn.com>
Mon, 23 Sep 2019 11:52:41 +0000 (18:52 +0700)
committerEgor Kislitsyn <egor@kislitsyn.com>
Thu, 26 Sep 2019 03:29:34 +0000 (10:29 +0700)
lib/pleroma/web/common_api/activity_draft.ex

index b4480bd18df32f3fc002b3f2473a0118fb128f75..aa7c8c3812bd36aea8ce98d444a8acdfd1569c6a 100644 (file)
@@ -40,20 +40,20 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
     |> put_params(params)
     |> status()
     |> summary()
-    |> attachments()
     |> full_payload()
-    |> in_reply_to()
-    |> in_reply_to_conversation()
-    |> visibility()
     |> expires_at()
     |> poll()
+    |> with_valid(&in_reply_to/1)
+    |> with_valid(&attachments/1)
+    |> with_valid(&in_reply_to_conversation/1)
+    |> with_valid(&visibility/1)
     |> content()
-    |> to_and_cc()
-    |> context()
+    |> with_valid(&to_and_cc/1)
+    |> with_valid(&context/1)
     |> sensitive()
-    |> object()
+    |> with_valid(&object/1)
     |> preview?()
-    |> changes()
+    |> with_valid(&changes/1)
     |> validate()
   end
 
@@ -136,8 +136,6 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
     %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
   end
 
-  defp to_and_cc(%{valid?: false} = draft), do: draft
-
   defp to_and_cc(draft) do
     addressed_users =
       draft.mentions
@@ -166,8 +164,6 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
     %__MODULE__{draft | sensitive: sensitive}
   end
 
-  defp object(%{valid?: false} = draft), do: draft
-
   defp object(draft) do
     emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
 
@@ -195,8 +191,6 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
     %__MODULE__{draft | preview?: preview?}
   end
 
-  defp changes(%{valid?: false} = draft), do: draft
-
   defp changes(draft) do
     direct? = draft.visibility == "direct"
 
@@ -213,6 +207,9 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
     %__MODULE__{draft | changes: changes}
   end
 
+  defp with_valid(%{valid?: true} = draft, func), do: func.(draft)
+  defp with_valid(draft, _func), do: draft
+
   defp add_error(draft, message) do
     %__MODULE__{draft | valid?: false, errors: [message | draft.errors]}
   end