From 1b3acf75b04fe354d9f5b44f4e63e00dad3ea0d3 Mon Sep 17 00:00:00 2001 From: Peter Saxton Date: Fri, 17 Apr 2020 16:02:51 +0100 Subject: [PATCH] implement session --- lib/calm.ex | 9 ---- lib/calm/www.ex | 5 ++- lib/calm/www/actions/home_page.ex | 45 ++++++++------------ lib/calm/www/actions/home_page.html.eex | 52 ++++++++++++++++------- lib/calm/www/actions/open_invite.ex | 4 +- lib/calm/www/actions/post_message.ex | 4 +- lib/calm/www/actions/thread_page.ex | 17 ++++++-- lib/calm/www/actions/thread_page.html.eex | 9 ++-- lib/mix/tasks/create_invite.ex | 2 - 9 files changed, 79 insertions(+), 68 deletions(-) diff --git a/lib/calm.ex b/lib/calm.ex index 947de39..aa38b3f 100644 --- a/lib/calm.ex +++ b/lib/calm.ex @@ -1,11 +1,2 @@ defmodule Calm do - def welcome_message(name, greeting \\ "Hello") - - def welcome_message(nil, _greeting) do - nil - end - - def welcome_message(name, greeting) do - "#{greeting}, #{name}!" - end end diff --git a/lib/calm/www.ex b/lib/calm/www.ex index 400165d..6721c23 100644 --- a/lib/calm/www.ex +++ b/lib/calm/www.ex @@ -10,10 +10,11 @@ defmodule Calm.WWW do end @session_config Raxx.Session.config( - key: "my_app_session", + key: "calm_session", store: Raxx.Session.SignedCookie, secret_key_base: System.get_env("SECRET_KEY_BASE"), - salt: "VSLaz9" + salt: "VSLaz9", + max_age: 365 * 24 * 60 * 60 ) # This works even if the reference file is not available at start up, diff --git a/lib/calm/www/actions/home_page.ex b/lib/calm/www/actions/home_page.ex index 565a9a1..9dff729 100644 --- a/lib/calm/www/actions/home_page.ex +++ b/lib/calm/www/actions/home_page.ex @@ -1,41 +1,30 @@ defmodule Calm.WWW.Actions.HomePage do use Raxx.SimpleServer - use Calm.WWW.Layout, arguments: [:greeting, :csrf_token] - alias Raxx.Session + use Calm.WWW.Layout, arguments: [:latest] @impl Raxx.SimpleServer - def handle_request(request = %{method: :GET}, state) do - {:ok, session} = Session.extract(request, state.session_config) + def handle_request(request = %{method: :GET}, config) do + {:ok, session} = Raxx.Session.extract(request, config.session_config) + session = session || %{threads: %{}} + thread_ids = Map.keys(session.threads) - {csrf_token, session} = Session.get_csrf_token(session) - {flash, session} = Session.pop_flash(session) - - greeting = Calm.welcome_message(session[:name]) + latest = load_latest(thread_ids) response(:ok) - |> Session.embed(session, state.session_config) - |> render(greeting, csrf_token, flash: flash) + |> render(latest) end - def handle_request(request = %{method: :POST}, state) do - data = URI.decode_query(request.body) - {:ok, session} = Session.extract(request, data["_csrf_token"], state.session_config) - - case data do - %{"name" => name} -> - session = - session - |> Map.put(:name, name) - |> Session.put_flash(:info, "Successfully changed name") + def load_latest(thread_ids) do + import Ecto.Query - redirect("/") - |> Session.embed(session, state.session_config) + query = + Calm.Message + |> order_by([m], desc: m.inserted_at) + |> join(:inner, [m], i in Calm.Invite, on: m.invite_id == i.id) + |> where([m, i], i.thread_id in ^thread_ids) + |> preload(invite: :thread) + |> distinct([m, i], i.thread_id) - _ -> - redirect("/") - end + Calm.Repo.all(query) end - - # Template helper functions. - # Add shared helper functions to Calm.WWW.Layout. end diff --git a/lib/calm/www/actions/home_page.html.eex b/lib/calm/www/actions/home_page.html.eex index 260b45a..cdea514 100644 --- a/lib/calm/www/actions/home_page.html.eex +++ b/lib/calm/www/actions/home_page.html.eex @@ -1,16 +1,38 @@ -
-
-

Calm

+
+
+

Calm: A better way to talk!!!

-

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

- -

View my inbox

- -
+
+
+ +

An experiment to keep in touch while respecting everyones time and attention.

+
+
+
+
+ <%= if latest == [] do %> +
+

You don't have any new messages.

+
+ <% end %> + <%= for update <- latest do %> + +
+ <%= update.invite.thread.subject %> + + <%= Calm.WWW.Actions.ThreadPage.update_time(update) %> +
+

<%= update.invite.nickname %>: <%= hd(String.split(update.text, ~r/\R/, parts: 2)) %>

+ +
+ <% end %> +
+ + diff --git a/lib/calm/www/actions/open_invite.ex b/lib/calm/www/actions/open_invite.ex index c70bf41..4d07353 100644 --- a/lib/calm/www/actions/open_invite.ex +++ b/lib/calm/www/actions/open_invite.ex @@ -1,7 +1,7 @@ defmodule Calm.WWW.Actions.OpenInvite do use Raxx.SimpleServer - def handle_request(request, config) do + def handle_request(request = %{method: :GET}, config) do ["i", invite_id, secret] = request.path {:ok, invite} = Calm.Invite.fetch_by_id(invite_id) {:ok, true} = Calm.Invite.check_secret(invite, secret) @@ -9,7 +9,7 @@ defmodule Calm.WWW.Actions.OpenInvite do {:ok, session} = Raxx.Session.extract(request, config.session_config) session = session || %{threads: %{}} threads = session.threads - threads = Map.put(threads, invite.thread_id, invite.id) + threads = Map.put(threads, invite.thread_id, {invite.id, secret}) session = %{session | threads: threads} redirect("/t/#{invite.thread_id}") diff --git a/lib/calm/www/actions/post_message.ex b/lib/calm/www/actions/post_message.ex index 6767b2b..f56699b 100644 --- a/lib/calm/www/actions/post_message.ex +++ b/lib/calm/www/actions/post_message.ex @@ -1,7 +1,7 @@ defmodule Calm.WWW.Actions.PostMessage do use Raxx.SimpleServer - def handle_request(request, config) do + def handle_request(request = %{method: :POST}, config) do ["t", thread_id, "post"] = request.path {thread_id, ""} = Integer.parse(thread_id) @@ -9,7 +9,7 @@ defmodule Calm.WWW.Actions.PostMessage do {:ok, csrf_token} = Map.fetch(params, "_csrf_token") {:ok, session} = Raxx.Session.extract(request, csrf_token, config.session_config) - {:ok, invite_id} = Map.fetch(session.threads, thread_id) + {:ok, {invite_id, _invite_secret}} = Map.fetch(session.threads, thread_id) {:ok, invite} = Calm.Invite.fetch_by_id(invite_id) {:ok, message} = Calm.Invite.post_message(invite, params) diff --git a/lib/calm/www/actions/thread_page.ex b/lib/calm/www/actions/thread_page.ex index 69f48be..655db37 100644 --- a/lib/calm/www/actions/thread_page.ex +++ b/lib/calm/www/actions/thread_page.ex @@ -2,11 +2,11 @@ defmodule Calm.WWW.Actions.ThreadPage do use Raxx.SimpleServer use Calm.WWW.Layout, arguments: [:invite, :messages, :csrf_token] - def handle_request(request, config) do + def handle_request(request = %{method: :GET}, config) do ["t", thread_id] = request.path {thread_id, ""} = Integer.parse(thread_id) {:ok, session} = Raxx.Session.extract(request, config.session_config) - {:ok, invite_id} = Map.fetch(session.threads, thread_id) + {:ok, {invite_id, _invite_secret}} = Map.fetch(session.threads, thread_id) {:ok, invite} = Calm.Invite.fetch_by_id(invite_id) messages = Calm.Message.load_thread(invite.thread_id) @@ -14,7 +14,18 @@ defmodule Calm.WWW.Actions.ThreadPage do {csrf_token, session} = Raxx.Session.get_csrf_token(session) response(:ok) - |> render(invite, messages, csrf_token) + |> render(invite, messages, csrf_token, title: invite.thread.subject) |> Raxx.Session.embed(session, config.session_config) end + + def update_time(message) do + today = "#{Date.utc_today()}" + + %Calm.Message{inserted_at: inserted_at} = message + + inserted_at + |> DateTime.to_string() + |> String.slice(0, 16) + |> String.replace(today, "Today") + end end diff --git a/lib/calm/www/actions/thread_page.html.eex b/lib/calm/www/actions/thread_page.html.eex index b6aee43..bbc727b 100644 --- a/lib/calm/www/actions/thread_page.html.eex +++ b/lib/calm/www/actions/thread_page.html.eex @@ -4,18 +4,16 @@
<%= for update <- messages do %> -
+
<%= update.invite.nickname %> - <%= update.inserted_at %> + <%= update_time(update) %>

<%= update.text %>

<% end %>
- -