-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
defmodule Swipex.Chat do | ||
def send_message(from, to, content) do | ||
conn = Bolt.Sips.conn() | ||
|
||
Bolt.Sips.query( | ||
conn, | ||
""" | ||
MATCH (u1:User {id: $from}) | ||
MATCH (u2:User {id: $to}) | ||
CREATE (u1)-[:MESSAGE {content: $content, timestamp: $time, from: $from, to: $to}]->(u2) | ||
""", | ||
%{ | ||
from: from, | ||
to: to, | ||
content: content, | ||
time: DateTime.utc_now() | ||
} | ||
) | ||
end | ||
|
||
def get_messages(from, to) do | ||
conn = Bolt.Sips.conn() | ||
|
||
{:ok, %Bolt.Sips.Response{results: sent}} = | ||
Bolt.Sips.Query.query( | ||
conn, | ||
""" | ||
MATCH ()-[sent:MESSAGE { from: $from, to: $to }]-() | ||
RETURN sent | ||
""", | ||
%{from: from, to: to} | ||
) | ||
|
||
{:ok, %Bolt.Sips.Response{results: received}} = | ||
Bolt.Sips.Query.query( | ||
conn, | ||
""" | ||
MATCH ()-[received:MESSAGE { from: $to, to: $from }]->() | ||
RETURN received | ||
""", | ||
%{from: from, to: to} | ||
) | ||
|
||
sent_messages = | ||
sent | ||
|> Enum.map(&Map.get(&1, "sent")) | ||
|> Enum.uniq_by(&Map.get(&1, :id)) | ||
|> Enum.map(&Map.get(&1, :properties)) | ||
|
||
received_messages = | ||
received | ||
|> Enum.map(&Map.get(&1, "received")) | ||
|> Enum.uniq_by(&Map.get(&1, :id)) | ||
|> Enum.map(&Map.get(&1, :properties)) | ||
|
||
Enum.concat(sent_messages, received_messages) | ||
|> Enum.sort(&(DateTime.compare(Map.get(&1, "timestamp"), Map.get(&2, "timestamp")) != :gt)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
defmodule SwipexWeb.ChatLive do | ||
use SwipexWeb, :live_view | ||
alias Phoenix.PubSub | ||
|
||
def mount(%{"id" => id}, %{"user_id" => _user_id}, socket) do | ||
case Swipex.User.get_user_by_id(id) do | ||
{:ok, user} -> | ||
PubSub.subscribe(Swipex.PubSub, "swipex") | ||
{:ok, assign(socket, :recipient, user)} | ||
|
||
{:error, _} -> | ||
{:ok, socket |> put_flash(:error, "Something went wrong.") |> redirect(to: "/profile")} | ||
end | ||
end | ||
|
||
def mount(_params, _session, socket) do | ||
{:ok, | ||
socket | ||
|> put_flash(:error, "You must be logged in to access this page.") | ||
|> redirect(to: "/login")} | ||
end | ||
|
||
def handle_event("send", %{"content" => content}, socket) do | ||
case Swipex.Chat.send_message( | ||
socket.assigns.user["id"], | ||
socket.assigns.recipient["id"], | ||
content | ||
) do | ||
{:ok, _} -> | ||
PubSub.broadcast(Swipex.PubSub, "swipex", {:message, socket.assigns.recipient["id"]}) | ||
{:noreply, socket |> put_flash(:info, "Message sent.")} | ||
|
||
{:error, _} -> | ||
{:noreply, socket |> put_flash(:error, "Failed to send message.")} | ||
end | ||
end | ||
|
||
def handle_info({:message, id}, socket) do | ||
if id == socket.assigns.user["id"] do | ||
{:noreply, socket |> put_flash(:info, "You have a new message!")} | ||
else | ||
{:noreply, socket} | ||
end | ||
end | ||
|
||
def render(assigns) do | ||
assigns = | ||
assign( | ||
assigns, | ||
:messages, | ||
Swipex.Chat.get_messages(assigns.user["id"], assigns.recipient["id"]) | ||
) | ||
|
||
~H""" | ||
<h1 class="text-4xl mb-2">Chat with <%= @recipient["name"] %></h1> | ||
<hr /> | ||
<div class="flex flex-col mt-4"> | ||
<%= for message <- @messages do %> | ||
<div class="flex flex-row mb-2"> | ||
<div class={"p-2 rounded-lg #{ | ||
if message["from"] == @user["id"], do: "bg-blue-600 text-white ml-auto", else: "bg-gray-200" | ||
}"}> | ||
<%= message["content"] %> | ||
</div> | ||
</div> | ||
<% end %> | ||
<form phx-submit="send" class="flex flex-row gap-2"> | ||
<input type="text" name="content" placeholder="Message" class="flex-1" /> | ||
<input type="submit" value="Send" class="bg-blue-500 text-white rounded-lg p-2" /> | ||
</form> | ||
</div> | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters