mod_mam: Additional checks for database failures
This commit is contained in:
parent
51cbbf313f
commit
d5fab856f4
|
@ -52,6 +52,7 @@
|
||||||
-define(MAX_PAGE_SIZE, 250).
|
-define(MAX_PAGE_SIZE, 250).
|
||||||
|
|
||||||
-type c2s_state() :: ejabberd_c2s:state().
|
-type c2s_state() :: ejabberd_c2s:state().
|
||||||
|
-type count() :: non_neg_integer() | undefined.
|
||||||
|
|
||||||
-callback init(binary(), gen_mod:opts()) -> any().
|
-callback init(binary(), gen_mod:opts()) -> any().
|
||||||
-callback remove_user(binary(), binary()) -> any().
|
-callback remove_user(binary(), binary()) -> any().
|
||||||
|
@ -66,7 +67,8 @@
|
||||||
-callback get_prefs(binary(), binary()) -> {ok, #archive_prefs{}} | error.
|
-callback get_prefs(binary(), binary()) -> {ok, #archive_prefs{}} | error.
|
||||||
-callback select(binary(), jid(), jid(), mam_query:result(),
|
-callback select(binary(), jid(), jid(), mam_query:result(),
|
||||||
#rsm_set{} | undefined, chat | groupchat) ->
|
#rsm_set{} | undefined, chat | groupchat) ->
|
||||||
{[{binary(), non_neg_integer(), xmlel()}], boolean(), non_neg_integer()}.
|
{[{binary(), non_neg_integer(), xmlel()}], boolean(), count()} |
|
||||||
|
{error, db_failure}.
|
||||||
-callback use_cache(binary()) -> boolean().
|
-callback use_cache(binary()) -> boolean().
|
||||||
-callback cache_nodes(binary()) -> [node()].
|
-callback cache_nodes(binary()) -> [node()].
|
||||||
-callback remove_from_archive(binary(), binary(), jid() | none) -> ok | {error, any()}.
|
-callback remove_from_archive(binary(), binary(), jid() | none) -> ok | {error, any()}.
|
||||||
|
@ -89,7 +91,8 @@ start(Host, Opts) ->
|
||||||
ok
|
ok
|
||||||
end,
|
end,
|
||||||
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
||||||
Mod:init(Host, Opts),
|
case Mod:init(Host, Opts) of
|
||||||
|
ok ->
|
||||||
init_cache(Mod, Host, Opts),
|
init_cache(Mod, Host, Opts),
|
||||||
register_iq_handlers(Host),
|
register_iq_handlers(Host),
|
||||||
ejabberd_hooks:add(sm_receive_packet, Host, ?MODULE,
|
ejabberd_hooks:add(sm_receive_packet, Host, ?MODULE,
|
||||||
|
@ -123,8 +126,10 @@ start(Host, Opts) ->
|
||||||
false ->
|
false ->
|
||||||
ok
|
ok
|
||||||
end,
|
end,
|
||||||
ejabberd_commands:register_commands(get_commands_spec()),
|
ejabberd_commands:register_commands(get_commands_spec());
|
||||||
ok.
|
Err ->
|
||||||
|
Err
|
||||||
|
end.
|
||||||
|
|
||||||
use_cache(Mod, Host) ->
|
use_cache(Mod, Host) ->
|
||||||
case erlang:function_exported(Mod, use_cache, 2) of
|
case erlang:function_exported(Mod, use_cache, 2) of
|
||||||
|
@ -940,15 +945,21 @@ maybe_activate_mam(LUser, LServer) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
select_and_send(LServer, Query, RSM, #iq{from = From, to = To} = IQ, MsgType) ->
|
select_and_send(LServer, Query, RSM, #iq{from = From, to = To} = IQ, MsgType) ->
|
||||||
{Msgs, IsComplete, Count} =
|
Ret = case MsgType of
|
||||||
case MsgType of
|
|
||||||
chat ->
|
chat ->
|
||||||
select(LServer, From, From, Query, RSM, MsgType);
|
select(LServer, From, From, Query, RSM, MsgType);
|
||||||
{groupchat, _Role, _MUCState} ->
|
{groupchat, _Role, _MUCState} ->
|
||||||
select(LServer, From, To, Query, RSM, MsgType)
|
select(LServer, From, To, Query, RSM, MsgType)
|
||||||
end,
|
end,
|
||||||
|
case Ret of
|
||||||
|
{Msgs, IsComplete, Count} ->
|
||||||
SortedMsgs = lists:keysort(2, Msgs),
|
SortedMsgs = lists:keysort(2, Msgs),
|
||||||
send(SortedMsgs, Count, IsComplete, IQ).
|
send(SortedMsgs, Count, IsComplete, IQ);
|
||||||
|
{error, _} ->
|
||||||
|
Txt = <<"Database failure">>,
|
||||||
|
Err = xmpp:err_internal_server_error(Txt, IQ#iq.lang),
|
||||||
|
xmpp:make_error(IQ, Err)
|
||||||
|
end.
|
||||||
|
|
||||||
select(_LServer, JidRequestor, JidArchive, Query, RSM,
|
select(_LServer, JidRequestor, JidArchive, Query, RSM,
|
||||||
{groupchat, _Role, #state{config = #config{mam = false},
|
{groupchat, _Role, #state{config = #config{mam = false},
|
||||||
|
@ -1045,7 +1056,7 @@ maybe_update_from_to(Pkt, _JidRequestor, _JidArchive, _Peer, chat, _Nick) ->
|
||||||
Pkt.
|
Pkt.
|
||||||
|
|
||||||
-spec send([{binary(), integer(), xmlel()}],
|
-spec send([{binary(), integer(), xmlel()}],
|
||||||
non_neg_integer(), boolean(), iq()) -> iq() | ignore.
|
count(), boolean(), iq()) -> iq() | ignore.
|
||||||
send(Msgs, Count, IsComplete,
|
send(Msgs, Count, IsComplete,
|
||||||
#iq{from = From, to = To,
|
#iq{from = From, to = To,
|
||||||
sub_els = [#mam_query{id = QID, xmlns = NS}]} = IQ) ->
|
sub_els = [#mam_query{id = QID, xmlns = NS}]} = IQ) ->
|
||||||
|
@ -1083,7 +1094,7 @@ send(Msgs, Count, IsComplete,
|
||||||
ignore
|
ignore
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec make_rsm_out([{binary(), integer(), xmlel()}], non_neg_integer()) -> rsm_set().
|
-spec make_rsm_out([{binary(), integer(), xmlel()}], count()) -> rsm_set().
|
||||||
make_rsm_out([], Count) ->
|
make_rsm_out([], Count) ->
|
||||||
#rsm_set{count = Count};
|
#rsm_set{count = Count};
|
||||||
make_rsm_out([{FirstID, _, _}|_] = Msgs, Count) ->
|
make_rsm_out([{FirstID, _, _}|_] = Msgs, Count) ->
|
||||||
|
|
|
@ -48,13 +48,20 @@
|
||||||
%%% API
|
%%% API
|
||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
init(_Host, _Opts) ->
|
init(_Host, _Opts) ->
|
||||||
ejabberd_mnesia:create(?MODULE, archive_msg,
|
try
|
||||||
|
{atomic, _} = ejabberd_mnesia:create(
|
||||||
|
?MODULE, archive_msg,
|
||||||
[{disc_only_copies, [node()]},
|
[{disc_only_copies, [node()]},
|
||||||
{type, bag},
|
{type, bag},
|
||||||
{attributes, record_info(fields, archive_msg)}]),
|
{attributes, record_info(fields, archive_msg)}]),
|
||||||
ejabberd_mnesia:create(?MODULE, archive_prefs,
|
{atomic, _} = ejabberd_mnesia:create(
|
||||||
|
?MODULE, archive_prefs,
|
||||||
[{disc_only_copies, [node()]},
|
[{disc_only_copies, [node()]},
|
||||||
{attributes, record_info(fields, archive_prefs)}]).
|
{attributes, record_info(fields, archive_prefs)}]),
|
||||||
|
ok
|
||||||
|
catch _:{badmatch, _} ->
|
||||||
|
{error, db_failure}
|
||||||
|
end.
|
||||||
|
|
||||||
remove_user(LUser, LServer) ->
|
remove_user(LUser, LServer) ->
|
||||||
US = {LUser, LServer},
|
US = {LUser, LServer},
|
||||||
|
|
Loading…
Reference in New Issue