Skip to content

Commit

Permalink
Fixes #156 - Deprecate "shout" event
Browse files Browse the repository at this point in the history
Deprecate "shout" event for conversation channel
in favor of "message:created".
  • Loading branch information
xprazak2 committed Dec 22, 2020
1 parent 4564f87 commit 95e27c5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/chat_api_web/channels/conversation_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule ChatApiWeb.ConversationChannel do

alias ChatApiWeb.Presence
alias ChatApi.{Messages, Conversations}
require Logger

@impl true
def join("conversation:lobby", payload, socket) do
Expand Down Expand Up @@ -80,20 +81,32 @@ defmodule ChatApiWeb.ConversationChannel do
{:reply, {:ok, payload}, socket}
end

def handle_in("shout", payload, socket) do
Logger.notice(
"'shout' is deprecated as event name on a new message and will be removed in a future version. Please migrate to a newer version of a client."
)

handle_in_msg("shout", payload, socket)
end

def handle_in("message:created", payload, socket) do
handle_in_msg("message:created", payload, socket)
end

# It is also common to receive messages from the client and
# broadcast to everyone in the current topic (conversation:lobby).
def handle_in("shout", payload, socket) do
defp handle_in_msg(event_name, payload, socket) do
with %{conversation: conversation} <- socket.assigns,
%{id: conversation_id, account_id: account_id} <- conversation,
{:ok, message} <-
payload
|> Map.merge(%{"conversation_id" => conversation_id, "account_id" => account_id})
|> Messages.create_message(),
message <- Messages.get_message!(message.id) do
broadcast_new_message(socket, message)
broadcast_new_message(socket, event_name, message)
else
_ ->
broadcast(socket, "shout", payload)
broadcast(socket, event_name, payload)
end

{:noreply, socket}
Expand Down Expand Up @@ -123,9 +136,9 @@ defmodule ChatApiWeb.ConversationChannel do
})
end

defp broadcast_new_message(socket, message) do
defp broadcast_new_message(socket, event_name, message) do
broadcast_conversation_update(message)
broadcast(socket, "shout", Messages.Helpers.format(message))
broadcast(socket, event_name, Messages.Helpers.format(message))

message
|> Messages.Notification.notify(:slack)
Expand Down
6 changes: 6 additions & 0 deletions test/chat_api_web/channels/conversation_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ defmodule ChatApiWeb.ConversationChannelTest do
assert_broadcast "shout", _msg
end

test "message:created broadcasts to conversation:lobby", %{socket: socket, account: account} do
msg = %{body: "Hello world!", account_id: account.id}
push(socket, "message:created", msg)
assert_broadcast "message:created", _msg
end

test "broadcasts are pushed to the client", %{socket: socket} do
broadcast_from!(socket, "broadcast", %{"some" => "data"})
assert_push "broadcast", %{"some" => "data"}
Expand Down

0 comments on commit 95e27c5

Please sign in to comment.