@@ -5,8 +5,8 @@ defmodule RealtimeWeb.RealtimeChannel do
5
5
use RealtimeWeb , :channel
6
6
use RealtimeWeb.RealtimeChannel.Logging
7
7
8
- alias RealtimeWeb.SocketDisconnect
9
8
alias DBConnection.Backoff
9
+ alias Phoenix.Socket
10
10
11
11
alias Realtime.Crypto
12
12
alias Realtime.GenCounter
@@ -22,11 +22,14 @@ defmodule RealtimeWeb.RealtimeChannel do
22
22
alias Realtime.Tenants.Connect
23
23
24
24
alias RealtimeWeb.Channels.Payloads.Join
25
+ alias RealtimeWeb.Channels.Payloads.Config
26
+ alias RealtimeWeb.Channels.Payloads.PostgresChange
25
27
alias RealtimeWeb.ChannelsAuthorization
26
28
alias RealtimeWeb.RealtimeChannel.BroadcastHandler
27
29
alias RealtimeWeb.RealtimeChannel.MessageDispatcher
28
30
alias RealtimeWeb.RealtimeChannel.PresenceHandler
29
31
alias RealtimeWeb.RealtimeChannel.Tracker
32
+ alias RealtimeWeb.SocketDisconnect
30
33
31
34
@ confirm_token_ms_interval :timer . minutes ( 5 )
32
35
@@ -47,20 +50,11 @@ defmodule RealtimeWeb.RealtimeChannel do
47
50
Logger . metadata ( external_id: tenant_id , project: tenant_id )
48
51
Logger . put_process_level ( self ( ) , log_level )
49
52
50
- socket =
51
- socket
52
- |> assign_access_token ( params )
53
- |> assign_counter ( )
54
- |> assign_presence_counter ( )
55
- |> assign ( :private? , ! ! params [ "config" ] [ "private" ] )
56
- |> assign ( :policies , nil )
57
-
58
- case Join . validate ( params ) do
59
- { :ok , _join } -> nil
60
- { :error , :invalid_join_payload , errors } -> log_error ( socket , "InvalidJoinPayload" , errors )
61
- end
53
+ # We always need to assign the access token so we can get the logs metadata working as expected
54
+ socket = assign_access_token ( socket , params )
62
55
63
- with :ok <- SignalHandler . shutdown_in_progress? ( ) ,
56
+ with { :ok , % Socket { } = socket , % Join { } = configuration } <- configure_socket ( socket , params ) ,
57
+ :ok <- SignalHandler . shutdown_in_progress? ( ) ,
64
58
:ok <- only_private? ( tenant_id , socket ) ,
65
59
:ok <- limit_joins ( socket ) ,
66
60
:ok <- limit_channels ( socket ) ,
@@ -70,7 +64,6 @@ defmodule RealtimeWeb.RealtimeChannel do
70
64
{ :ok , db_conn } <- Connect . lookup_or_start_connection ( tenant_id ) ,
71
65
{ :ok , socket } <- maybe_assign_policies ( sub_topic , db_conn , socket ) do
72
66
tenant_topic = Tenants . tenant_topic ( tenant_id , sub_topic , ! socket . assigns . private? )
73
-
74
67
# fastlane subscription
75
68
metadata =
76
69
MessageDispatcher . fastlane_metadata ( transport_pid , serializer , topic , socket . assigns . log_level , tenant_id )
@@ -79,15 +72,11 @@ defmodule RealtimeWeb.RealtimeChannel do
79
72
80
73
Phoenix.PubSub . subscribe ( Realtime.PubSub , "realtime:operations:" <> tenant_id )
81
74
82
- is_new_api = new_api? ( params )
83
- # TODO: Default will be moved to false in the future
84
- presence_enabled? =
85
- case get_in ( params , [ "config" , "presence" , "enabled" ] ) do
86
- enabled when is_boolean ( enabled ) -> enabled
87
- _ -> true
88
- end
75
+ is_new_api = new_api? ( configuration )
89
76
90
- pg_change_params = pg_change_params ( is_new_api , params , channel_pid , claims , sub_topic )
77
+ presence_enabled? = Join . presence_enabled? ( configuration )
78
+
79
+ pg_change_params = pg_change_params ( is_new_api , configuration , channel_pid , claims , sub_topic )
91
80
92
81
opts = % {
93
82
is_new_api: is_new_api ,
@@ -104,13 +93,13 @@ defmodule RealtimeWeb.RealtimeChannel do
104
93
state = % { postgres_changes: add_id_to_postgres_changes ( pg_change_params ) }
105
94
106
95
assigns = % {
107
- ack_broadcast: ! ! params [ "config" ] [ "broadcast" ] [ "ack" ] ,
96
+ ack_broadcast: Join . ack_broadcast? ( configuration ) ,
108
97
confirm_token_ref: confirm_token_ref ,
109
98
is_new_api: is_new_api ,
110
99
pg_sub_ref: nil ,
111
100
pg_change_params: pg_change_params ,
112
- presence_key: presence_key ( params ) ,
113
- self_broadcast: ! ! params [ "config" ] [ "broadcast" ] [ "self" ] ,
101
+ presence_key: Join . presence_key ( configuration ) ,
102
+ self_broadcast: Join . self_broadcast? ( configuration ) ,
114
103
tenant_topic: tenant_topic ,
115
104
channel_name: sub_topic ,
116
105
presence_enabled?: presence_enabled?
@@ -124,6 +113,9 @@ defmodule RealtimeWeb.RealtimeChannel do
124
113
125
114
{ :ok , state , assign ( socket , assigns ) }
126
115
else
116
+ { :error , :invalid_join_payload , errors , socket } ->
117
+ log_error ( socket , "InvalidJoinPayload" , errors )
118
+
127
119
{ :error , :expired_token , msg } ->
128
120
maybe_log_warning ( socket , "InvalidJWTToken" , msg )
129
121
@@ -200,6 +192,23 @@ defmodule RealtimeWeb.RealtimeChannel do
200
192
end
201
193
end
202
194
195
+ defp configure_socket ( socket , params ) do
196
+ case Join . validate ( params ) do
197
+ { :ok , configuration } ->
198
+ socket =
199
+ socket
200
+ |> assign_counter ( )
201
+ |> assign_presence_counter ( )
202
+ |> assign ( :private? , Join . private? ( configuration ) )
203
+ |> assign ( :policies , nil )
204
+
205
+ { :ok , socket , configuration }
206
+
207
+ { :error , :invalid_join_payload , errors } ->
208
+ { :error , :invalid_join_payload , errors , socket }
209
+ end
210
+ end
211
+
203
212
@ impl true
204
213
def handle_info ( :update_rate_counter , % { assigns: % { limits: % { max_events_per_second: max } } } = socket ) do
205
214
count ( socket )
@@ -537,40 +546,24 @@ defmodule RealtimeWeb.RealtimeChannel do
537
546
538
547
defp count ( % { assigns: % { rate_counter: counter } } ) , do: GenCounter . add ( counter . id )
539
548
540
- defp presence_key ( params ) do
541
- case params [ "config" ] [ "presence" ] [ "key" ] do
542
- key when is_binary ( key ) and key != "" -> key
543
- _ -> UUID . uuid1 ( )
544
- end
545
- end
546
-
547
- defp assign_access_token ( % { assigns: % { headers: headers } } = socket , params ) do
548
- access_token = Map . get ( params , "access_token" ) || Map . get ( params , "user_token" )
549
+ defp assign_access_token ( socket , params ) do
550
+ % { assigns: % { tenant_token: tenant_token , headers: headers } } = socket
549
551
{ _ , header } = Enum . find ( headers , { nil , nil } , fn { k , _ } -> k == "x-api-key" end )
550
552
551
- case access_token do
552
- nil -> assign ( socket , :access_token , header )
553
- "sb_" <> _ -> assign ( socket , :access_token , header )
554
- _ -> handle_access_token ( socket , params )
555
- end
556
- end
557
-
558
- defp assign_access_token ( socket , params ) , do: handle_access_token ( socket , params )
553
+ access_token = Map . get ( params , "access_token" )
554
+ user_token = Map . get ( params , "user_token" )
559
555
560
- defp handle_access_token ( % { assigns: % { tenant_token: _tenant_token } } = socket , % { "user_token" => user_token } )
561
- when is_binary ( user_token ) do
562
- assign ( socket , :access_token , user_token )
563
- end
556
+ access_token =
557
+ cond do
558
+ is_binary ( access_token ) and ! String . starts_with? ( access_token , "sb_" ) -> access_token
559
+ is_binary ( user_token ) and ! String . starts_with? ( user_token , "sb_" ) -> user_token
560
+ is_binary ( tenant_token ) and ! String . starts_with? ( tenant_token , "sb_" ) -> tenant_token
561
+ true -> header
562
+ end
564
563
565
- defp handle_access_token ( % { assigns: % { tenant_token: _tenant_token } } = socket , % { "access_token" => access_token } )
566
- when is_binary ( access_token ) do
567
564
assign ( socket , :access_token , access_token )
568
565
end
569
566
570
- defp handle_access_token ( % { assigns: % { tenant_token: tenant_token } } = socket , _params ) when is_binary ( tenant_token ) do
571
- assign ( socket , :access_token , tenant_token )
572
- end
573
-
574
567
defp confirm_token ( % { assigns: assigns } ) do
575
568
% { jwt_secret: jwt_secret , access_token: access_token } = assigns
576
569
@@ -637,28 +630,30 @@ defmodule RealtimeWeb.RealtimeChannel do
637
630
} )
638
631
end
639
632
640
- defp new_api? ( % { " config" => _ } ) , do: true
633
+ defp new_api? ( % Join { config: config } ) when not is_nil ( config ) , do: true
641
634
defp new_api? ( _ ) , do: false
642
635
643
- defp pg_change_params ( true , params , channel_pid , claims , _ ) do
644
- case get_in ( params , [ "config" , " postgres_changes" ] ) do
645
- [ _ | _ ] = params_list ->
646
- params_list
647
- |> Enum . reject ( & is_nil / 1 )
648
- |> Enum . map ( fn params ->
649
- % {
650
- id: UUID . uuid1 ( ) ,
651
- channel_pid: channel_pid ,
652
- claims: claims ,
653
- params: params
654
- }
655
- end )
656
-
657
- _ ->
658
- [ ]
659
- end
636
+ defp pg_change_params ( true , % Join { config: % Config { postgres_changes: postgres_changes } } , channel_pid , claims , _ )
637
+ when not is_nil ( postgres_changes ) do
638
+ postgres_changes
639
+ |> Enum . reject ( & is_nil / 1 )
640
+ |> Enum . map ( fn % PostgresChange { table: table , event: event , schema: schema , filter: filter } ->
641
+ params =
642
+ % { "table" => table , "filter" => filter , "schema" => schema , "event" => event }
643
+ |> Enum . reject ( fn { _ , v } -> is_nil ( v ) end )
644
+ |> Map . new ( )
645
+
646
+ % {
647
+ id: UUID . uuid1 ( ) ,
648
+ channel_pid: channel_pid ,
649
+ claims: claims ,
650
+ params: params
651
+ }
652
+ end )
660
653
end
661
654
655
+ defp pg_change_params ( true , _ , _ , _ , _ ) , do: [ ]
656
+
662
657
defp pg_change_params ( false , _ , channel_pid , claims , sub_topic ) do
663
658
params =
664
659
case String . split ( sub_topic , ":" , parts: 3 ) do
0 commit comments