Skip to content

Commit c37afad

Browse files
committed
Fixes #156 - Deprecate "shout" event
Deprecate "shout" event for conversation channel in favor of "message:created".
1 parent 4564f87 commit c37afad

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/chat_api_web/channels/conversation_channel.ex

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule ChatApiWeb.ConversationChannel do
33

44
alias ChatApiWeb.Presence
55
alias ChatApi.{Messages, Conversations}
6+
require Logger
67

78
@impl true
89
def join("conversation:lobby", payload, socket) do
@@ -80,20 +81,33 @@ defmodule ChatApiWeb.ConversationChannel do
8081
{:reply, {:ok, payload}, socket}
8182
end
8283

84+
@impl true
85+
def handle_in("shout", payload, socket) do
86+
Logger.warn(
87+
"'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."
88+
)
89+
90+
handle_in_msg("shout", payload, socket)
91+
end
92+
93+
def handle_in("message:created", payload, socket) do
94+
handle_in_msg("message:created", payload, socket)
95+
end
96+
8397
# It is also common to receive messages from the client and
8498
# broadcast to everyone in the current topic (conversation:lobby).
85-
def handle_in("shout", payload, socket) do
99+
defp handle_in_msg(event_name, payload, socket) do
86100
with %{conversation: conversation} <- socket.assigns,
87101
%{id: conversation_id, account_id: account_id} <- conversation,
88102
{:ok, message} <-
89103
payload
90104
|> Map.merge(%{"conversation_id" => conversation_id, "account_id" => account_id})
91105
|> Messages.create_message(),
92106
message <- Messages.get_message!(message.id) do
93-
broadcast_new_message(socket, message)
107+
broadcast_new_message(socket, event_name, message)
94108
else
95109
_ ->
96-
broadcast(socket, "shout", payload)
110+
broadcast(socket, event_name, payload)
97111
end
98112

99113
{:noreply, socket}
@@ -123,9 +137,9 @@ defmodule ChatApiWeb.ConversationChannel do
123137
})
124138
end
125139

126-
defp broadcast_new_message(socket, message) do
140+
defp broadcast_new_message(socket, event_name, message) do
127141
broadcast_conversation_update(message)
128-
broadcast(socket, "shout", Messages.Helpers.format(message))
142+
broadcast(socket, event_name, Messages.Helpers.format(message))
129143

130144
message
131145
|> Messages.Notification.notify(:slack)

test/chat_api_web/channels/conversation_channel_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ defmodule ChatApiWeb.ConversationChannelTest do
2525
assert_broadcast "shout", _msg
2626
end
2727

28+
test "message:created broadcasts to conversation:lobby", %{socket: socket, account: account} do
29+
msg = %{body: "Hello world!", account_id: account.id}
30+
push(socket, "message:created", msg)
31+
assert_broadcast "message:created", _msg
32+
end
33+
2834
test "broadcasts are pushed to the client", %{socket: socket} do
2935
broadcast_from!(socket, "broadcast", %{"some" => "data"})
3036
assert_push "broadcast", %{"some" => "data"}

0 commit comments

Comments
 (0)