Skip to content

Commit cca3fa0

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

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/chat_api_web/channels/conversation_channel.ex

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

84+
def handle_in("shout", payload, socket) do
85+
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.")
86+
handle_in_msg "shout", payload, socket
87+
end
88+
89+
def handle_in("message:created", payload, socket) do
90+
handle_in_msg "message:created", payload, socket
91+
end
92+
8393
# It is also common to receive messages from the client and
8494
# broadcast to everyone in the current topic (conversation:lobby).
85-
def handle_in("shout", payload, socket) do
95+
defp handle_in_msg(event_name, payload, socket) do
8696
with %{conversation: conversation} <- socket.assigns,
8797
%{id: conversation_id, account_id: account_id} <- conversation,
8898
{:ok, message} <-
8999
payload
90100
|> Map.merge(%{"conversation_id" => conversation_id, "account_id" => account_id})
91101
|> Messages.create_message(),
92102
message <- Messages.get_message!(message.id) do
93-
broadcast_new_message(socket, message)
103+
broadcast_new_message(socket, event_name, message)
94104
else
95105
_ ->
96-
broadcast(socket, "shout", payload)
106+
broadcast(socket, event_name, payload)
97107
end
98108

99109
{:noreply, socket}
@@ -123,9 +133,9 @@ defmodule ChatApiWeb.ConversationChannel do
123133
})
124134
end
125135

126-
defp broadcast_new_message(socket, message) do
136+
defp broadcast_new_message(socket, event_name, message) do
127137
broadcast_conversation_update(message)
128-
broadcast(socket, "shout", Messages.Helpers.format(message))
138+
broadcast(socket, event_name, Messages.Helpers.format(message))
129139

130140
message
131141
|> 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)