common_fixes: Get fixes common from Audio and Question
authorHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Tue, 18 Aug 2020 00:11:38 +0000 (02:11 +0200)
committerHaelwenn (lanodan) Monnier <contact@hacktivis.me>
Tue, 18 Aug 2020 00:24:48 +0000 (02:24 +0200)
lib/pleroma/web/activity_pub/object_validators/audio_validator.ex
lib/pleroma/web/activity_pub/object_validators/common_fixes.ex [new file with mode: 0644]
lib/pleroma/web/activity_pub/object_validators/question_validator.ex

index 5ff9e3832c279e2dedd1c9eab60c6ecbf7b9d1bb..5d9bf345f512d9bf0a9a5e7c51cb08019a4fdbb2 100644 (file)
@@ -7,15 +7,14 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AudioValidator do
 
   alias Pleroma.EctoType.ActivityPub.ObjectValidators
   alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
+  alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
   alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
-  alias Pleroma.Web.ActivityPub.Utils
 
   import Ecto.Changeset
 
   @primary_key false
   @derive Jason.Encoder
 
-  # Extends from NoteValidator
   embedded_schema do
     field(:id, ObjectValidators.ObjectID, primary_key: true)
     field(:to, ObjectValidators.Recipients, default: [])
@@ -67,26 +66,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AudioValidator do
     |> changeset(data)
   end
 
-  # based on Pleroma.Web.ActivityPub.Utils.lazy_put_objects_defaults
-  defp fix_defaults(data) do
-    %{data: %{"id" => context}, id: context_id} =
-      Utils.create_context(data["context"] || data["conversation"])
-
-    data
-    |> Map.put_new_lazy("published", &Utils.make_date/0)
-    |> Map.put_new("context", context)
-    |> Map.put_new("context_id", context_id)
-  end
-
-  defp fix_attribution(data) do
-    data
-    |> Map.put_new("actor", data["attributedTo"])
-  end
-
   defp fix(data) do
     data
-    |> fix_defaults()
-    |> fix_attribution()
+    |> CommonFixes.fix_defaults()
+    |> CommonFixes.fix_attribution()
   end
 
   def changeset(struct, data) do
diff --git a/lib/pleroma/web/activity_pub/object_validators/common_fixes.ex b/lib/pleroma/web/activity_pub/object_validators/common_fixes.ex
new file mode 100644 (file)
index 0000000..f13c16e
--- /dev/null
@@ -0,0 +1,23 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes do
+  alias Pleroma.Web.ActivityPub.Utils
+
+  # based on Pleroma.Web.ActivityPub.Utils.lazy_put_objects_defaults
+  def fix_defaults(data) do
+    %{data: %{"id" => context}, id: context_id} =
+      Utils.create_context(data["context"] || data["conversation"])
+
+    data
+    |> Map.put_new_lazy("published", &Utils.make_date/0)
+    |> Map.put_new("context", context)
+    |> Map.put_new("context_id", context_id)
+  end
+
+  def fix_attribution(data) do
+    data
+    |> Map.put_new("actor", data["attributedTo"])
+  end
+end
index f47acf606aabd89ddc756f83176db4f4db48e628..0aa70ee309543fb63b0e5da2638b9d08e779c810 100644 (file)
@@ -7,9 +7,9 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do
 
   alias Pleroma.EctoType.ActivityPub.ObjectValidators
   alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
+  alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
   alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
   alias Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator
-  alias Pleroma.Web.ActivityPub.Utils
 
   import Ecto.Changeset
 
@@ -81,27 +81,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do
     end
   end
 
-  # based on Pleroma.Web.ActivityPub.Utils.lazy_put_objects_defaults
-  defp fix_defaults(data) do
-    %{data: %{"id" => context}, id: context_id} =
-      Utils.create_context(data["context"] || data["conversation"])
-
-    data
-    |> Map.put_new_lazy("published", &Utils.make_date/0)
-    |> Map.put_new("context", context)
-    |> Map.put_new("context_id", context_id)
-  end
-
-  defp fix_attribution(data) do
-    data
-    |> Map.put_new("actor", data["attributedTo"])
-  end
-
   defp fix(data) do
     data
-    |> fix_attribution()
+    |> CommonFixes.fix_defaults()
+    |> CommonFixes.fix_attribution()
     |> fix_closed()
-    |> fix_defaults()
   end
 
   def changeset(struct, data) do