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.
This commit is contained in:
Andreas Köhler 2010-11-05 18:32:25 +01:00 committed by Badlop
parent 7d93cad452
commit 6b46b8f794
1 changed files with 22 additions and 15 deletions

View File

@ -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) ->