@@ -5,19 +5,24 @@ defmodule ChatApiWeb.NotificationChannel do
5
5
alias ChatApi . { Messages , Conversations }
6
6
7
7
@ impl true
8
- def join ( "notification:" <> account_id , % { "ids" => ids } , socket ) do
9
- if authorized? ( socket , account_id ) do
10
- topics = for conversation_id <- ids , do: "conversation:#{ conversation_id } "
11
-
12
- { :ok ,
13
- socket
14
- |> assign ( :topics , [ ] )
15
- |> put_new_topics ( topics ) }
8
+ def join ( "notification:lobby" , payload , socket ) do
9
+ if authorized? ( payload ) do
10
+ { :ok , socket }
16
11
else
17
- { :error , % { reason: "Unauthorized " } }
12
+ { :error , % { reason: "unauthorized " } }
18
13
end
19
14
end
20
15
16
+ # TODO: secure this better!
17
+ def join ( "notification:" <> _account_id , % { "ids" => ids } , socket ) do
18
+ topics = for conversation_id <- ids , do: "conversation:#{ conversation_id } "
19
+
20
+ { :ok ,
21
+ socket
22
+ |> assign ( :topics , [ ] )
23
+ |> put_new_topics ( topics ) }
24
+ end
25
+
21
26
# Channels can be used in a request/response fashion
22
27
# by sending replies to requests from the client
23
28
@ impl true
@@ -46,22 +51,22 @@ defmodule ChatApiWeb.NotificationChannel do
46
51
end
47
52
48
53
def handle_in ( "shout" , payload , socket ) do
49
- with % { current_user: current_user } <- socket . assigns ,
50
- % { id: user_id , account_id: account_id } <- current_user do
51
- msg = Map . merge ( payload , % { "user_id" => user_id , "account_id" => account_id } )
52
- { :ok , message } = Messages . create_message ( msg )
53
- message = Messages . get_message! ( message . id )
54
- result = ChatApiWeb.MessageView . render ( "expanded.json" , message: message )
54
+ { :ok , message } = Messages . create_message ( payload )
55
+ message = Messages . get_message! ( message . id )
56
+ result = ChatApiWeb.MessageView . render ( "expanded.json" , message: message )
57
+ # TODO: write doc explaining difference between push, broadcast, etc.
58
+ push ( socket , "shout" , result )
55
59
56
- # TODO: write doc explaining difference between push, broadcast, etc.
57
- push ( socket , "shout" , result )
60
+ case result do
61
+ % { conversation_id: conversation_id } ->
62
+ topic = "conversation:" <> conversation_id
58
63
59
- % { conversation_id: conversation_id } = result
60
- topic = "conversation:" <> conversation_id
64
+ ChatApiWeb.Endpoint . broadcast_from! ( self ( ) , topic , "shout" , result )
61
65
62
- ChatApiWeb.Endpoint . broadcast_from! ( self ( ) , topic , "shout" , result )
66
+ ChatApi.Slack . send_conversation_message_alert ( conversation_id , message . body , type: "agent" )
63
67
64
- ChatApi.Slack . send_conversation_message_alert ( conversation_id , message . body , type: "agent" )
68
+ _ ->
69
+ nil
65
70
end
66
71
67
72
{ :noreply , socket }
@@ -93,12 +98,8 @@ defmodule ChatApiWeb.NotificationChannel do
93
98
end )
94
99
end
95
100
96
- defp authorized? ( socket , account_id ) do
97
- with % { current_user: current_user } <- socket . assigns ,
98
- % { account_id: acct } <- current_user do
99
- acct == account_id
100
- else
101
- _ -> false
102
- end
101
+ # Add authorization logic here as required.
102
+ defp authorized? ( _payload ) do
103
+ true
103
104
end
104
105
end
0 commit comments