Skip to content

Commit a45dda8

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

File tree

5 files changed

+84
-34
lines changed

5 files changed

+84
-34
lines changed

lib/chat_api/messages/notification.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ defmodule ChatApi.Messages.Notification do
1616
})
1717
end
1818

19-
@spec broadcast_to_conversation!(Message.t()) :: Message.t()
20-
def broadcast_to_conversation!(%Message{} = message) do
19+
@spec broadcast_to_conversation!(Message.t(), String.t()) :: Message.t()
20+
def broadcast_to_conversation!(%Message{} = message, event_name \\ "shout") do
2121
message
2222
|> Helpers.get_conversation_topic()
23-
|> ChatApiWeb.Endpoint.broadcast!("shout", Helpers.format(message))
23+
|> ChatApiWeb.Endpoint.broadcast!(event_name, Helpers.format(message))
2424

2525
message
2626
end

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: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,15 @@ 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()
60-
61-
message
62-
|> Map.get(:id)
63-
|> Messages.get_message!()
64-
|> broadcast_new_message()
65-
|> Messages.Helpers.handle_post_creation_conversation_updates()
66-
end
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+
)
57+
handle_incoming_message("shout", payload, socket)
58+
end
6759

68-
{:reply, :ok, socket}
60+
@impl true
61+
def handle_in("message:created", payload, socket) do
62+
handle_incoming_message("message:created", payload, socket)
6963
end
7064

7165
@impl true
@@ -105,9 +99,9 @@ defmodule ChatApiWeb.NotificationChannel do
10599
{:noreply, socket}
106100
end
107101

108-
defp broadcast_new_message(message) do
102+
defp broadcast_new_message(message, event_name) do
109103
message
110-
|> Messages.Notification.broadcast_to_conversation!()
104+
|> Messages.Notification.broadcast_to_conversation!(event_name)
111105
|> Messages.Notification.notify(:slack)
112106
|> Messages.Notification.notify(:slack_support_channel)
113107
|> Messages.Notification.notify(:slack_company_channel)
@@ -137,4 +131,22 @@ defmodule ChatApiWeb.NotificationChannel do
137131
_ -> false
138132
end
139133
end
134+
135+
defp handle_incoming_message(event_name, payload, socket) do
136+
with %{current_user: current_user} <- socket.assigns,
137+
%{id: user_id, account_id: account_id} <- current_user do
138+
{:ok, message} =
139+
payload
140+
|> Map.merge(%{"user_id" => user_id, "account_id" => account_id})
141+
|> Messages.create_message()
142+
143+
message
144+
|> Map.get(:id)
145+
|> Messages.get_message!()
146+
|> broadcast_new_message(event_name)
147+
|> Messages.Helpers.handle_post_creation_conversation_updates()
148+
end
149+
150+
{:reply, :ok, socket}
151+
end
140152
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
@@ -40,6 +40,22 @@ defmodule ChatApiWeb.NotificationChannelTest do
4040
assert_push("shout", _msg)
4141
end
4242

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

0 commit comments

Comments
 (0)