Merge branch 'configurable-favicon' into 'develop'

Let favicon be configurable

See merge request soapbox-pub/rebased!192
This commit is contained in:
Alex Gleason 2022-10-27 20:03:32 +00:00
commit ad014adc1c
8 changed files with 55 additions and 32 deletions

View file

@ -190,6 +190,7 @@
short_description: "",
background_image: "/images/city.jpg",
instance_thumbnail: "/instance/thumbnail.png",
favicon: "/favicon.png",
limit: 5_000,
description_limit: 5_000,
remote_limit: 100_000,

View file

@ -988,6 +988,13 @@
"The instance thumbnail can be any image that represents your instance and is used by some apps or services when they display information about your instance.",
suggestions: ["/instance/thumbnail.jpeg"]
},
%{
key: :favicon,
type: {:string, :image},
description:
"Shortcut icon displayed in the browser, and possibly displayed by other instances.",
suggestions: ["/favicon.png"]
},
%{
key: :show_reactions,
type: :boolean,

View file

@ -17,10 +17,8 @@ def api_not_implemented(conn, _params) do
|> json(%{error: "Not implemented"})
end
def redirector(conn, _params, code \\ 200) do
conn
|> put_resp_content_type("text/html")
|> send_file(code, index_file_path())
def redirector(conn, params, code \\ 200) do
redirector_with_ssr(conn, params, [:title, :favicon], code)
end
def redirector_with_meta(conn, %{"maybe_nickname_or_id" => maybe_nickname_or_id} = params) do
@ -33,19 +31,7 @@ def redirector_with_meta(conn, %{"maybe_nickname_or_id" => maybe_nickname_or_id}
end
def redirector_with_meta(conn, params) do
{:ok, index_content} = File.read(index_file_path())
tags = build_tags(conn, params)
preloads = preload_data(conn, params)
title = "<title>#{Pleroma.Config.get([:instance, :name])}</title>"
response =
index_content
|> String.replace("<!--server-generated-meta-->", tags <> preloads <> title)
conn
|> put_resp_content_type("text/html")
|> send_resp(200, response)
redirector_with_ssr(conn, params, [:tags, :preload, :title, :favicon])
end
def redirector_with_preload(conn, %{"path" => ["pleroma", "admin"]}) do
@ -53,17 +39,21 @@ def redirector_with_preload(conn, %{"path" => ["pleroma", "admin"]}) do
end
def redirector_with_preload(conn, params) do
redirector_with_ssr(conn, params, [:preload, :title, :favicon])
end
defp redirector_with_ssr(conn, params, keys, code \\ 200) do
{:ok, index_content} = File.read(index_file_path())
preloads = preload_data(conn, params)
title = "<title>#{Pleroma.Config.get([:instance, :name])}</title>"
meta = compose_meta(conn, params, keys)
response =
index_content
|> String.replace("<!--server-generated-meta-->", preloads <> title)
|> String.replace("<!--server-generated-meta-->", Enum.join(meta))
conn
|> put_resp_content_type("text/html")
|> send_resp(200, response)
|> send_resp(code, response)
end
def registration_page(conn, params) do
@ -80,7 +70,13 @@ defp index_file_path do
Pleroma.Web.Plugs.InstanceStatic.file_path("index.html")
end
defp build_tags(conn, params) do
defp compose_meta(conn, params, attrs) when is_list(attrs) do
Enum.map(attrs, fn attr ->
build_meta(attr, {conn, params})
end)
end
defp build_meta(:tags, {conn, params}) do
try do
Metadata.build_tags(params)
rescue
@ -94,7 +90,7 @@ defp build_tags(conn, params) do
end
end
defp preload_data(conn, params) do
defp build_meta(:preload, {conn, params}) do
try do
Preload.build_tags(conn, params)
rescue
@ -107,4 +103,12 @@ defp preload_data(conn, params) do
""
end
end
defp build_meta(:title, _) do
"<title>#{Pleroma.Config.get([:instance, :name])}</title>"
end
defp build_meta(:favicon, _) do
"<link rel=\"icon\" href=\"#{Pleroma.Config.get([:instance, :favicon])}\">"
end
end

View file

@ -56,7 +56,10 @@ def render("show.json", _) do
},
stats: %{mau: Pleroma.User.active_user_count()},
vapid_public_key: Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key),
oauth_consumer_strategies: Pleroma.Config.oauth_consumer_strategies()
oauth_consumer_strategies: Pleroma.Config.oauth_consumer_strategies(),
favicon:
URI.merge(Pleroma.Web.Endpoint.url(), Keyword.get(instance, :favicon))
|> to_string
},
configuration: configuration(),
soapbox: %{

View file

@ -4,7 +4,6 @@
<title>Welcome to Rebased</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover,user-scalable=no">
<link rel="icon" type="image/png" href="/favicon.png">
<style type="text/css">
:root {
--brand-color: #0482d8;

View file

@ -10,13 +10,13 @@ defmodule Pleroma.Web.FallbackTest do
test "GET /registration/:token", %{conn: conn} do
response = get(conn, "/registration/foo")
assert html_response(response, 200) =~ "<!--server-generated-meta-->"
refute html_response(response, 200) =~ "initial-results"
end
test "GET /*path", %{conn: conn} do
assert conn
refute conn
|> get("/foo")
|> html_response(200) =~ "<!--server-generated-meta-->"
|> html_response(200) =~ "initial-results"
end
end
@ -31,21 +31,25 @@ test "GET /*path adds a title", %{conn: conn} do
describe "preloaded data and metadata attached to" do
test "GET /:maybe_nickname_or_id", %{conn: conn} do
clear_config([:instance, :name], "a cool title")
clear_config([:instance, :favicon], "/favicon.svg")
user = insert(:user)
user_missing = get(conn, "/foo")
user_present = get(conn, "/#{user.nickname}")
assert html_response(user_missing, 200) =~ "<!--server-generated-meta-->"
refute html_response(user_missing, 200) =~ "<!--server-generated-meta-->"
refute html_response(user_present, 200) =~ "<!--server-generated-meta-->"
assert html_response(user_present, 200) =~ "initial-results"
assert html_response(user_present, 200) =~ "<title>a cool title</title>"
assert html_response(user_present, 200) =~ "<link rel=\"icon\" href=\"/favicon.svg\">"
end
test "GET /*path", %{conn: conn} do
clear_config([:instance, :name], "Rebased")
assert conn
|> get("/foo")
|> html_response(200) =~ "<!--server-generated-meta-->"
|> html_response(200) =~ "Rebased"
refute conn
|> get("/foo/bar")

View file

@ -19,6 +19,7 @@ test "get instance information", %{conn: conn} do
email = Pleroma.Config.get([:instance, :email])
thumbnail = Pleroma.Web.Endpoint.url() <> Pleroma.Config.get([:instance, :instance_thumbnail])
background = Pleroma.Web.Endpoint.url() <> Pleroma.Config.get([:instance, :background_image])
favicon = Pleroma.Web.Endpoint.url() <> Pleroma.Config.get([:instance, :favicon])
# Note: not checking for "max_toot_chars" since it's optional
assert %{
@ -44,7 +45,10 @@ test "get instance information", %{conn: conn} do
"background_image" => from_config_background,
"shout_limit" => _,
"description_limit" => _,
"rules" => _
"rules" => _,
"pleroma" => %{
"favicon" => from_config_favicon
}
} = result
assert result["version"] =~ "Pleroma"
@ -60,6 +64,7 @@ test "get instance information", %{conn: conn} do
assert email == from_config_email
assert thumbnail == from_config_thumbnail
assert background == from_config_background
assert favicon == from_config_favicon
end
test "get instance stats", %{conn: conn} do

View file

@ -196,7 +196,7 @@ test "render html for redirect for html format", %{conn: conn} do
|> get("/notice/#{like_activity.id}")
|> response(200)
assert resp =~ "<!--server-generated-meta-->"
refute resp =~ "initial-results"
end
test "404s a private notice", %{conn: conn} do