Expose /manifest.json for PWA
authorAlex Gleason <alex@alexgleason.me>
Wed, 24 Nov 2021 22:45:05 +0000 (16:45 -0600)
committerAlex Gleason <alex@alexgleason.me>
Wed, 24 Nov 2021 23:50:55 +0000 (17:50 -0600)
lib/pleroma/web/manifest_controller.ex [new file with mode: 0644]
lib/pleroma/web/router.ex
lib/pleroma/web/views/manifest_view.ex [new file with mode: 0644]
test/pleroma/web/manifest_controller_test.exs [new file with mode: 0644]
test/pleroma/web/plugs/frontend_static_plug_test.exs

diff --git a/lib/pleroma/web/manifest_controller.ex b/lib/pleroma/web/manifest_controller.ex
new file mode 100644 (file)
index 0000000..5258954
--- /dev/null
@@ -0,0 +1,14 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ManifestController do
+  use Pleroma.Web, :controller
+
+  plug(:skip_auth when action == :show)
+
+  @doc "GET /manifest.json"
+  def show(conn, _params) do
+    render(conn, "manifest.json")
+  end
+end
index efca7078a178344c091e17ac61f748bee68e65ba..c3b74e622efa34c21703d1ddf58f0dc311509863 100644 (file)
@@ -750,6 +750,12 @@ defmodule Pleroma.Web.Router do
     get("/web/manifest.json", MastoFEController, :manifest)
   end
 
+  scope "/", Pleroma.Web do
+    pipe_through(:api)
+
+    get("/manifest.json", ManifestController, :show)
+  end
+
   scope "/", Pleroma.Web do
     pipe_through(:mastodon_html)
 
diff --git a/lib/pleroma/web/views/manifest_view.ex b/lib/pleroma/web/views/manifest_view.ex
new file mode 100644 (file)
index 0000000..cc78ea3
--- /dev/null
@@ -0,0 +1,28 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ManifestView do
+  use Pleroma.Web, :view
+  alias Pleroma.Config
+  alias Pleroma.Web.Endpoint
+
+  def render("manifest.json", _params) do
+    %{
+      name: Config.get([:instance, :name]),
+      description: Config.get([:instance, :description]),
+      icons: Config.get([:manifest, :icons]),
+      theme_color: Config.get([:manifest, :theme_color]),
+      background_color: Config.get([:manifest, :background_color]),
+      display: "standalone",
+      scope: Endpoint.url(),
+      start_url: "/",
+      categories: [
+        "social"
+      ],
+      serviceworker: %{
+        src: "/sw.js"
+      }
+    }
+  end
+end
diff --git a/test/pleroma/web/manifest_controller_test.exs b/test/pleroma/web/manifest_controller_test.exs
new file mode 100644 (file)
index 0000000..b7a4940
--- /dev/null
@@ -0,0 +1,17 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ManifestControllerTest do
+  use Pleroma.Web.ConnCase
+
+  setup do
+    clear_config([:instance, :name], "Manifest Test")
+    clear_config([:manifest, :theme_color], "#ff0000")
+  end
+
+  test "manifest.json", %{conn: conn} do
+    conn = get(conn, "/manifest.json")
+    assert %{"name" => "Manifest Test", "theme_color" => "#ff0000"} = json_response(conn, 200)
+  end
+end
index 4152cdefe90ebd572254a5954dc98aeff768f33e..a1cce6398e0a2c89e08e857d1de48e683bde8b34 100644 (file)
@@ -95,6 +95,7 @@ defmodule Pleroma.Web.Plugs.FrontendStaticPlugTest do
       ".well-known",
       "nodeinfo",
       "web",
+      "manifest.json",
       "auth",
       "embed",
       "proxy",