From cd387f8693c57b925576ab92f8202ef28007cfc0 Mon Sep 17 00:00:00 2001
From: rinpatch <rinpatch@sdf.org>
Date: Sat, 30 Mar 2019 13:57:54 +0300
Subject: [PATCH] Add a fake option to lazy_put_actvity_defaults

---
 lib/pleroma/web/activity_pub/activity_pub.ex |  2 +-
 lib/pleroma/web/activity_pub/utils.ex        | 30 +++++++++++++-------
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index b459fd882..a94040d01 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -115,7 +115,7 @@ def decrease_replies_count_if_reply(_object), do: :noop
 
   def insert(map, local \\ true, fake \\ false) when is_map(map) do
     with nil <- Activity.normalize(map),
-         map <- lazy_put_activity_defaults(map),
+         map <- lazy_put_activity_defaults(map, fake),
          :ok <- check_actor_is_active(map["actor"]),
          {_, true} <- {:remote_limit_error, check_remote_limit(map)},
          {:ok, map} <- MRF.filter(map),
diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex
index 2e9ffe41c..3959e9bd9 100644
--- a/lib/pleroma/web/activity_pub/utils.ex
+++ b/lib/pleroma/web/activity_pub/utils.ex
@@ -175,21 +175,29 @@ def maybe_federate(_), do: :ok
   Adds an id and a published data if they aren't there,
   also adds it to an included object
   """
-  def lazy_put_activity_defaults(map) do
-    %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
+  def lazy_put_activity_defaults(map, fake \\ false) do
+    unless fake do
+      %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
 
-    map =
-      map
-      |> Map.put_new_lazy("id", &generate_activity_id/0)
-      |> Map.put_new_lazy("published", &make_date/0)
-      |> Map.put_new("context", context)
-      |> Map.put_new("context_id", context_id)
+      map =
+        map
+        |> Map.put_new_lazy("id", &generate_activity_id/0)
+        |> Map.put_new_lazy("published", &make_date/0)
+        |> Map.put_new("context", context)
+        |> Map.put_new("context_id", context_id)
 
-    if is_map(map["object"]) do
-      object = lazy_put_object_defaults(map["object"], map)
-      %{map | "object" => object}
+      if is_map(map["object"]) do
+        object = lazy_put_object_defaults(map["object"], map)
+        %{map | "object" => object}
+      else
+        map
+      end
     else
       map
+      |> Map.put_new("id", "pleroma:fakeid")
+      |> Map.put_new_lazy("published", &make_date/0)
+      |> Map.put_new("context", "pleroma:fakecontext")
+      |> Map.put_new("context_id", -1)
     end
   end