Skip to content

Commit 2bfd0f6

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

File tree

4 files changed

+79
-28
lines changed

4 files changed

+79
-28
lines changed

lib/chat_api_web/channels/conversation_channel.ex

Lines changed: 31 additions & 15 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
@@ -79,20 +80,16 @@ defmodule ChatApiWeb.ConversationChannel do
7980
end
8081

8182
def handle_in("shout", payload, socket) do
82-
with %{conversation: conversation} <- socket.assigns,
83-
%{id: conversation_id, account_id: account_id} <- conversation,
84-
{:ok, message} <-
85-
payload
86-
|> Map.merge(%{"conversation_id" => conversation_id, "account_id" => account_id})
87-
|> Messages.create_message(),
88-
message <- Messages.get_message!(message.id) do
89-
broadcast_new_message(socket, message)
90-
else
91-
_ ->
92-
broadcast(socket, "shout", payload)
93-
end
83+
Logger.warn(
84+
"'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."
85+
)
9486

95-
{:noreply, socket}
87+
handle_incoming_message("shout", payload, socket)
88+
end
89+
90+
@impl true
91+
def handle_in("message:created", payload, socket) do
92+
handle_incoming_message("message:created", payload, socket)
9693
end
9794

9895
def handle_in("messages:seen", _payload, socket) do
@@ -119,9 +116,9 @@ defmodule ChatApiWeb.ConversationChannel do
119116
})
120117
end
121118

122-
defp broadcast_new_message(socket, message) do
119+
defp broadcast_new_message(socket, event_name, message) do
123120
broadcast_conversation_update(message)
124-
broadcast(socket, "shout", Messages.Helpers.format(message))
121+
broadcast(socket, event_name, Messages.Helpers.format(message))
125122

126123
message
127124
|> Messages.Notification.notify(:slack)
@@ -143,4 +140,23 @@ defmodule ChatApiWeb.ConversationChannel do
143140
_ -> false
144141
end
145142
end
143+
144+
# It is also common to receive messages from the client and
145+
# broadcast to everyone in the current topic (conversation:lobby).
146+
defp handle_incoming_message(event_name, payload, socket) do
147+
with %{conversation: conversation} <- socket.assigns,
148+
%{id: conversation_id, account_id: account_id} <- conversation,
149+
{:ok, message} <-
150+
payload
151+
|> Map.merge(%{"conversation_id" => conversation_id, "account_id" => account_id})
152+
|> Messages.create_message(),
153+
message <- Messages.get_message!(message.id) do
154+
broadcast_new_message(socket, event_name, message)
155+
else
156+
_ ->
157+
broadcast(socket, event_name, payload)
158+
end
159+
160+
{:noreply, socket}
161+
end
146162
end

lib/chat_api_web/channels/notification_channel.ex

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,16 @@ defmodule ChatApiWeb.NotificationChannel do
5151
end
5252

5353
def handle_in("shout", payload, socket) do
54-
with %{current_user: current_user} <- socket.assigns,
55-
%{id: user_id, account_id: account_id} <- current_user do
56-
{:ok, message} =
57-
payload
58-
|> Map.merge(%{"user_id" => user_id, "account_id" => account_id})
59-
|> Messages.create_message()
54+
Logger.warn(
55+
"'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."
56+
)
6057

61-
message
62-
|> Map.get(:id)
63-
|> Messages.get_message!()
64-
|> broadcast_new_message()
65-
|> maybe_update_conversation_assignee()
66-
end
58+
handle_incoming_message(payload, socket)
59+
end
6760

68-
{:noreply, socket}
61+
@impl true
62+
def handle_in("message:created", payload, socket) do
63+
handle_incoming_message(payload, socket)
6964
end
7065

7166
@impl true
@@ -168,4 +163,22 @@ defmodule ChatApiWeb.NotificationChannel do
168163
_ -> false
169164
end
170165
end
166+
167+
defp handle_incoming_message(payload, socket) do
168+
with %{current_user: current_user} <- socket.assigns,
169+
%{id: user_id, account_id: account_id} <- current_user do
170+
{:ok, message} =
171+
payload
172+
|> Map.merge(%{"user_id" => user_id, "account_id" => account_id})
173+
|> Messages.create_message()
174+
175+
message
176+
|> Map.get(:id)
177+
|> Messages.get_message!()
178+
|> broadcast_new_message()
179+
|> maybe_update_conversation_assignee()
180+
end
181+
182+
{:noreply, socket}
183+
end
171184
end

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"}

test/chat_api_web/channels/notification_channel_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ defmodule ChatApiWeb.NotificationChannelTest do
3838
assert_push("shout", _msg)
3939
end
4040

41+
test "message:created broadcasts to notification:lobby", %{
42+
socket: socket,
43+
account: account,
44+
conversation: conversation
45+
} do
46+
msg = %{
47+
body: "Hello world!",
48+
account_id: account.id,
49+
conversation_id: conversation.id
50+
}
51+
52+
push(socket, "message:created", msg)
53+
54+
assert_push("message:created", _msg)
55+
end
56+
4157
test "broadcasts are pushed to the client", %{socket: socket} do
4258
broadcast_from!(socket, "broadcast", %{"some" => "data"})
4359
assert_push "broadcast", %{"some" => "data"}

0 commit comments

Comments
 (0)