From a19f8778afddb7f504b08cedde752e37da52dc96 Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 29 Jun 2020 11:06:20 +0200 Subject: [PATCH] User preloader: Put user info at correct key --- lib/pleroma/web/preload/user.ex | 11 ++++++----- test/web/preload/user_test.exs | 14 +++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/pleroma/web/preload/user.ex b/lib/pleroma/web/preload/user.ex index 3a244845b..7fef0a4ac 100644 --- a/lib/pleroma/web/preload/user.ex +++ b/lib/pleroma/web/preload/user.ex @@ -3,11 +3,12 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.Preload.Providers.User do + alias Pleroma.User alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.Preload.Providers.Provider @behaviour Provider - @account_url :"/api/v1/accounts" + @account_url_base :"/api/v1/accounts" @impl Provider def generate_terms(%{user: user}) do @@ -16,10 +17,10 @@ def generate_terms(%{user: user}) do def generate_terms(_params), do: %{} - def build_accounts_tag(acc, nil), do: acc - - def build_accounts_tag(acc, user) do + def build_accounts_tag(acc, %User{} = user) do account_data = AccountView.render("show.json", %{user: user, for: user}) - Map.put(acc, @account_url, account_data) + Map.put(acc, :"#{@account_url_base}/#{user.id}", account_data) end + + def build_accounts_tag(acc, _), do: acc end diff --git a/test/web/preload/user_test.exs b/test/web/preload/user_test.exs index 99232cdfa..68d69d977 100644 --- a/test/web/preload/user_test.exs +++ b/test/web/preload/user_test.exs @@ -9,13 +9,11 @@ defmodule Pleroma.Web.Preload.Providers.UserTest do describe "returns empty when user doesn't exist" do test "nil user specified" do - refute User.generate_terms(%{user: nil}) - |> Map.has_key?("/api/v1/accounts") + assert User.generate_terms(%{user: nil}) == %{} end test "missing user specified" do - refute User.generate_terms(%{user: :not_a_user}) - |> Map.has_key?("/api/v1/accounts") + assert User.generate_terms(%{user: :not_a_user}) == %{} end end @@ -23,11 +21,13 @@ test "missing user specified" do setup do user = insert(:user) - {:ok, User.generate_terms(%{user: user})} + terms = User.generate_terms(%{user: user}) + %{terms: terms, user: user} end - test "account is rendered", %{"/api/v1/accounts": accounts} do - assert %{acct: user, username: user} = accounts + test "account is rendered", %{terms: terms, user: user} do + account = terms[:"/api/v1/accounts/#{user.id}"] + assert %{acct: user, username: user} = account end end end