From 83e5356e80ba50fee5e0cb4f932f3a412fbdd12a Mon Sep 17 00:00:00 2001 From: Inhji Date: Thu, 9 Mar 2023 21:43:56 +0100 Subject: [PATCH] add global_assigns and public pipeline --- lib/chiya_web/global_assigns.ex | 21 +++++++++++++++++++++ lib/chiya_web/router.ex | 8 +++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 lib/chiya_web/global_assigns.ex diff --git a/lib/chiya_web/global_assigns.ex b/lib/chiya_web/global_assigns.ex new file mode 100644 index 0000000..923049e --- /dev/null +++ b/lib/chiya_web/global_assigns.ex @@ -0,0 +1,21 @@ +defmodule ChiyaWeb.GlobalAssigns do + import Plug.Conn + + def fetch_settings(conn, _opts) do + settings = Chiya.Site.get_settings() + assign(conn, :settings, settings) + end + + def fetch_identities(conn, _opts) do + identities = Chiya.Identities.list_identities() + assign(conn, :identities, identities) + end + + def fetch_public_channels(conn, _opts) do + channels = + Chiya.Channels.list_channels() + |> Enum.filter(fn c -> c.visibility == :public end) + + assign(conn, :channels, channels) + end +end diff --git a/lib/chiya_web/router.ex b/lib/chiya_web/router.ex index 92047a0..56f2996 100644 --- a/lib/chiya_web/router.ex +++ b/lib/chiya_web/router.ex @@ -2,6 +2,7 @@ defmodule ChiyaWeb.Router do use ChiyaWeb, :router import ChiyaWeb.UserAuth + import ChiyaWeb.GlobalAssigns pipeline :browser do plug :accepts, ["html"] @@ -13,6 +14,11 @@ defmodule ChiyaWeb.Router do plug :fetch_current_user end + pipeline :public do + plug :fetch_settings + plug :fetch_identities + end + pipeline :api do plug :accepts, ["json"] end @@ -93,7 +99,7 @@ defmodule ChiyaWeb.Router do ## Public routes scope "/", ChiyaWeb do - pipe_through :browser + pipe_through [:browser, :public] get "/:slug", PageController, :channel get "/", PageController, :home