diff options
Diffstat (limited to 'src/mod_push.erl')
-rw-r--r-- | src/mod_push.erl | 98 |
1 files changed, 58 insertions, 40 deletions
diff --git a/src/mod_push.erl b/src/mod_push.erl index 2ca0bf525..a57062eb9 100644 --- a/src/mod_push.erl +++ b/src/mod_push.erl @@ -61,21 +61,21 @@ -> any(). -callback store_session(binary(), binary(), timestamp(), jid(), binary(), xdata()) - -> {ok, push_session()} | error. + -> {ok, push_session()} | {error, any()}. -callback lookup_session(binary(), binary(), jid(), binary()) - -> {ok, push_session()} | error. + -> {ok, push_session()} | error | {error, any()}. -callback lookup_session(binary(), binary(), timestamp()) - -> {ok, push_session()} | error. + -> {ok, push_session()} | error | {error, any()}. -callback lookup_sessions(binary(), binary(), jid()) - -> {ok, [push_session()]} | error. + -> {ok, [push_session()]} | {error, any()}. -callback lookup_sessions(binary(), binary()) - -> {ok, [push_session()]} | error. + -> {ok, [push_session()]} | {error, any()}. -callback lookup_sessions(binary()) - -> {ok, [push_session()]} | error. + -> {ok, [push_session()]} | {error, any()}. -callback delete_session(binary(), binary(), timestamp()) - -> ok | error. + -> ok | {error, any()}. -callback delete_old_sessions(binary() | global, erlang:timestamp()) - -> any(). + -> ok | {error, any()}. -callback use_cache(binary()) -> boolean(). -callback cache_nodes(binary()) @@ -253,29 +253,39 @@ process_iq(#iq{lang = Lang, sub_els = [#push_enable{node = <<>>}]} = IQ) -> xmpp:make_error(IQ, xmpp:err_feature_not_implemented(Txt, Lang)); process_iq(#iq{from = #jid{lserver = LServer} = JID, to = #jid{lserver = LServer}, + lang = Lang, sub_els = [#push_enable{jid = PushJID, node = Node, xdata = XData}]} = IQ) -> case enable(JID, PushJID, Node, XData) of ok -> xmpp:make_iq_result(IQ); - error -> - xmpp:make_error(IQ, xmpp:err_internal_server_error()) + {error, db_failure} -> + Txt = <<"Database, failure">>, + xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang)); + {error, notfound} -> + Txt = <<"User session not found">>, + xmpp:make_error(IQ, xmpp:err_item_not_found(Txt, Lang)) end; process_iq(#iq{from = #jid{lserver = LServer} = JID, to = #jid{lserver = LServer}, + lang = Lang, sub_els = [#push_disable{jid = PushJID, node = Node}]} = IQ) -> case disable(JID, PushJID, Node) of ok -> xmpp:make_iq_result(IQ); - error -> - xmpp:make_error(IQ, xmpp:err_item_not_found()) + {error, db_failure} -> + Txt = <<"Database, failure">>, + xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang)); + {error, notfound} -> + Txt = <<"Push record not found">>, + xmpp:make_error(IQ, xmpp:err_item_not_found(Txt, Lang)) end; process_iq(IQ) -> xmpp:make_error(IQ, xmpp:err_not_allowed()). --spec enable(jid(), jid(), binary(), xdata()) -> ok | error. +-spec enable(jid(), jid(), binary(), xdata()) -> ok | {error, notfound | db_failure}. enable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID, PushJID, Node, XData) -> case ejabberd_sm:get_session_sid(LUser, LServer, LResource) of @@ -285,18 +295,18 @@ enable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID, ?INFO_MSG("Enabling push notifications for ~s", [jid:encode(JID)]), ejabberd_c2s:cast(PID, push_enable); - error -> + {error, _} -> ?ERROR_MSG("Cannot enable push for ~s: database error", [jid:encode(JID)]), - error + {error, db_failure} end; none -> ?WARNING_MSG("Cannot enable push for ~s: session not found", [jid:encode(JID)]), - error + {error, notfound} end. --spec disable(jid(), jid(), binary() | undefined) -> ok | error. +-spec disable(jid(), jid(), binary() | undefined) -> ok | {error, notfound | db_failure}. disable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID, PushJID, Node) -> case ejabberd_sm:get_session_sid(LUser, LServer, LResource) of @@ -308,7 +318,7 @@ disable(#jid{luser = LUser, lserver = LServer, lresource = LResource} = JID, ?WARNING_MSG("Session not found while disabling push for ~s", [jid:encode(JID)]) end, - if Node /= undefined -> + if Node /= <<>> -> delete_session(LUser, LServer, PushJID, Node); true -> delete_sessions(LUser, LServer, PushJID) @@ -388,7 +398,7 @@ c2s_handle_cast(State, push_disable) -> c2s_handle_cast(State, _Msg) -> State. --spec remove_user(binary(), binary()) -> ok | error. +-spec remove_user(binary(), binary()) -> ok | {error, any()}. remove_user(LUser, LServer) -> ?INFO_MSG("Removing any push sessions of ~s@~s", [LUser, LServer]), Mod = gen_mod:db_mod(LServer, ?MODULE), @@ -403,7 +413,7 @@ notify(#{jid := #jid{luser = LUser, lserver = LServer}, sid := {TS, _}}) -> case lookup_session(LUser, LServer, TS) of {ok, Client} -> notify(LUser, LServer, [Client]); - error -> + _Err -> ok end. @@ -440,7 +450,7 @@ notify(LServer, PushLJID, Node, XData, HandleResponse) -> %% Internal functions. %%-------------------------------------------------------------------- -spec store_session(binary(), binary(), timestamp(), jid(), binary(), xdata()) - -> {ok, push_session()} | error. + -> {ok, push_session()} | {error, any()}. store_session(LUser, LServer, TS, PushJID, Node, XData) -> Mod = gen_mod:db_mod(LServer, ?MODULE), delete_session(LUser, LServer, PushJID, Node), @@ -460,7 +470,7 @@ store_session(LUser, LServer, TS, PushJID, Node, XData) -> end. -spec lookup_session(binary(), binary(), timestamp()) - -> {ok, push_session()} | error. + -> {ok, push_session()} | error | {error, any()}. lookup_session(LUser, LServer, TS) -> Mod = gen_mod:db_mod(LServer, ?MODULE), case use_cache(Mod, LServer) of @@ -472,7 +482,7 @@ lookup_session(LUser, LServer, TS) -> Mod:lookup_session(LUser, LServer, TS) end. --spec lookup_sessions(binary(), binary()) -> {ok, [push_session()]} | error. +-spec lookup_sessions(binary(), binary()) -> {ok, [push_session()]} | {error, any()}. lookup_sessions(LUser, LServer) -> Mod = gen_mod:db_mod(LServer, ?MODULE), case use_cache(Mod, LServer) of @@ -484,40 +494,48 @@ lookup_sessions(LUser, LServer) -> Mod:lookup_sessions(LUser, LServer) end. --spec delete_session(binary(), binary(), timestamp()) -> ok | error. +-spec delete_session(binary(), binary(), timestamp()) -> ok | {error, db_failure}. delete_session(LUser, LServer, TS) -> Mod = gen_mod:db_mod(LServer, ?MODULE), - ok = Mod:delete_session(LUser, LServer, TS), - case use_cache(Mod, LServer) of - true -> - ets_cache:delete(?PUSH_CACHE, {LUser, LServer}, - cache_nodes(Mod, LServer)), - ets_cache:delete(?PUSH_CACHE, {LUser, LServer, TS}, - cache_nodes(Mod, LServer)); - false -> - ok + case Mod:delete_session(LUser, LServer, TS) of + ok -> + case use_cache(Mod, LServer) of + true -> + ets_cache:delete(?PUSH_CACHE, {LUser, LServer}, + cache_nodes(Mod, LServer)), + ets_cache:delete(?PUSH_CACHE, {LUser, LServer, TS}, + cache_nodes(Mod, LServer)); + false -> + ok + end; + {error, _} -> + {error, db_failure} end. --spec delete_session(binary(), binary(), jid(), binary()) -> ok | error. +-spec delete_session(binary(), binary(), jid(), binary()) -> ok | {error, notfound | db_failure}. delete_session(LUser, LServer, PushJID, Node) -> Mod = gen_mod:db_mod(LServer, ?MODULE), case Mod:lookup_session(LUser, LServer, PushJID, Node) of {ok, {TS, _, _, _}} -> delete_session(LUser, LServer, TS); error -> - error + {error, notfound}; + {error, _} -> + {error, db_failure} end. --spec delete_sessions(binary(), binary(), jid()) -> ok | error. +-spec delete_sessions(binary(), binary(), jid()) -> ok | {error, notfound | db_failure}. delete_sessions(LUser, LServer, PushJID) -> Mod = gen_mod:db_mod(LServer, ?MODULE), LookupFun = fun() -> Mod:lookup_sessions(LUser, LServer, PushJID) end, delete_sessions(LUser, LServer, LookupFun, Mod). --spec delete_sessions(binary(), binary(), fun(() -> ok | error), module()) - -> ok | error. +-spec delete_sessions(binary(), binary(), fun(() -> any()), module()) + -> ok | {error, _}. delete_sessions(LUser, LServer, LookupFun, Mod) -> case LookupFun() of + {ok, []} -> + {error, notfound}; {ok, Clients} -> case use_cache(Mod, LServer) of true -> @@ -538,8 +556,8 @@ delete_sessions(LUser, LServer, LookupFun, Mod) -> ok end end, Clients); - error -> - error + {error, _} -> + {error, db_failure} end. -spec drop_online_sessions(binary(), binary(), [push_session()]) |