Skip to content
4 changes: 2 additions & 2 deletions lib/ex_chat/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ defmodule ExChat.Application do

def init(:ok) do
children = [
{Registry, keys: :unique, name: ExChat.ChatRoomRegistry},
{Registry, keys: :unique, name: ExChat.RoomRegistry},
{Registry, keys: :unique, name: ExChat.UserSessionRegistry},
ExChat.ChatRooms,
ExChat.Rooms,
ExChat.UserSessions,
ExChat.AccessTokenRepository,
ExChat.Setup,
Expand Down
10 changes: 5 additions & 5 deletions lib/ex_chat/chat_room.ex → lib/ex_chat/room.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
defmodule ExChat.ChatRoom do
defmodule ExChat.Room do
use GenServer

alias ExChat.UserSessions

defstruct session_ids: [], name: nil

def create(name = {:via, Registry, {_registry_name, chatroom_name}}) do
GenServer.start_link(__MODULE__, %__MODULE__{name: chatroom_name}, name: name)
def create(name = {:via, Registry, {_registry_name, room_name}}) do
GenServer.start_link(__MODULE__, %__MODULE__{name: room_name}, name: name)
end
def create(chatroom_name) do
GenServer.start_link(__MODULE__, %__MODULE__{name: chatroom_name}, name: String.to_atom(chatroom_name))
def create(room_name) do
GenServer.start_link(__MODULE__, %__MODULE__{name: room_name}, name: String.to_atom(room_name))
end

def start_link(name), do: create(name)
Expand Down
22 changes: 11 additions & 11 deletions lib/ex_chat/chat_rooms.ex → lib/ex_chat/rooms.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ExChat.ChatRooms do
defmodule ExChat.Rooms do
use DynamicSupervisor

alias ExChat.{ChatRoom, ChatRoomRegistry}
alias ExChat.{Room, RoomRegistry}

##############
# Client API #
Expand All @@ -20,21 +20,21 @@ defmodule ExChat.ChatRooms do
def join(room, [as: session_id]) do
case find(room) do
{:ok, pid} ->
try_join_chatroom(pid, session_id)
try_join_room(pid, session_id)
{:error, :unexisting_room} ->
{:error, :unexisting_room}
end
end

def send(message, [to: room, as: session_id]) do
case find(room) do
{:ok, pid} -> ChatRoom.send(pid, message, as: session_id)
{:ok, pid} -> Room.send(pid, message, as: session_id)
error -> error
end
end

defp try_join_chatroom(chatroom_pid, session_id) do
case ChatRoom.join(chatroom_pid, session_id) do
defp try_join_room(room_pid, session_id) do
case Room.join(room_pid, session_id) do
:ok ->
:ok
{:error, :already_joined} ->
Expand All @@ -43,7 +43,7 @@ defmodule ExChat.ChatRooms do
end

defp find(room) do
case Registry.lookup(ChatRoomRegistry, room) do
case Registry.lookup(RoomRegistry, room) do
[] -> {:error, :unexisting_room}
[{pid, nil}] -> {:ok, pid}
end
Expand All @@ -54,16 +54,16 @@ defmodule ExChat.ChatRooms do
####################

def start_link(_opts) do
DynamicSupervisor.start_link(__MODULE__, [], name: :chatroom_supervisor)
DynamicSupervisor.start_link(__MODULE__, [], name: :room_supervisor)
end

def init(_) do
DynamicSupervisor.init(strategy: :one_for_one)
end

defp start(chatroom_name) do
name = {:via, Registry, {ChatRoomRegistry, chatroom_name}}
defp start(room_name) do
name = {:via, Registry, {RoomRegistry, room_name}}

DynamicSupervisor.start_child(:chatroom_supervisor, {ChatRoom, name})
DynamicSupervisor.start_child(:room_supervisor, {Room, name})
end
end
4 changes: 2 additions & 2 deletions lib/ex_chat/setup.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule ExChat.Setup do
use Task, restart: :transient

alias ExChat.ChatRooms
alias ExChat.Rooms

def start_link(_args) do
Task.start_link(__MODULE__, :run, [])
end

def run() do
ChatRooms.create("default")
Rooms.create("default")
end
end
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule ExChat.UseCases.CreateChatRoom do
defmodule ExChat.UseCases.CreateRoom do

alias ExChat.ChatRooms
alias ExChat.Rooms

def on(room) do
case ChatRooms.create(room) do
case Rooms.create(room) do
:ok ->
{:ok, "#{room} has been created!"}
{:error, :already_exists} ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule ExChat.UseCases.JoinChatRoom do
defmodule ExChat.UseCases.JoinRoom do

alias ExChat.{ChatRooms, UserSessions}
alias ExChat.{Rooms, UserSessions}

def on(room, user_id) do
case ChatRooms.join(room, as: user_id) do
case Rooms.join(room, as: user_id) do
:ok ->
UserSessions.notify(%{room: room, message: "welcome to the #{room} chat room, #{user_id}!"}, to: user_id)
:ok
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule ExChat.UseCases.SendMessageToChatRoom do
defmodule ExChat.UseCases.SendMessageToRoom do

alias ExChat.ChatRooms
alias ExChat.Rooms

def on(message, room, user_id) do
case ChatRooms.send(message, to: room, as: user_id) do
case Rooms.send(message, to: room, as: user_id) do
:ok ->
:ok
{:error, :unexisting_room} ->
Expand Down
10 changes: 5 additions & 5 deletions lib/ex_chat/web/websocket_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule ExChat.Web.WebSocketController do
@behaviour :cowboy_websocket
end

alias ExChat.UseCases.{ValidateAccessToken, SendMessageToChatRoom,
CreateChatRoom, JoinChatRoom, SubscribeToUserSession}
alias ExChat.UseCases.{ValidateAccessToken, SendMessageToRoom,
CreateRoom, JoinRoom, SubscribeToUserSession}

@default_ping_interval 30_000
@default_idle_timeout 60_000
Expand Down Expand Up @@ -48,7 +48,7 @@ defmodule ExChat.Web.WebSocketController do
end

defp handle(%{"command" => "join", "room" => room}, session_id) do
case JoinChatRoom.on(room, session_id) do
case JoinRoom.on(room, session_id) do
:ok ->
{:ok, session_id}
{:error, message} ->
Expand All @@ -61,7 +61,7 @@ defmodule ExChat.Web.WebSocketController do
end

defp handle(%{"room" => room, "message" => message}, session_id) do
case SendMessageToChatRoom.on(message, room, session_id) do
case SendMessageToRoom.on(message, room, session_id) do
{:error, message} ->
{:reply, {:text, to_json(%{ error: message })}, session_id}
:ok ->
Expand All @@ -70,7 +70,7 @@ defmodule ExChat.Web.WebSocketController do
end

defp handle(%{"command" => "create", "room" => room}, session_id) do
response = case CreateChatRoom.on(room) do
response = case CreateRoom.on(room) do
{:ok, message} -> %{success: message}
{:error, message} -> %{error: message}
end
Expand Down
10 changes: 5 additions & 5 deletions test/ex_chat/chat_room_test.exs → test/ex_chat/room_test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
defmodule ExChat.ChatRoomTest do
defmodule ExChat.RoomTest do
use ExUnit.Case, async: false

import Mock

alias ExChat.UserSessions
alias ExChat.ChatRoom
alias ExChat.Room

test "notify subscribed user session when message is received" do
{:ok, chatroom} = ChatRoom.create("room_name")
ChatRoom.join(chatroom, "a-user-session-id")
{:ok, room} = Room.create("room_name")
Room.join(room, "a-user-session-id")

with_mock UserSessions, [notify: fn(_message, [to: _user_session_id]) -> :ok end] do
expected_message = %{
Expand All @@ -17,7 +17,7 @@ defmodule ExChat.ChatRoomTest do
message: "a message"
}

ChatRoom.send(chatroom, "a message", as: "another-user-session-id")
Room.send(room, "a message", as: "another-user-session-id")

assert called UserSessions.notify(expected_message, to: "a-user-session-id")
end
Expand Down
26 changes: 0 additions & 26 deletions test/ex_chat/use_cases/create_chat_room_test.exs

This file was deleted.

26 changes: 26 additions & 0 deletions test/ex_chat/use_cases/create_room_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule ExChat.UseCases.CreateRoomTest do
use ExUnit.Case, async: true

import Mock

alias ExChat.Rooms
alias ExChat.UseCases.CreateRoom

test "return an error message when the room already exists" do
with_mock(Rooms, create: fn(_) -> {:error, :already_exists} end) do
result = CreateRoom.on("a room")

assert result == {:error, "a room already exists"}
assert called Rooms.create("a room")
end
end

test "return an successful message when create a room" do
with_mock(Rooms, create: fn(_) -> :ok end) do
result = CreateRoom.on("a room")

assert result == {:ok, "a room has been created!"}
assert called Rooms.create("a room")
end
end
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
defmodule ExChat.UseCases.JoinChatRoomTest do
defmodule ExChat.UseCases.JoinRoomTest do
use ExUnit.Case, async: true

import Mock

alias ExChat.{ChatRooms, UserSessions}
alias ExChat.UseCases.JoinChatRoom
alias ExChat.{Rooms, UserSessions}
alias ExChat.UseCases.JoinRoom

test "return an error message when the chat room does not exists" do
with_mocks([
{ChatRooms, [], join: fn(_, _) -> {:error, :unexisting_room} end},
{Rooms, [], join: fn(_, _) -> {:error, :unexisting_room} end},
{UserSessions, [], notify: fn(_, _) -> nil end}
]) do
result = JoinChatRoom.on("a room", "a user id")
result = JoinRoom.on("a room", "a user id")

assert result == {:error, "a room does not exists"}
refute called UserSessions.notify(%{room: "a room", message: "welcome to the a room chat room, a user id!"}, to: "a user id")
Expand All @@ -20,10 +20,10 @@ defmodule ExChat.UseCases.JoinChatRoomTest do

test "return an error message when already joined the chat room" do
with_mocks([
{ChatRooms, [], join: fn(_, _) -> {:error, :already_joined} end},
{Rooms, [], join: fn(_, _) -> {:error, :already_joined} end},
{UserSessions, [], notify: fn(_, _) -> nil end}
]) do
result = JoinChatRoom.on("a room", "a user id")
result = JoinRoom.on("a room", "a user id")

assert result == {:error, "you already joined the a room room!"}
refute called UserSessions.notify(%{room: "a room", message: "welcome to the a room chat room, a user id!"}, to: "a user id")
Expand All @@ -32,10 +32,10 @@ defmodule ExChat.UseCases.JoinChatRoomTest do

test "notifies user sessions when joining a chat room" do
with_mocks([
{ChatRooms, [], join: fn(_, _) -> :ok end},
{Rooms, [], join: fn(_, _) -> :ok end},
{UserSessions, [], notify: fn(_, _) -> nil end}
]) do
result = JoinChatRoom.on("a room", "a user id")
result = JoinRoom.on("a room", "a user id")

assert result == :ok
assert called UserSessions.notify(%{room: "a room", message: "welcome to the a room chat room, a user id!"}, to: "a user id")
Expand Down
26 changes: 0 additions & 26 deletions test/ex_chat/use_cases/send_message_to_chat_room_test.exs

This file was deleted.

26 changes: 26 additions & 0 deletions test/ex_chat/use_cases/send_message_to_room_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule ExChat.UseCases.SendMessageToRoomTest do
use ExUnit.Case, async: true

import Mock

alias ExChat.Rooms
alias ExChat.UseCases.SendMessageToRoom

test "return an error message when the room does not exists" do
with_mock(Rooms, send: fn(_, _) -> {:error, :unexisting_room} end) do
result = SendMessageToRoom.on("a message", "a room", "a user id")

assert result == {:error, "a room does not exists"}
assert called Rooms.send("a message", to: "a room", as: "a user id")
end
end

test "return ok when send a message" do
with_mock(Rooms, send: fn(_, _) -> :ok end) do
result = SendMessageToRoom.on("a message", "a room", "a user id")

assert result == :ok
assert called Rooms.send("a message", to: "a room", as: "a user id")
end
end
end
Loading