From bc6bfe383f642e74bbf09828181d9c89b1b17f05 Mon Sep 17 00:00:00 2001 From: floatingghost Date: Wed, 6 Jul 2022 20:00:43 +0000 Subject: [PATCH] Add configurable theme color (#53) Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/53 --- CHANGELOG.md | 1 + config/config.exs | 2 + config/description.exs | 15 ++++ docs/configuration/cheatsheet.md | 12 +++ lib/pleroma/application.ex | 1 - lib/pleroma/telemetry/logger.ex | 85 ------------------- lib/pleroma/web/endpoint.ex | 2 - .../web/fallback/redirect_controller.ex | 3 +- lib/pleroma/web/metadata.ex | 20 ++++- lib/pleroma/web/metadata/providers/theme.ex | 16 ++++ .../web/metadata/providers/theme_test.exs | 12 +++ 11 files changed, 79 insertions(+), 90 deletions(-) delete mode 100644 lib/pleroma/telemetry/logger.ex create mode 100644 lib/pleroma/web/metadata/providers/theme.ex create mode 100644 test/pleroma/web/metadata/providers/theme_test.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index 55ae540b6..2a12a0c57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Added move account API +- Added ability to set instance accent-color via theme-color ### Removed - SSH frontend, to be potentially re-enabled via a bridge rather than wired into the main system diff --git a/config/config.exs b/config/config.exs index 06c946b2a..908b79d09 100644 --- a/config/config.exs +++ b/config/config.exs @@ -477,6 +477,8 @@ config :pleroma, Pleroma.Web.Metadata, ], unfurl_nsfw: false +config :pleroma, Pleroma.Web.Metadata.Providers.Theme, theme_color: "#593196" + config :pleroma, Pleroma.Web.Preload, providers: [ Pleroma.Web.Preload.Providers.Instance diff --git a/config/description.exs b/config/description.exs index d8eaa34a2..c36e9d2b2 100644 --- a/config/description.exs +++ b/config/description.exs @@ -1979,6 +1979,21 @@ config :pleroma, :config_description, [ } ] }, + %{ + group: :pleroma, + key: Pleroma.Web.Metadata.Providers.Theme, + type: :group, + description: "Specific provider to hand out themes to instances that scrape index.html", + children: [ + %{ + key: :theme_color, + type: :string, + description: + "The 'accent color' of the instance, used in places like misskey's instance ticker", + suggestions: ["#593196"] + } + ] + }, %{ group: :pleroma, key: :rich_media, diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md index 11083e831..cef86e806 100644 --- a/docs/configuration/cheatsheet.md +++ b/docs/configuration/cheatsheet.md @@ -1114,6 +1114,18 @@ config :pleroma, :frontends, This would serve the frontend from the the folder at `$instance_static/frontends/pleroma/stable`. You have to copy the frontend into this folder yourself. You can choose the name and ref any way you like, but they will be used by mix tasks to automate installation in the future, the name referring to the project and the ref referring to a commit. +### Theme settings + +Settings to change theme as exposed to the outside world, for software +that scans `index.html` (mainly misskey) + +``` +config :pleroma, Pleroma.Web.Metadata.Providers.Theme, theme_color: "#593196" +``` + +This sets the `theme-color` meta tag on `index.html`, and is basically +a hack to make misskey find the right thing. + ## Ephemeral activities (Pleroma.Workers.PurgeExpiredActivity) Settings to enable and configure expiration for ephemeral activities diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index b55d980e9..0f4f5a358 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -47,7 +47,6 @@ defmodule Pleroma.Application do # Disable warnings_as_errors at runtime, it breaks Phoenix live reload # due to protocol consolidation warnings Code.compiler_options(warnings_as_errors: false) - Pleroma.Telemetry.Logger.attach() Config.Holder.save_default() Pleroma.HTML.compile_scrubbers() Pleroma.Config.Oban.warn() diff --git a/lib/pleroma/telemetry/logger.ex b/lib/pleroma/telemetry/logger.ex deleted file mode 100644 index 4c781c504..000000000 --- a/lib/pleroma/telemetry/logger.ex +++ /dev/null @@ -1,85 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2021 Pleroma Authors -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Telemetry.Logger do - @moduledoc "Transforms Pleroma telemetry events to logs" - - require Logger - - @events [ - [:pleroma, :repo, :query] - ] - def attach do - :telemetry.attach_many( - "pleroma-logger", - @events, - &Pleroma.Telemetry.Logger.handle_event/4, - [] - ) - end - - # Passing anonymous functions instead of strings to logger is intentional, - # that way strings won't be concatenated if the message is going to be thrown - # out anyway due to higher log level configured - - def handle_event( - [:pleroma, :repo, :query] = _name, - %{query_time: query_time} = measurements, - %{source: source} = metadata, - config - ) do - logging_config = Pleroma.Config.get([:telemetry, :slow_queries_logging], []) - - if logging_config[:enabled] && - logging_config[:min_duration] && - query_time > logging_config[:min_duration] and - (is_nil(logging_config[:exclude_sources]) or - source not in logging_config[:exclude_sources]) do - log_slow_query(measurements, metadata, config) - else - :ok - end - end - - defp log_slow_query( - %{query_time: query_time} = _measurements, - %{source: _source, query: query, params: query_params, repo: repo} = _metadata, - _config - ) do - sql_explain = - with {:ok, %{rows: explain_result_rows}} <- - repo.query("EXPLAIN " <> query, query_params, log: false) do - Enum.map_join(explain_result_rows, "\n", & &1) - end - - {:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace) - - pleroma_stacktrace = - Enum.filter(stacktrace, fn - {__MODULE__, _, _, _} -> - false - - {mod, _, _, _} -> - mod - |> to_string() - |> String.starts_with?("Elixir.Pleroma.") - end) - - Logger.warn(fn -> - """ - Slow query! - - Total time: #{round(query_time / 1_000)} ms - - #{query} - - #{inspect(query_params, limit: :infinity)} - - #{sql_explain} - - #{Exception.format_stacktrace(pleroma_stacktrace)} - """ - end) - end -end diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex index 9d73cf0f8..9ba98d0a8 100644 --- a/lib/pleroma/web/endpoint.ex +++ b/lib/pleroma/web/endpoint.ex @@ -12,8 +12,6 @@ defmodule Pleroma.Web.Endpoint do socket("/socket", Pleroma.Web.UserSocket) socket("/live", Phoenix.LiveView.Socket) - plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint]) - plug(Pleroma.Web.Plugs.SetLocalePlug) plug(CORSPlug) plug(Pleroma.Web.Plugs.HTTPSecurityPlug) diff --git a/lib/pleroma/web/fallback/redirect_controller.ex b/lib/pleroma/web/fallback/redirect_controller.ex index 5fca290e5..49f659cf0 100644 --- a/lib/pleroma/web/fallback/redirect_controller.ex +++ b/lib/pleroma/web/fallback/redirect_controller.ex @@ -55,11 +55,12 @@ defmodule Pleroma.Web.Fallback.RedirectController do def redirector_with_preload(conn, params) do {:ok, index_content} = File.read(index_file_path()) preloads = preload_data(conn, params) + tags = Metadata.build_static_tags(params) title = "#{Pleroma.Config.get([:instance, :name])}" response = index_content - |> String.replace("", preloads <> title) + |> String.replace("", tags <> preloads <> title) conn |> put_resp_content_type("text/html") diff --git a/lib/pleroma/web/metadata.ex b/lib/pleroma/web/metadata.ex index 46ef00c08..48801b588 100644 --- a/lib/pleroma/web/metadata.ex +++ b/lib/pleroma/web/metadata.ex @@ -5,10 +5,28 @@ defmodule Pleroma.Web.Metadata do alias Phoenix.HTML + def build_static_tags(params) do + providers = [ + Pleroma.Web.Metadata.Providers.Theme + ] + + Enum.reduce(providers, "", fn parser, acc -> + rendered_html = + params + |> parser.build_tags() + |> Enum.map(&to_tag/1) + |> Enum.map(&HTML.safe_to_string/1) + |> Enum.join() + + acc <> rendered_html + end) + end + def build_tags(params) do providers = [ Pleroma.Web.Metadata.Providers.RelMe, - Pleroma.Web.Metadata.Providers.RestrictIndexing + Pleroma.Web.Metadata.Providers.RestrictIndexing, + Pleroma.Web.Metadata.Providers.Theme | activated_providers() ] diff --git a/lib/pleroma/web/metadata/providers/theme.ex b/lib/pleroma/web/metadata/providers/theme.ex new file mode 100644 index 000000000..581ff7624 --- /dev/null +++ b/lib/pleroma/web/metadata/providers/theme.ex @@ -0,0 +1,16 @@ +defmodule Pleroma.Web.Metadata.Providers.Theme do + alias Pleroma.Web.Metadata.Providers.Provider + + @behaviour Provider + + @impl Provider + def build_tags(_) do + [ + {:meta, + [ + name: "theme-color", + content: Pleroma.Config.get([__MODULE__, :theme_color]) + ], []} + ] + end +end diff --git a/test/pleroma/web/metadata/providers/theme_test.exs b/test/pleroma/web/metadata/providers/theme_test.exs new file mode 100644 index 000000000..0c2c1e431 --- /dev/null +++ b/test/pleroma/web/metadata/providers/theme_test.exs @@ -0,0 +1,12 @@ +defmodule Pleroma.Web.Metadata.Providers.ThemeTest do + use Pleroma.DataCase + alias Pleroma.Web.Metadata.Providers.Theme + + setup do: clear_config([Pleroma.Web.Metadata.Providers.Theme, :theme_color], "configured") + + test "it renders the theme-color meta tag" do + result = Theme.build_tags(%{}) + + assert {:meta, [name: "theme-color", content: "configured"], []} in result + end +end