From f85cdd5e3c639cf290ae5722528a8113c07f03d6 Mon Sep 17 00:00:00 2001 From: sennui Date: Tue, 10 Mar 2020 17:30:28 +0100 Subject: [PATCH 1/3] cleanup, whitespace --- apps/roster/src/protocol/roster_message.erl | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/apps/roster/src/protocol/roster_message.erl b/apps/roster/src/protocol/roster_message.erl index 4c341efca..afdbe65b5 100644 --- a/apps/roster/src/protocol/roster_message.erl +++ b/apps/roster/src/protocol/roster_message.erl @@ -47,7 +47,7 @@ info(#'Message'{status = [], id = [], feed_id = F, from=From0, to = To, <<"sys">> -> From0; <<"emqttd">> -> roster:phone_id(ClientId) end, - Created=roster:now_msec(), + FId = roster:roster_id(From), FeedData = {R, UID} = case Feed = roster:feed_key(F) of @@ -268,9 +268,12 @@ info(#'Message'{id = Id, msg_id = ClMID, feed_id = Feed, from = From0, seenby = _ -> skip end; _ -> skip end, n2o_vnode:send(C, Topic, term_to_binary(Internal)), %% NOTE! send push about deleted msg only if acted user is in the seenby - case Seen of [-1] -> - n2o_async:pid(system, ?MODULE) ! {send_push, From1, To1, Internal, ?MSG_DELETE_ACTION}; _ -> - skip end, + case Seen of + [-1] -> + n2o_async:pid(system, ?MODULE) ! {send_push, From1, To1, Internal, ?MSG_DELETE_ACTION}; + _ -> + skip + end, case {Type, Link} of {[reply], Link} when is_integer(Link) andalso NewSeen == [-1] -> case kvs:get('Message', Link) of @@ -431,7 +434,13 @@ proc({send_push, From, To, #'Message'{type = TypeList} = Msg, Action}, #handler{ %% Check should server proceed push %% TODO add push for message/delete case From of - To -> case lists:member(cursor, TypeList) of true -> notify(From, To, Msg, Action); _ -> roster:info(?MODULE, "ExcessivePush:~p", [From]) end; + To -> + case lists:member(cursor, TypeList) of + true -> + notify(From, To, Msg, Action); + _ -> + roster:info(?MODULE, "ExcessivePush:~p", [From]) + end; _ -> notify(From, To, Msg, Action) end, {reply, [], H}; -- GitLab From 39f5681b101a650f85430ef1eb87aab8703a87dd Mon Sep 17 00:00:00 2001 From: sennui Date: Tue, 10 Mar 2020 17:30:47 +0100 Subject: [PATCH 2/3] handle connection drop and subscription tables --- apps/roster/src/protocol/micro_auth.erl | 7 +++++++ apps/roster/src/protocol/roster_presence.erl | 13 ++++++++++-- apps/roster/src/roster.erl | 21 +++++++++++++------- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/apps/roster/src/protocol/micro_auth.erl b/apps/roster/src/protocol/micro_auth.erl index 44cf751d9..5508c1acb 100644 --- a/apps/roster/src/protocol/micro_auth.erl +++ b/apps/roster/src/protocol/micro_auth.erl @@ -45,6 +45,13 @@ check(#mqtt_client{client_id = <<"emqttd_", _/binary>> = ClientId, AuthPid = n2o_async:pid(system, ?MODULE), Ver = binary_to_list(BVer), case kvs:get('Auth', ClientId) of + {ok, #'Auth'{user_id = PhoneId, type = logout}} -> + FreshAuth = #'Auth'{user_id = PhoneId, phone = roster:phone(PhoneId), + last_online = roster:now_msec(), type = verified}, + kvs:put(FreshAuth), + roster:info(?MODULE, "~p:Auth:auth(micro)/check:session created, post logout ", [ClientId]), + AuthPid ! roster_auth:control_ver(FreshAuth, Ver), + ok; {ok, #'Auth'{user_id = PhoneId} = Auth} -> AuthPid ! roster_auth:control_ver(Auth#'Auth'{type = []}, Ver), ok; diff --git a/apps/roster/src/protocol/roster_presence.erl b/apps/roster/src/protocol/roster_presence.erl index 68ca6c173..57e26ec43 100644 --- a/apps/roster/src/protocol/roster_presence.erl +++ b/apps/roster/src/protocol/roster_presence.erl @@ -36,11 +36,20 @@ on_disconnect(#'Auth'{type = logout, phone = Phone, client_id = ClientId, user_i roster:info(?MODULE, "~p:~p:DISCONNECT:LOGOUT", [Phone, ClientId]), send_presence(offline, Phone, C, ClientId), roster:unsubscribe_p2p(ClientId, roster:roster_id(PhoneId)), - roster:unsubscribe_room(ClientId), kvs:delete('Auth', ClientId), + roster:unsubscribe_room(ClientId), + kvs:delete('Auth', ClientId), roster:final_disconnect(ClientId); -on_disconnect(#'Auth'{phone = Phone, client_id = ClientId}, C) -> +on_disconnect(#'Auth'{type = disconnect, phone = Phone, client_id = ClientId, user_id = PhoneId}, C) -> roster:info(?MODULE, "~p:~p:DISCONNECT", [Phone, ClientId]), + send_presence(offline, Phone, C, ClientId), + roster:unsubscribe_p2p(ClientId, roster:roster_id(PhoneId)), + roster:unsubscribe_room(ClientId), + %% Warning: Do not delete Auth record, it holds push notification tokens + roster:final_disconnect(ClientId); + +on_disconnect(#'Auth'{phone = Phone, client_id = ClientId, type=Type}, C) -> + roster:info(?MODULE, "~p:~p:DISCONNECT:~p", [Phone, ClientId, Type]), send_presence(offline, Phone, C, ClientId). on_verify(ClientId, PhoneId) -> roster:sub_client(subscribe, ClientId, PhoneId). diff --git a/apps/roster/src/roster.erl b/apps/roster/src/roster.erl index 094ff5327..860add5bb 100644 --- a/apps/roster/src/roster.erl +++ b/apps/roster/src/roster.erl @@ -321,7 +321,8 @@ parts_phone_id(PhoneId) -> [Phone] -> list_rosters(Phone, first_roster); _E -> [<<>>, 0] end. roster_id(<<"emqttd_", _/binary>> = ClientId) -> - {ok, #'Auth'{user_id = PhoneId}} = kvs:get('Auth', ClientId), roster_id(PhoneId); + {ok, #'Auth'{user_id = PhoneId}} = kvs:get('Auth', ClientId), + roster_id(PhoneId); roster_id(PhoneId) -> [_, Id] = parts_phone_id(PhoneId), Id. roster_index(Index,V)-> [Id || #'Roster'{id=Id} <-kvs:index('Roster',Index,V)]. @@ -568,18 +569,24 @@ unload() -> emqttd:unhook('session.terminated', fun ?MODULE:on_session_terminated/4), emqttd:unhook('client.disconnected', fun ?MODULE:on_client_disconnected/3). -on_client_disconnected(_Reason, #mqtt_client{client_id = <<"emqttd_", _/bytes>> = ClientId}, _Env) -> +on_client_disconnected(_Reason, #mqtt_client{client_id = <<"emqttd_", _/bytes>> = ClientId} = Client, _Env) -> + info(roster_auth, "~p:MQTT_CLIENT DISCONNECT ~p",[ClientId, Client]), % TODO: mqttc:publish(C, lists:concat(["ses/",Phone])) on_disconnect(ClientId); -on_client_disconnected(_Reason, #mqtt_client{client_id = <<"reg_", _/bytes>> = ClientId}, _) -> +on_client_disconnected(_Reason, #mqtt_client{client_id = <<"reg_", _/bytes>> = ClientId} = Client, _) -> + info(roster_auth, "~p:MQTT_CLIENT DISCONNECT ~p",[ClientId, Client]), final_disconnect(ClientId); -on_client_disconnected(_Reason, _Client = #mqtt_client{}, _Env) -> ok. +on_client_disconnected(_Reason, #mqtt_client{client_id = ClientId} = Client, _Env) -> + info(roster_auth, "~p:MQTT_CLIENT DISCONNECT ~p",[ClientId, Client]), + ok. on_disconnect(<<"emqttd_", _/binary>> = ClientId) -> info(roster_auth, "~p:CLIENT DISCONNECT",[ClientId]), case kvs:get('Auth', ClientId) of - {ok, Auth} -> n2o_async:pid(system, roster_auth) ! {disconnect, Auth}, - kvs:put(Auth#'Auth'{last_online = now_msec()}); - _ -> skip end, + {ok, Auth} -> + n2o_async:pid(system, roster_auth) ! {disconnect, Auth#'Auth'{type = disconnect}}, + kvs:put(Auth#'Auth'{last_online = now_msec(), type = logout}); + _ -> skip + end, ok. final_disconnect(ClientId) -> % info(roster_auth, "~p:ANY DISCONNECT",[RegClientId]), -- GitLab From 43b9bd1d973f80efed1de5e89db12c23ac263c62 Mon Sep 17 00:00:00 2001 From: sennui Date: Wed, 11 Mar 2020 10:27:49 +0100 Subject: [PATCH 3/3] preserve auth entry after logout --- apps/roster/src/protocol/micro_auth.erl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/apps/roster/src/protocol/micro_auth.erl b/apps/roster/src/protocol/micro_auth.erl index 5508c1acb..00a0ef818 100644 --- a/apps/roster/src/protocol/micro_auth.erl +++ b/apps/roster/src/protocol/micro_auth.erl @@ -45,12 +45,11 @@ check(#mqtt_client{client_id = <<"emqttd_", _/binary>> = ClientId, AuthPid = n2o_async:pid(system, ?MODULE), Ver = binary_to_list(BVer), case kvs:get('Auth', ClientId) of - {ok, #'Auth'{user_id = PhoneId, type = logout}} -> - FreshAuth = #'Auth'{user_id = PhoneId, phone = roster:phone(PhoneId), - last_online = roster:now_msec(), type = verified}, - kvs:put(FreshAuth), + {ok, #'Auth'{user_id = PhoneId, type = logout} = Auth0} -> + Auth1 = Auth0#'Auth'{last_online = roster:now_msec(), type = verified}, + kvs:put(Auth1), roster:info(?MODULE, "~p:Auth:auth(micro)/check:session created, post logout ", [ClientId]), - AuthPid ! roster_auth:control_ver(FreshAuth, Ver), + AuthPid ! roster_auth:control_ver(Auth1, Ver), ok; {ok, #'Auth'{user_id = PhoneId} = Auth} -> AuthPid ! roster_auth:control_ver(Auth#'Auth'{type = []}, Ver), -- GitLab