aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/web/preload
diff options
context:
space:
mode:
authorstwf <steven.fuchs@dockyard.com>2020-05-12 11:08:00 -0400
committerstwf <steven.fuchs@dockyard.com>2020-06-03 16:34:03 -0400
commit29ae5bb77166d9d7f8108a965b0c3d147b747e80 (patch)
tree4761fc69f5a71cc0b5c467705acd9ada46a79aec /lib/pleroma/web/preload
parentd6693a91028ab3ee7865f530f3ad2532542fc2d2 (diff)
downloadpleroma-29ae5bb77166d9d7f8108a965b0c3d147b747e80.tar.gz
preload data into index.html
Diffstat (limited to 'lib/pleroma/web/preload')
-rw-r--r--lib/pleroma/web/preload/instance.ex49
-rw-r--r--lib/pleroma/web/preload/provider.ex7
-rw-r--r--lib/pleroma/web/preload/timelines.ex42
-rw-r--r--lib/pleroma/web/preload/user.ex25
4 files changed, 123 insertions, 0 deletions
diff --git a/lib/pleroma/web/preload/instance.ex b/lib/pleroma/web/preload/instance.ex
new file mode 100644
index 000000000..0b6fd3313
--- /dev/null
+++ b/lib/pleroma/web/preload/instance.ex
@@ -0,0 +1,49 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.Preload.Providers.Instance do
+ 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"
+
+ @impl Provider
+ def generate_terms(_params) do
+ %{}
+ |> build_info_tag()
+ |> build_panel_tag()
+ |> build_nodeinfo_tag()
+ end
+
+ defp build_info_tag(acc) do
+ info_data = InstanceView.render("show.json", %{})
+
+ Map.put(acc, @instance_url, info_data)
+ end
+
+ defp build_panel_tag(acc) do
+ instance_path = Path.join(:code.priv_dir(:pleroma), "static/instance/panel.html")
+
+ if File.exists?(instance_path) do
+ panel_data = File.read!(instance_path)
+ Map.put(acc, @panel_url, panel_data)
+ else
+ acc
+ end
+ end
+
+ defp build_nodeinfo_tag(acc) do
+ case Nodeinfo.get_nodeinfo("2.0") do
+ {:error, _} ->
+ acc
+
+ nodeinfo_data ->
+ Map.put(acc, @nodeinfo_url, nodeinfo_data)
+ end
+ end
+end
diff --git a/lib/pleroma/web/preload/provider.ex b/lib/pleroma/web/preload/provider.ex
new file mode 100644
index 000000000..7ef595a34
--- /dev/null
+++ b/lib/pleroma/web/preload/provider.ex
@@ -0,0 +1,7 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.Preload.Providers.Provider do
+ @callback generate_terms(map()) :: map()
+end
diff --git a/lib/pleroma/web/preload/timelines.ex b/lib/pleroma/web/preload/timelines.ex
new file mode 100644
index 000000000..dbd7db407
--- /dev/null
+++ b/lib/pleroma/web/preload/timelines.ex
@@ -0,0 +1,42 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.Preload.Providers.Timelines do
+ alias Pleroma.Web.ActivityPub.ActivityPub
+ alias Pleroma.Web.MastodonAPI.StatusView
+ alias Pleroma.Web.Preload.Providers.Provider
+
+ @behaviour Provider
+ @public_url :"/api/v1/timelines/public"
+
+ @impl Provider
+ def generate_terms(_params) do
+ build_public_tag(%{})
+ end
+
+ def build_public_tag(acc) do
+ if Pleroma.Config.get([:restrict_unauthenticated, :timelines, :federated], true) do
+ acc
+ else
+ Map.put(acc, @public_url, public_timeline(nil))
+ end
+ end
+
+ defp public_timeline(user) do
+ activities =
+ create_timeline_params(user)
+ |> Map.put("local_only", false)
+ |> ActivityPub.fetch_public_activities()
+
+ StatusView.render("index.json", activities: activities, for: user, as: :activity)
+ end
+
+ defp create_timeline_params(user) do
+ %{}
+ |> Map.put("type", ["Create", "Announce"])
+ |> Map.put("blocking_user", user)
+ |> Map.put("muting_user", user)
+ |> Map.put("user", user)
+ end
+end
diff --git a/lib/pleroma/web/preload/user.ex b/lib/pleroma/web/preload/user.ex
new file mode 100644
index 000000000..3a244845b
--- /dev/null
+++ b/lib/pleroma/web/preload/user.ex
@@ -0,0 +1,25 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.Preload.Providers.User do
+ alias Pleroma.Web.MastodonAPI.AccountView
+ alias Pleroma.Web.Preload.Providers.Provider
+
+ @behaviour Provider
+ @account_url :"/api/v1/accounts"
+
+ @impl Provider
+ def generate_terms(%{user: user}) do
+ build_accounts_tag(%{}, user)
+ end
+
+ def generate_terms(_params), do: %{}
+
+ def build_accounts_tag(acc, nil), do: acc
+
+ def build_accounts_tag(acc, user) do
+ account_data = AccountView.render("show.json", %{user: user, for: user})
+ Map.put(acc, @account_url, account_data)
+ end
+end