Skip to content

Commit

Permalink
persist chats
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleecreates committed Jul 7, 2024
1 parent 3cf7c82 commit 5ab9017
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 279 deletions.
6 changes: 0 additions & 6 deletions src/chatservice/config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ config :chatservice, ChatServiceWeb.Endpoint,
secret_key_base: "cfHSrMhdqQLdzdAiLRZazXjUBlnd12ZuG3ilwKigBsbA58cOWzW0Rm2cUa5oF8ts",
server: false

# In test we don't send emails.
config :chatservice, ChatService.Mailer, adapter: Swoosh.Adapters.Test

# Disable swoosh api client as it is only required for production adapters.
config :swoosh, :api_client, false

# Print only warnings and errors during test
config :logger, level: :warning

Expand Down
5 changes: 1 addition & 4 deletions src/chatservice/lib/chatservice.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
defmodule ChatService do
@moduledoc """
ChatService keeps the contexts that define your domain
and business logic.
The ChatService supervision tree is in chatservice/application.ex
Contexts are also responsible for managing your data, regardless
if it comes from the database, an external API or others.
"""
end
1 change: 0 additions & 1 deletion src/chatservice/lib/chatservice/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ defmodule ChatService.Application do
children = [
ChatServiceWeb.Telemetry,
ChatService.Repo,
# {DNSCluster, query: Application.get_env(:chatservice, :dns_cluster_query) || :ignore},
{Phoenix.PubSub, name: ChatService.PubSub},
{Registry, keys: :unique, name: ChatService.Registry},
ChatServiceWeb.Endpoint
Expand Down
22 changes: 22 additions & 0 deletions src/chatservice/lib/chatservice/chat_context.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule ChatService.ChatContext do
@moduledoc """
The database/persistence context
"""

import Ecto.Query, warn: false
alias ChatService.Repo

alias ChatService.ChatContext.Message

def list_messages(topic) do
Message
|> where(topic: ^topic)
|> Repo.all()
end

def create_message(attrs \\ %{}) do
%Message{}
|> Message.changeset(attrs)
|> Repo.insert()
end
end
20 changes: 20 additions & 0 deletions src/chatservice/lib/chatservice/chat_context/message.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule ChatService.ChatContext.Message do
@derive {Jason.Encoder, only: [:name, :message, :inserted_at, :topic]}
use Ecto.Schema
import Ecto.Changeset

schema "messages" do
field :topic, :string
field :name, :string
field :message, :string

timestamps(type: :utc_datetime)
end

@doc false
def changeset(message, attrs) do
message
|> cast(attrs, [:topic, :name, :message])
|> validate_required([:topic, :name, :message])
end
end
26 changes: 15 additions & 11 deletions src/chatservice/lib/chatservice/chat_server.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule ChatService.ChatServer do
require OpenTelemetry.Tracer
use GenServer
alias ChatService.ChatContext

def start_chat(topic) do
case Registry.lookup(ChatService.Registry, topic) do
Expand All @@ -8,34 +9,37 @@ defmodule ChatService.ChatServer do
end
end

def start_link(topic) do
GenServer.start_link(__MODULE__, %{}, name: via_tuple(topic))
end

def list_topics() do
ChatService.Registry
|> Registry.select([{{:"$1", :_, :_}, [], [{{:"$1"}}]}])
|> Enum.map(fn {key} -> key end)
end

def send_message(topic, message) do
OpenTelemetry.Tracer.with_span :send_message do
GenServer.call(via_tuple(topic), {:send_message, message})
end
GenServer.call(via_tuple(topic), {:send_message, Map.put(message, "topic", topic)})
end

def get_messages(topic) do
GenServer.call(via_tuple(topic), :get_messages)
end

def init(_) do
{:ok, []}
def start_link(topic) do
GenServer.start_link(__MODULE__, topic, name: via_tuple(topic))
end

@impl true
def init(topic) do
messages = ChatContext.list_messages(topic)
{:ok, messages}
end

@impl true
def handle_call({:send_message, message}, _from, state) do
{:reply, :ok, [message | state]}
saved = ChatContext.create_message(message)
{:reply, saved, [saved | state]}
end

@impl true
def handle_call(:get_messages, _from, state) do
{:reply, Enum.reverse(state), state}
end
Expand Down
1 change: 0 additions & 1 deletion src/chatservice/lib/chatservice_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ defmodule ChatServiceWeb do
layouts: [html: ChatServiceWeb.Layouts]

import Plug.Conn
import ChatServiceWeb.Gettext

unquote(verified_routes())
end
Expand Down
24 changes: 0 additions & 24 deletions src/chatservice/lib/chatservice_web/gettext.ex

This file was deleted.

112 changes: 0 additions & 112 deletions src/chatservice/priv/gettext/en/LC_MESSAGES/errors.po

This file was deleted.

109 changes: 0 additions & 109 deletions src/chatservice/priv/gettext/errors.pot

This file was deleted.

Loading

0 comments on commit 5ab9017

Please sign in to comment.