Skip to content

Commit

Permalink
implement session
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowdHailer committed Apr 17, 2020
1 parent 7d97276 commit 1b3acf7
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 68 deletions.
9 changes: 0 additions & 9 deletions lib/calm.ex
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions lib/calm/www.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
45 changes: 17 additions & 28 deletions lib/calm/www/actions/home_page.ex
Original file line number Diff line number Diff line change
@@ -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
52 changes: 37 additions & 15 deletions lib/calm/www/actions/home_page.html.eex
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
<main class="p-6">
<header class="p-4 text-2xl">
<h1>Calm</h1>
<div class="flex flex-col h-full overflow-y-hidden mx-auto max-w-3xl bg-white shadow-2xl overflow-hidden">
<header class="bg-gray-300">
<h1 class="p-4 text-2xl truncate">Calm: A better way to talk!!!</h1>
</header>
<p>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.</p>
<textarea class="block border-2 w-full" ></textarea>
<h2 class="text-2xl ">View my inbox</h2>
<!-- <form action="/" method="post">
<input type="hidden" name="_csrf_token" value="<%= csrf_token %>">
<label>
<span>tell us your name</span>
<input type="text" name="name" value="" autofocus>
</label>
<button type="submit">Submit</button>
</form> -->
</main>
<div id="updates" class="flex-auto overflow-y-auto">
<article class="mx-4 my-4">
<!-- <header class="my-2">
<span class="inline-block mr-2 px-4 rounded bg-blue-700 text-white">The team</span>
<span class="text-gray-500">2020</span>
</header> -->
<p class="whitespace-pre-line">An experiment to keep in touch while respecting everyones time and attention.</p>
</article>
<div class="mx-4">
<hr class="w-1/2 mx-auto">
</div>
<%= if latest == [] do %>
<article class="mx-4 my-4">
<p class="whitespace-pre-line">You don't have any new messages.</p>
</article>
<% end %>
<%= for update <- latest do %>
<a class="block mx-4 my-6 border-l-2 pl-4 border-black cursor-pointer" href="/t/<%= update.invite.thread_id %>">
<header class="">
<span class="inline-block mr-2 font-bold"><%= update.invite.thread.subject %></span>
<!-- <span class="inline-block mr-2 px-4 rounded bg-<%= update.invite.color %>-700 text-white"><%= update.invite.nickname %></span> -->
<span class="text-gray-500"><%= Calm.WWW.Actions.ThreadPage.update_time(update) %></span>
</header>
<p class="whitespace-pre-line text-gray-600"><%= update.invite.nickname %>: <%= hd(String.split(update.text, ~r/\R/, parts: 2)) %></p>
<!-- <p class="whitespace-pre-line"><%= update.invite.nickname %>: <%= update.text %></p> -->
</a>
<% end %>
</div>
<footer class="">
<div class="mx-4 my-4 text-right">
<a class="text-blue-500" href="#">View on Github</a>
</div>
</footer>
</div>
4 changes: 2 additions & 2 deletions lib/calm/www/actions/open_invite.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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)

{: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}")
Expand Down
4 changes: 2 additions & 2 deletions lib/calm/www/actions/post_message.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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)

params = get_form(request)
{: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)
Expand Down
17 changes: 14 additions & 3 deletions lib/calm/www/actions/thread_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@ 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)

{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
9 changes: 4 additions & 5 deletions lib/calm/www/actions/thread_page.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@
</header>
<div id="updates" class="flex-auto overflow-y-auto">
<%= for update <- messages do %>
<article id="<%= update.cursor %>" class="mx-4 my-6">
<article id="<%= update.cursor %>" class="mx-4 my-4">
<header class="my-2">
<span class="inline-block mr-2 px-4 rounded bg-<%= update.invite.color %>-700 text-white"><%= update.invite.nickname %></span>
<span class="text-gray-500"><%= update.inserted_at %></span>
<span class="text-gray-500"><%= update_time(update) %></span>
</header>
<p class="whitespace-pre-line"><%= update.text %></p>
</article>

<% end %>
</div>
<!-- TODO show who you are writing as -->
<!-- TODO Poll for updates -->
<!-- TODO Readme, pretty much all JS is optional -->
<footer class="">
<div class="mx-4 my-2 p-2 border-2 rounded focus-within:border-gray-500">
<form class="" action="/t/<%= invite.thread.id %>/post" method="post">
<input type="hidden" name="_csrf_token" value="<%= csrf_token %>">
<textarea class="w-full resize-y rounded focus:outline-none overflow-auto" placeholder="Write an update.
To share a link include the 'http' prefix." style="min-height:4.5em;max-height:40vh;" name="text"></textarea>
<div class="flex flex-row-reverse">
<div class="flex flex-row-reverse items-center">
<button class="font-bold mx-1 py-1 px-4 bg-gray-200 hover:bg-gray-400 rounded" type="submit">Send</button>
<button class="text-gray-700 mx-1 py-1 px-4 hover:bg-gray-300 rounded" type="reset">Discard</button>
<!-- <span class="block mr-auto ml-2">Write message as <span class="inline-block mr-2 px-4 rounded bg-<%= invite.color %>-700 text-white"><%= invite.nickname %></span> </span> -->
</div>
</form>
</div>
Expand Down
2 changes: 0 additions & 2 deletions lib/mix/tasks/create_invite.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ defmodule Mix.Tasks.CreateInvite do
[thread_id, nickname, color] = args
{:ok, thread} = Calm.Thread.fetch_by_id(thread_id)
secret = Calm.Invite.generate_secret()
IO.inspect(thread)

{:ok, invite} =
Calm.Invite.create(thread, %{"nickname" => nickname, "color" => color}, secret)

IO.inspect(invite)
Logger.info("Invite created with path: '/i/#{invite.id}/#{secret}'")
end
end

0 comments on commit 1b3acf7

Please sign in to comment.