Merge branch 'develop' into support/tests
authorMaksim Pechnikov <parallel588@gmail.com>
Sun, 15 Sep 2019 11:07:07 +0000 (14:07 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Sun, 15 Sep 2019 11:22:15 +0000 (14:22 +0300)
1  2 
lib/pleroma/user.ex
lib/pleroma/web/activity_pub/utils.ex

Simple merge
index 72e07b59dbbfe994ab84c688fa73d893db222fec,47917f5d3190a57b19fde77831aa69cccdb063df..35a477b4855346070ccb564cbb138a52fd4f7546
@@@ -178,59 -186,54 +176,59 @@@ defmodule Pleroma.Web.ActivityPub.Util
    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, fake? \\ false) do
 -    map =
 -      if not fake? do
 -        %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
 -
 -        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)
 -      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
 +  @spec lazy_put_activity_defaults(map(), boolean) :: map()
-   def lazy_put_activity_defaults(map, fake \\ false)
++  def lazy_put_activity_defaults(map, fake? \\ false)
  
 -    if is_map(map["object"]) do
 -      object = lazy_put_object_defaults(map["object"], map, fake?)
 -      %{map | "object" => object}
 -    else
 -      map
 -    end
 +  def lazy_put_activity_defaults(map, true) do
 +    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)
 +    |> lazy_put_object_defaults(true)
    end
  
-   def lazy_put_activity_defaults(map, _fake) do
 -  @doc """
 -  Adds an id and published date if they aren't there.
 -  """
 -  def lazy_put_object_defaults(map, activity \\ %{}, fake?)
++  def lazy_put_activity_defaults(map, _fake?) do
 +    %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
  
 -  def lazy_put_object_defaults(map, activity, true = _fake?) do
      map
 +    |> Map.put_new_lazy("id", &generate_activity_id/0)
      |> Map.put_new_lazy("published", &make_date/0)
 -    |> Map.put_new("id", "pleroma:fake_object_id")
 -    |> Map.put_new("context", activity["context"])
 -    |> Map.put_new("fake", true)
 -    |> Map.put_new("context_id", activity["context_id"])
 +    |> Map.put_new("context", context)
 +    |> Map.put_new("context_id", context_id)
 +    |> lazy_put_object_defaults(false)
    end
  
 -  def lazy_put_object_defaults(map, activity, _fake?) do
 -    map
 -    |> Map.put_new_lazy("id", &generate_object_id/0)
 -    |> Map.put_new_lazy("published", &make_date/0)
 -    |> Map.put_new("context", activity["context"])
 -    |> Map.put_new("context_id", activity["context_id"])
 +  # Adds an id and published date if they aren't there.
 +  #
 +  @spec lazy_put_object_defaults(map(), boolean()) :: map()
 +  defp lazy_put_object_defaults(%{"object" => map} = activity, true)
 +       when is_map(map) do
 +    object =
 +      map
 +      |> Map.put_new("id", "pleroma:fake_object_id")
 +      |> Map.put_new_lazy("published", &make_date/0)
 +      |> Map.put_new("context", activity["context"])
 +      |> Map.put_new("context_id", activity["context_id"])
 +      |> Map.put_new("fake", true)
 +
 +    %{activity | "object" => object}
 +  end
 +
 +  defp lazy_put_object_defaults(%{"object" => map} = activity, _)
 +       when is_map(map) do
 +    object =
 +      map
 +      |> Map.put_new_lazy("id", &generate_object_id/0)
 +      |> Map.put_new_lazy("published", &make_date/0)
 +      |> Map.put_new("context", activity["context"])
 +      |> Map.put_new("context_id", activity["context_id"])
 +
 +    %{activity | "object" => object}
    end
  
 +  defp lazy_put_object_defaults(activity, _), do: activity
 +
    @doc """
    Inserts a full object if it is contained in an activity.
    """
    @doc """
    Retruns an existing announce activity if the notice has already been announced
    """
-   def get_existing_announce(actor, %{data: %{"id" => id}}) do
-     actor
-     |> Activity.Queries.by_actor()
-     |> Activity.Queries.by_type("Announce")
-     |> Activity.Queries.by_object_id(id)
-     |> Activity.Queries.limit(1)
 +  @spec get_existing_announce(String.t(), map()) :: Activity.t() | nil
+   def get_existing_announce(actor, %{data: %{"id" => ap_id}}) do
+     "Announce"
+     |> Activity.Queries.by_type()
+     |> where(actor: ^actor)
+     # this is to use the index
+     |> Activity.Queries.by_object_id(ap_id)
      |> Repo.one()
    end
  
    end
  
    #### Block-related helpers
 +  @spec fetch_latest_block(User.t(), User.t()) :: Activity.t() | nil
    def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do
-     query =
-       blocker_id
-       |> Activity.Queries.by_actor()
-       |> Activity.Queries.by_type("Block")
-       |> Activity.Queries.by_object_id(blocked_id)
-       |> Activity.Queries.limit(1)
-     from(
-       activity in query,
-       order_by: [fragment("? desc nulls last", activity.id)]
-     )
+     "Block"
+     |> Activity.Queries.by_type()
+     |> where(actor: ^blocker_id)
+     # this is to use the index
+     |> Activity.Queries.by_object_id(blocked_id)
+     |> order_by([activity], fragment("? desc nulls last", activity.id))
+     |> limit(1)
      |> Repo.one()
    end