Merge branch 'favicon' into 'develop'

Make favicon configurable, embed favicon and manifest in server-generated meta

See merge request pleroma/pleroma!3963
This commit is contained in:
HJ 2023-11-19 08:52:37 +00:00
commit 4ebfc011fc
6 changed files with 50 additions and 30 deletions

1
changelog.d/favicon.add Normal file
View File

@ -0,0 +1 @@
Add support for configuring favicon, embed favicon and PWA manifest in server-generated meta

View File

@ -171,6 +171,7 @@
short_description: "", short_description: "",
background_image: "/images/city.jpg", background_image: "/images/city.jpg",
instance_thumbnail: "/instance/thumbnail.jpeg", instance_thumbnail: "/instance/thumbnail.jpeg",
favicon: "/favicon.png",
limit: 5_000, limit: 5_000,
description_limit: 5_000, description_limit: 5_000,
remote_limit: 100_000, remote_limit: 100_000,
@ -346,6 +347,8 @@
icons: [ icons: [
%{ %{
src: "/static/logo.svg", src: "/static/logo.svg",
sizes: "144x144",
purpose: "any",
type: "image/svg+xml" type: "image/svg+xml"
} }
], ],

View File

@ -987,6 +987,12 @@
"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.", "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"] suggestions: ["/instance/thumbnail.jpeg"]
}, },
%{
key: :favicon,
type: {:string, :image},
description: "Favicon of the instance",
suggestions: ["/favicon.png"]
},
%{ %{
key: :show_reactions, key: :show_reactions,
type: :boolean, type: :boolean,

View File

@ -17,10 +17,28 @@ def api_not_implemented(conn, _params) do
|> json(%{error: "Not implemented"}) |> json(%{error: "Not implemented"})
end end
def add_generated_metadata(page_content, extra \\ "") do
title = "<title>#{Pleroma.Config.get([:instance, :name])}</title>"
favicon = "<link rel='icon' href='#{Pleroma.Config.get([:instance, :favicon])}'>"
manifest = "<link rel='manifest' href='/manifest.json'>"
page_content
|> String.replace(
"<!--server-generated-meta-->",
title <> favicon <> manifest <> extra
)
end
def redirector(conn, _params, code \\ 200) do def redirector(conn, _params, code \\ 200) do
{:ok, index_content} = File.read(index_file_path())
response =
index_content
|> add_generated_metadata()
conn conn
|> put_resp_content_type("text/html") |> put_resp_content_type("text/html")
|> send_file(code, index_file_path()) |> send_resp(code, response)
end end
def redirector_with_meta(conn, %{"maybe_nickname_or_id" => maybe_nickname_or_id} = params) do def redirector_with_meta(conn, %{"maybe_nickname_or_id" => maybe_nickname_or_id} = params) do
@ -34,14 +52,12 @@ def redirector_with_meta(conn, %{"maybe_nickname_or_id" => maybe_nickname_or_id}
def redirector_with_meta(conn, params) do def redirector_with_meta(conn, params) do
{:ok, index_content} = File.read(index_file_path()) {:ok, index_content} = File.read(index_file_path())
tags = build_tags(conn, params) tags = build_tags(conn, params)
preloads = preload_data(conn, params) preloads = preload_data(conn, params)
title = "<title>#{Pleroma.Config.get([:instance, :name])}</title>"
response = response =
index_content index_content
|> String.replace("<!--server-generated-meta-->", tags <> preloads <> title) |> add_generated_metadata(tags <> preloads)
conn conn
|> put_resp_content_type("text/html") |> put_resp_content_type("text/html")
@ -55,11 +71,10 @@ def redirector_with_preload(conn, %{"path" => ["pleroma", "admin"]}) do
def redirector_with_preload(conn, params) do def redirector_with_preload(conn, params) do
{:ok, index_content} = File.read(index_file_path()) {:ok, index_content} = File.read(index_file_path())
preloads = preload_data(conn, params) preloads = preload_data(conn, params)
title = "<title>#{Pleroma.Config.get([:instance, :name])}</title>"
response = response =
index_content index_content
|> String.replace("<!--server-generated-meta-->", preloads <> title) |> add_generated_metadata(preloads)
conn conn
|> put_resp_content_type("text/html") |> put_resp_content_type("text/html")

View File

@ -6,20 +6,6 @@ defmodule Pleroma.Web.FallbackTest do
use Pleroma.Web.ConnCase use Pleroma.Web.ConnCase
import Pleroma.Factory import Pleroma.Factory
describe "neither preloaded data nor metadata attached to" do
test "GET /registration/:token", %{conn: conn} do
response = get(conn, "/registration/foo")
assert html_response(response, 200) =~ "<!--server-generated-meta-->"
end
test "GET /*path", %{conn: conn} do
assert conn
|> get("/foo")
|> html_response(200) =~ "<!--server-generated-meta-->"
end
end
test "GET /*path adds a title", %{conn: conn} do test "GET /*path adds a title", %{conn: conn} do
clear_config([:instance, :name], "a cool title") clear_config([:instance, :name], "a cool title")
@ -29,21 +15,28 @@ test "GET /*path adds a title", %{conn: conn} do
end end
describe "preloaded data and metadata attached to" do describe "preloaded data and metadata attached to" do
test "GET /:maybe_nickname_or_id", %{conn: conn} do test "GET /:maybe_nickname_or_id with existing user", %{conn: conn} do
clear_config([:instance, :name], "a cool title")
user = insert(:user)
resp = get(conn, "/#{user.nickname}")
assert html_response(resp, 200) =~ "<title>a cool title</title>"
refute html_response(resp, 200) =~ "<!--server-generated-meta-->"
assert html_response(resp, 200) =~ "initial-results"
end
test "GET /:maybe_nickname_or_id with missing user", %{conn: conn} do
clear_config([:instance, :name], "a cool title") clear_config([:instance, :name], "a cool title")
user = insert(:user) resp = get(conn, "/foo")
user_missing = get(conn, "/foo")
user_present = get(conn, "/#{user.nickname}")
assert html_response(user_missing, 200) =~ "<!--server-generated-meta-->" assert html_response(resp, 200) =~ "<title>a cool title</title>"
refute html_response(user_present, 200) =~ "<!--server-generated-meta-->" refute html_response(resp, 200) =~ "initial-results"
assert html_response(user_present, 200) =~ "initial-results"
assert html_response(user_present, 200) =~ "<title>a cool title</title>"
end end
test "GET /*path", %{conn: conn} do test "GET /*path", %{conn: conn} do
assert conn refute conn
|> get("/foo") |> get("/foo")
|> html_response(200) =~ "<!--server-generated-meta-->" |> html_response(200) =~ "<!--server-generated-meta-->"
@ -65,10 +58,12 @@ test "GET /main/public", %{conn: conn} do
end end
test "GET /main/all", %{conn: conn} do test "GET /main/all", %{conn: conn} do
clear_config([:instance, :name], "a cool title")
public_page = get(conn, "/main/all") public_page = get(conn, "/main/all")
refute html_response(public_page, 200) =~ "<!--server-generated-meta-->" refute html_response(public_page, 200) =~ "<!--server-generated-meta-->"
assert html_response(public_page, 200) =~ "initial-results" assert html_response(public_page, 200) =~ "initial-results"
assert html_response(public_page, 200) =~ "<title>a cool title</title>"
end end
end end

View File

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