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
defmodule Pleroma.Web.Preload.Providers.Instance do
alias Pleroma.Plugs.InstanceStatic
alias Pleroma.Web.MastodonAPI.InstanceView
alias Pleroma.Web.Nodeinfo.Nodeinfo
alias Pleroma.Web.Preload.Providers.Provider
@behaviour Provider
@instance_url :"/api/v1/instance"
@panel_url :"/instance/panel.html"
@nodeinfo_url :"/nodeinfo/2.0"
@instance_url "/api/v1/instance"
@panel_url "/instance/panel.html"
@nodeinfo_url "/nodeinfo/2.0.json"
@impl Provider
def generate_terms(_params) do
@ -27,7 +28,7 @@ defmodule Pleroma.Web.Preload.Providers.Instance do
end
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
panel_data = File.read!(instance_path)

View File

@ -4,10 +4,10 @@
defmodule Pleroma.Web.Preload.Providers.StatusNet do
alias Pleroma.Web.Preload.Providers.Provider
alias Pleroma.Web.TwitterAPI.UtilView
alias Pleroma.Web.TwitterAPI.UtilController
@behaviour Provider
@config_url :"/api/statusnet/config.json"
@config_url "/api/statusnet/config.json"
@impl Provider
def generate_terms(_params) do
@ -16,9 +16,10 @@ defmodule Pleroma.Web.Preload.Providers.StatusNet do
end
defp build_config_tag(acc) do
instance = Pleroma.Config.get(:instance)
info_data = UtilView.status_net_config(instance)
resp =
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

View File

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

View File

@ -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 @@ defmodule Pleroma.Web.Preload.Providers.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

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)}
test "it renders the info", %{"/api/v1/instance": info} do
test "it renders the info", %{"/api/v1/instance" => info} do
assert %{
description: description,
email: "admin@example.com",
@ -18,14 +18,25 @@ defmodule Pleroma.Web.Preload.Providers.InstanceTest do
assert String.equivalent?(description, "Pleroma: An efficient and flexible fediverse server")
end
test "it renders the panel", %{"/instance/panel.html": panel} do
test "it renders the panel", %{"/instance/panel.html" => panel} do
assert String.contains?(
panel,
"<p>Welcome to <a href=\"https://pleroma.social\" target=\"_blank\">Pleroma!</a></p>"
)
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,
version: "2.0"

View File

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

View File

@ -9,7 +9,7 @@ defmodule Pleroma.Web.Preload.Providers.TimelineTest do
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.Preload.Providers.Timelines
@public_url :"/api/v1/timelines/public"
@public_url "/api/v1/timelines/public"
describe "unauthenticated timeliness when restricted" 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
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 @@ defmodule Pleroma.Web.Preload.Providers.UserTest 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