@@ -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
146162end
0 commit comments