From 6b46b8f794c2d8a941670f4e837e2588446654e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20K=C3=B6hler?= Date: Fri, 5 Nov 2010 18:32:25 +0100 Subject: [PATCH] Refactor mod_last to use the same core get_last/2 functionality, but keep api stable The local function get_last/4 has been renamed to get_last_iq/4, since it converts the result of get_last/2 (typically {ok, TimeStamp, Status}) to an iq packet. --- src/mod_last.erl | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/mod_last.erl b/src/mod_last.erl index bf1b7f1ce..9e9a73272 100644 --- a/src/mod_last.erl +++ b/src/mod_last.erl @@ -156,7 +156,7 @@ process_sm_iq(From, To, #iq{type = get} = IQ_Rec) -> exmpp_presence:available()}, out]) of allow -> - get_last(IQ_Rec, exmpp_jid:prep_node(To), exmpp_jid:prep_domain(To)); + get_last_iq(IQ_Rec, exmpp_jid:prep_node(To), exmpp_jid:prep_domain(To)); deny -> exmpp_iq:error(IQ_Rec, 'forbidden') end; @@ -167,15 +167,26 @@ process_sm_iq(_From, _To, #iq{type = set} = IQ_Rec) -> exmpp_iq:error(IQ_Rec, 'not-allowed'). %% TODO: This function could use get_last_info/2 -get_last(IQ_Rec, LUser, LServer) -> +%% @spec (LUser::string(), LServer::string()) -> +%% {ok, TimeStamp::integer(), Status::string()} | not_found | {error, Reason} +get_last(LUser, LServer) -> case catch gen_storage:dirty_read(LServer, last_activity, {LUser, LServer}) of - {'EXIT', _Reason} -> - exmpp_iq:error(IQ_Rec, 'internal-server-error'); + {'EXIT', Reason} -> + {error, Reason}; [] -> - exmpp_iq:error(IQ_Rec, 'service-unavailable'); + not_found; [#last_activity{timestamp = TimeStamp, status = Status}] -> - {MegaSecs, Secs, _MicroSecs} = now(), - TimeStamp2 = MegaSecs * 1000000 + Secs, + {ok, TimeStamp, Status} + end. + +get_last_iq(IQ_Rec, LUser, LServer) -> + case get_last(LUser, LServer) of + {error, _Reason} -> + exmpp_iq:error(IQ_Rec, 'internal-server-error'); + not_found -> + exmpp_iq:error(IQ_Rec, 'service-unavailable'); + {ok, TimeStamp, Status} -> + TimeStamp2 = now_to_seconds(now()), Sec = TimeStamp2 - TimeStamp, Response = #xmlel{ns = ?NS_LAST_ACTIVITY, name = 'query', attrs = [?XMLATTR('seconds', Sec)], @@ -183,8 +194,6 @@ get_last(IQ_Rec, LUser, LServer) -> exmpp_iq:result(IQ_Rec, Response) end. - - on_presence_update(User, Server, _Resource, Status) -> {MegaSecs, Secs, _MicroSecs} = now(), TimeStamp = MegaSecs * 1000000 + Secs, @@ -211,13 +220,11 @@ store_last_info(User, Server, TimeStamp, Status) get_last_info(LUser, LServer) when is_list(LUser), is_list(LServer) -> get_last_info(list_to_binary(LUser), list_to_binary(LServer)); get_last_info(LUser, LServer) when is_binary(LUser), is_binary(LServer) -> - case catch gen_storage:dirty_read(LServer, last_activity, {LUser, LServer}) of - {'EXIT', _Reason} -> + case get_last(LUser, LServer) of + {error, _Reason} -> not_found; - [] -> - not_found; - [#last_activity{timestamp = TimeStamp, status = Status}] -> - {ok, TimeStamp, Status} + Res -> + Res end. remove_user(User, Server) when is_binary(User), is_binary(Server) ->