Merge branch 'fix/mastoapi-more-object-preloads' into 'develop'
authorHaelwenn <contact+git.pleroma.social@hacktivis.me>
Tue, 13 Aug 2019 22:32:49 +0000 (22:32 +0000)
committerHaelwenn <contact+git.pleroma.social@hacktivis.me>
Tue, 13 Aug 2019 22:32:49 +0000 (22:32 +0000)
Mastodon API: Preloading and normalization optimizations

See merge request pleroma/pleroma!1558

1  2 
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex

index 47e263aaee0bfa9567246b4f6abad086109674ed,96e0d82aa3018c22202ea7388d1c3dbb132b855e..c3c75bd9a5a6b6f654426c0785f8edf17fe52dd0
@@@ -435,6 -435,7 +435,7 @@@ defmodule Pleroma.Web.MastodonAPI.Masto
        |> Map.put("local_only", local_only)
        |> Map.put("blocking_user", user)
        |> Map.put("muting_user", user)
+       |> Map.put("user", user)
        |> ActivityPub.fetch_public_activities()
        |> Enum.reverse()
  
        |> put_view(StatusView)
        |> try_render("poll.json", %{object: object, for: user})
      else
 -      nil -> render_error(conn, :not_found, "Record not found")
 -      false -> render_error(conn, :not_found, "Record not found")
 +      error when is_nil(error) or error == false ->
 +        render_error(conn, :not_found, "Record not found")
      end
    end
  
    end
  
    def favourited_by(%{assigns: %{user: user}} = conn, %{"id" => id}) do
-     with %Activity{data: %{"object" => object}} <- Activity.get_by_id(id),
-          %Object{data: %{"likes" => likes}} <- Object.normalize(object) do
+     with %Activity{} = activity <- Activity.get_by_id_with_object(id),
+          %Object{data: %{"likes" => likes}} <- Object.normalize(activity) do
        q = from(u in User, where: u.ap_id in ^likes)
  
        users =
    end
  
    def reblogged_by(%{assigns: %{user: user}} = conn, %{"id" => id}) do
-     with %Activity{data: %{"object" => object}} <- Activity.get_by_id(id),
-          %Object{data: %{"announcements" => announces}} <- Object.normalize(object) do
+     with %Activity{} = activity <- Activity.get_by_id_with_object(id),
+          %Object{data: %{"announcements" => announces}} <- Object.normalize(activity) do
        q = from(u in User, where: u.ap_id in ^announces)
  
        users =
        |> Map.put("local_only", local_only)
        |> Map.put("blocking_user", user)
        |> Map.put("muting_user", user)
+       |> Map.put("user", user)
        |> Map.put("tag", tags)
        |> Map.put("tag_all", tag_all)
        |> Map.put("tag_reject", tag_reject)
          params
          |> Map.put("type", "Create")
          |> Map.put("blocking_user", user)
+         |> Map.put("user", user)
          |> Map.put("muting_user", user)
  
        # we must filter the following list for the user to avoid leaking statuses the user
          |> String.replace("{{user}}", user)
  
        with {:ok, %{status: 200, body: body}} <-
 -             HTTP.get(
 -               url,
 -               [],
 -               adapter: [
 -                 recv_timeout: timeout,
 -                 pool: :default
 -               ]
 -             ),
 +             HTTP.get(url, [], adapter: [recv_timeout: timeout, pool: :default]),
             {:ok, data} <- Jason.decode(body) do
          data =
            data
            |> Enum.slice(0, limit)
            |> Enum.map(fn x ->
 -            Map.put(
 -              x,
 -              "id",
 -              case User.get_or_fetch(x["acct"]) do
 -                {:ok, %User{id: id}} -> id
 -                _ -> 0
 -              end
 -            )
 -          end)
 -          |> Enum.map(fn x ->
 -            Map.put(x, "avatar", MediaProxy.url(x["avatar"]))
 -          end)
 -          |> Enum.map(fn x ->
 -            Map.put(x, "avatar_static", MediaProxy.url(x["avatar_static"]))
 +            x
 +            |> Map.put("id", fetch_suggestion_id(x))
 +            |> Map.put("avatar", MediaProxy.url(x["avatar"]))
 +            |> Map.put("avatar_static", MediaProxy.url(x["avatar_static"]))
            end)
  
 -        conn
 -        |> json(data)
 +        json(conn, data)
        else
 -        e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
 +        e ->
 +          Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}")
        end
      else
        json(conn, [])
      end
 +  end
 +
 +  defp fetch_suggestion_id(attrs) do
 +    case User.get_or_fetch(attrs["acct"]) do
 +      {:ok, %User{id: id}} -> id
 +      _ -> 0
 +    end
    end
  
    def status_card(%{assigns: %{user: user}} = conn, %{"id" => status_id}) do