Skip to content

Commit 95e27c5

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

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/chat_api_web/channels/conversation_channel.ex

Lines changed: 18 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,32 @@ defmodule ChatApiWeb.ConversationChannel do
8081
{:reply, {:ok, payload}, socket}
8182
end
8283

84+
def handle_in("shout", payload, socket) do
85+
Logger.notice(
86+
"'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."
87+
)
88+
89+
handle_in_msg("shout", payload, socket)
90+
end
91+
92+
def handle_in("message:created", payload, socket) do
93+
handle_in_msg("message:created", payload, socket)
94+
end
95+
8396
# It is also common to receive messages from the client and
8497
# broadcast to everyone in the current topic (conversation:lobby).
85-
def handle_in("shout", payload, socket) do
98+
defp handle_in_msg(event_name, payload, socket) do
8699
with %{conversation: conversation} <- socket.assigns,
87100
%{id: conversation_id, account_id: account_id} <- conversation,
88101
{:ok, message} <-
89102
payload
90103
|> Map.merge(%{"conversation_id" => conversation_id, "account_id" => account_id})
91104
|> Messages.create_message(),
92105
message <- Messages.get_message!(message.id) do
93-
broadcast_new_message(socket, message)
106+
broadcast_new_message(socket, event_name, message)
94107
else
95108
_ ->
96-
broadcast(socket, "shout", payload)
109+
broadcast(socket, event_name, payload)
97110
end
98111

99112
{:noreply, socket}
@@ -123,9 +136,9 @@ defmodule ChatApiWeb.ConversationChannel do
123136
})
124137
end
125138

126-
defp broadcast_new_message(socket, message) do
139+
defp broadcast_new_message(socket, event_name, message) do
127140
broadcast_conversation_update(message)
128-
broadcast(socket, "shout", Messages.Helpers.format(message))
141+
broadcast(socket, event_name, Messages.Helpers.format(message))
129142

130143
message
131144
|> 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)