Merge branch 'preloadfixups' into 'develop'

Some fixups for the preload.

See merge request pleroma/pleroma!2692
This commit is contained in:
lain 2020-06-30 10:52:55 +00:00
commit b9e6ad571a
9 changed files with 44 additions and 28 deletions

View File

@ -3,14 +3,15 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Preload.Providers.Instance do defmodule Pleroma.Web.Preload.Providers.Instance do
alias Pleroma.Plugs.InstanceStatic
alias Pleroma.Web.MastodonAPI.InstanceView alias Pleroma.Web.MastodonAPI.InstanceView
alias Pleroma.Web.Nodeinfo.Nodeinfo alias Pleroma.Web.Nodeinfo.Nodeinfo
alias Pleroma.Web.Preload.Providers.Provider alias Pleroma.Web.Preload.Providers.Provider
@behaviour Provider @behaviour Provider
@instance_url :"/api/v1/instance" @instance_url "/api/v1/instance"
@panel_url :"/instance/panel.html" @panel_url "/instance/panel.html"
@nodeinfo_url :"/nodeinfo/2.0" @nodeinfo_url "/nodeinfo/2.0.json"
@impl Provider @impl Provider
def generate_terms(_params) do def generate_terms(_params) do
@ -27,7 +28,7 @@ defp build_info_tag(acc) do
end end
defp build_panel_tag(acc) do defp build_panel_tag(acc) do
instance_path = Path.join(:code.priv_dir(:pleroma), "static/instance/panel.html") instance_path = InstanceStatic.file_path(@panel_url |> to_string())
if File.exists?(instance_path) do if File.exists?(instance_path) do
panel_data = File.read!(instance_path) panel_data = File.read!(instance_path)

View File

@ -4,10 +4,10 @@
defmodule Pleroma.Web.Preload.Providers.StatusNet do defmodule Pleroma.Web.Preload.Providers.StatusNet do
alias Pleroma.Web.Preload.Providers.Provider alias Pleroma.Web.Preload.Providers.Provider
alias Pleroma.Web.TwitterAPI.UtilView alias Pleroma.Web.TwitterAPI.UtilController
@behaviour Provider @behaviour Provider
@config_url :"/api/statusnet/config.json" @config_url "/api/statusnet/config.json"
@impl Provider @impl Provider
def generate_terms(_params) do def generate_terms(_params) do
@ -16,9 +16,10 @@ def generate_terms(_params) do
end end
defp build_config_tag(acc) do defp build_config_tag(acc) do
instance = Pleroma.Config.get(:instance) resp =
info_data = UtilView.status_net_config(instance) Plug.Test.conn(:get, @config_url |> to_string())
|> UtilController.config(nil)
Map.put(acc, @config_url, info_data) Map.put(acc, @config_url, resp.resp_body)
end end
end end

View File

@ -8,7 +8,7 @@ defmodule Pleroma.Web.Preload.Providers.Timelines do
alias Pleroma.Web.Preload.Providers.Provider alias Pleroma.Web.Preload.Providers.Provider
@behaviour Provider @behaviour Provider
@public_url :"/api/v1/timelines/public" @public_url "/api/v1/timelines/public"
@impl Provider @impl Provider
def generate_terms(params) do def generate_terms(params) do

View File

@ -3,11 +3,12 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Preload.Providers.User do defmodule Pleroma.Web.Preload.Providers.User do
alias Pleroma.User
alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.Preload.Providers.Provider alias Pleroma.Web.Preload.Providers.Provider
@behaviour Provider @behaviour Provider
@account_url :"/api/v1/accounts" @account_url_base "/api/v1/accounts"
@impl Provider @impl Provider
def generate_terms(%{user: user}) do def generate_terms(%{user: user}) do
@ -16,10 +17,10 @@ def generate_terms(%{user: user}) do
def generate_terms(_params), do: %{} def generate_terms(_params), do: %{}
def build_accounts_tag(acc, nil), do: acc def build_accounts_tag(acc, %User{} = user) do
def build_accounts_tag(acc, user) do
account_data = AccountView.render("show.json", %{user: user, for: user}) 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 end
def build_accounts_tag(acc, _), do: acc
end end

View File

@ -0,0 +1 @@
HEY!

View File

@ -8,7 +8,7 @@ defmodule Pleroma.Web.Preload.Providers.InstanceTest do
setup do: {:ok, Instance.generate_terms(nil)} setup do: {:ok, Instance.generate_terms(nil)}
test "it renders the info", %{"/api/v1/instance": info} do test "it renders the info", %{"/api/v1/instance" => info} do
assert %{ assert %{
description: description, description: description,
email: "admin@example.com", email: "admin@example.com",
@ -18,14 +18,25 @@ test "it renders the info", %{"/api/v1/instance": info} do
assert String.equivalent?(description, "Pleroma: An efficient and flexible fediverse server") assert String.equivalent?(description, "Pleroma: An efficient and flexible fediverse server")
end end
test "it renders the panel", %{"/instance/panel.html": panel} do test "it renders the panel", %{"/instance/panel.html" => panel} do
assert String.contains?( assert String.contains?(
panel, panel,
"<p>Welcome to <a href=\"https://pleroma.social\" target=\"_blank\">Pleroma!</a></p>" "<p>Welcome to <a href=\"https://pleroma.social\" target=\"_blank\">Pleroma!</a></p>"
) )
end end
test "it renders the node_info", %{"/nodeinfo/2.0": nodeinfo} do test "it works with overrides" do
clear_config([:instance, :static_dir], "test/fixtures/preload_static")
%{"/instance/panel.html" => panel} = Instance.generate_terms(nil)
assert String.contains?(
panel,
"HEY!"
)
end
test "it renders the node_info", %{"/nodeinfo/2.0.json" => nodeinfo} do
%{ %{
metadata: metadata, metadata: metadata,
version: "2.0" version: "2.0"

View File

@ -8,7 +8,8 @@ defmodule Pleroma.Web.Preload.Providers.StatusNetTest do
setup do: {:ok, StatusNet.generate_terms(nil)} setup do: {:ok, StatusNet.generate_terms(nil)}
test "it renders the info", %{"/api/statusnet/config.json": info} do test "it renders the info", %{"/api/statusnet/config.json" => info} do
assert info =~ "<name>Pleroma</name>" assert {:ok, res} = Jason.decode(info)
assert res["site"]
end end
end end

View File

@ -9,7 +9,7 @@ defmodule Pleroma.Web.Preload.Providers.TimelineTest do
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
alias Pleroma.Web.Preload.Providers.Timelines alias Pleroma.Web.Preload.Providers.Timelines
@public_url :"/api/v1/timelines/public" @public_url "/api/v1/timelines/public"
describe "unauthenticated timeliness when restricted" do describe "unauthenticated timeliness when restricted" do
setup do setup do

View File

@ -9,13 +9,11 @@ defmodule Pleroma.Web.Preload.Providers.UserTest do
describe "returns empty when user doesn't exist" do describe "returns empty when user doesn't exist" do
test "nil user specified" do test "nil user specified" do
refute User.generate_terms(%{user: nil}) assert User.generate_terms(%{user: nil}) == %{}
|> Map.has_key?("/api/v1/accounts")
end end
test "missing user specified" do test "missing user specified" do
refute User.generate_terms(%{user: :not_a_user}) assert User.generate_terms(%{user: :not_a_user}) == %{}
|> Map.has_key?("/api/v1/accounts")
end end
end end
@ -23,11 +21,13 @@ test "missing user specified" do
setup do setup do
user = insert(:user) user = insert(:user)
{:ok, User.generate_terms(%{user: user})} terms = User.generate_terms(%{user: user})
%{terms: terms, user: user}
end end
test "account is rendered", %{"/api/v1/accounts": accounts} do test "account is rendered", %{terms: terms, user: user} do
assert %{acct: user, username: user} = accounts account = terms["/api/v1/accounts/#{user.id}"]
assert %{acct: user, username: user} = account
end end
end end
end end