Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement domain removal in mod_offline #3299

Merged
merged 4 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions big_tests/tests/domain_removal_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ all() ->
{group, inbox_removal},
{group, muc_light_removal},
{group, private_removal},
{group, roster_removal}].
{group, roster_removal},
{group, offline_removal}].

groups() ->
[
Expand All @@ -29,7 +30,8 @@ groups() ->
{muc_light_removal, [], [muc_light_removal,
muc_light_blocking_removal]},
{private_removal, [], [private_removal]},
{roster_removal, [], [roster_removal]}
{roster_removal, [], [roster_removal]},
{offline_removal, [], [offline_removal]}
].

%%%===================================================================
Expand Down Expand Up @@ -78,8 +80,9 @@ group_to_modules(private_removal) ->
group_to_modules(roster_removal) ->
[{mod_roster, [{backend, rdbms}]}];
group_to_modules(auth_removal) ->
[].

[];
group_to_modules(offline_removal) ->
[{mod_offline, [{backend, rdbms}]}].

%%%===================================================================
%%% Testcase specific setup/teardown
Expand Down Expand Up @@ -214,6 +217,21 @@ private_removal(Config) ->
?assert_equal_extra(<<>>, Val2, #{stanza => Res2})
end).

offline_removal(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}, {bob, 1}], fun(FreshConfig, Alice, Bob) ->
mongoose_helper:logout_user(FreshConfig, Bob),
escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"msgtxt">>)),
% wait until message is stored
BobJid = jid:from_binary(escalus_client:full_jid(Bob)),
{LUser, LServer} = jid:to_lus(BobJid),
mongoose_helper:wait_until(
fun() -> mongoose_helper:total_offline_messages({LUser, LServer}) end, 1),
% check messages in DB
?assertMatch({ok, [_]}, rpc(mim(), mod_offline_rdbms, fetch_messages, [host_type(), BobJid])),
run_remove_domain(),
?assertMatch({ok, []}, rpc(mim(), mod_offline_rdbms, fetch_messages, [host_type(), BobJid]))
end).

roster_removal(Config) ->
escalus:fresh_story(Config, [{alice, 1}, {bob, 1}], fun(Alice, Bob) ->
%% add contact
Expand All @@ -228,33 +246,24 @@ roster_removal(Config) ->
escalus:assert(is_roster_result, Received2),
escalus:assert(roster_contains, [BobJid], Received2),
escalus:assert(count_roster_items, [1], Received2),

{selected, [_]} = select_rosterusers(host_type(), domain()),
{selected, [_]} = select_rostergroups(host_type(), domain()),
{selected, [_]} = select_roster_version(host_type(), domain()),
?assertMatch([_], select_from_roster("rosterusers")),
?assertMatch([_], select_from_roster("rostergroups")),
?assertMatch([_], select_from_roster("roster_version")),

%% remove domain and check roster
run_remove_domain(),
Received3 = escalus:send_iq_and_wait_for_result(Alice, escalus_stanza:roster_get()),
escalus:assert(is_roster_result, Received3),
escalus:assert(count_roster_items, [0], Received3),

{selected, []} = select_rosterusers(host_type(), domain()),
{selected, []} = select_rostergroups(host_type(), domain()),
{selected, []} = select_roster_version(host_type(), domain())
end).

select_rosterusers(HostType, Domain) ->
Query = "SELECT * FROM rosterusers WHERE server='" ++ binary_to_list(Domain) ++ "'",
rpc(mim(), mongoose_rdbms, sql_query, [HostType, Query]).

select_rostergroups(HostType, Domain) ->
Query = "SELECT * FROM rostergroups WHERE server='" ++ binary_to_list(Domain) ++ "'",
rpc(mim(), mongoose_rdbms, sql_query, [HostType, Query]).

select_roster_version(HostType, Domain) ->
Query = "SELECT * FROM roster_version WHERE server='" ++ binary_to_list(Domain) ++ "'",
rpc(mim(), mongoose_rdbms, sql_query, [HostType, Query]).
?assertMatch([], select_from_roster("rosterusers")),
?assertMatch([], select_from_roster("rostergroups")),
?assertMatch([], select_from_roster("roster_version"))
end).

select_from_roster(Table) ->
Query = "SELECT * FROM " ++ Table ++ " WHERE server='" ++ binary_to_list(domain()) ++ "'",
{selected, Res} = rpc(mim(), mongoose_rdbms, sql_query, [host_type(), Query]),
Res.

run_remove_domain() ->
rpc(mim(), mongoose_hooks, remove_domain, [host_type(), domain()]).
Expand Down
21 changes: 20 additions & 1 deletion src/offline/mod_offline.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
-export([inspect_packet/4,
pop_offline_messages/2,
remove_user/3,
remove_domain/3,
disco_features/1,
determine_amp_strategy/5,
amp_failed_event/1,
Expand Down Expand Up @@ -69,6 +70,7 @@
{?MOD_OFFLINE_BACKEND, remove_expired_messages, 1},
{?MOD_OFFLINE_BACKEND, remove_old_messages, 2},
{?MOD_OFFLINE_BACKEND, remove_user, 2},
{?MOD_OFFLINE_BACKEND, remove_domain, 2},
{?MOD_OFFLINE_BACKEND, init, 2},
{?MOD_OFFLINE_BACKEND, write_messages, 3},
{?MOD_OFFLINE_BACKEND, write_messages, 4},
Expand All @@ -78,7 +80,7 @@
amp_failed_event/1, behaviour_info/1, code_change/3, determine_amp_strategy/5,
disco_features/1, get_personal_data/3, handle_call/3, handle_cast/2,
handle_info/2, init/1, inspect_packet/4, pop_offline_messages/2, remove_user/2,
remove_user/3, start_link/3, terminate/2
remove_user/3, remove_domain/3, start_link/3, terminate/2
]).

-include("mongoose.hrl").
Expand Down Expand Up @@ -134,6 +136,10 @@

-callback remove_user(host_type(), jid:luser(), jid:lserver()) -> ok.

-callback remove_domain(mongooseim:host_type(), jid:lserver()) -> ok.

-optional_callbacks([remove_domain/2]).

%% Types used in backend callbacks
-type msg_count() :: non_neg_integer().
-type timestamp() :: integer().
Expand Down Expand Up @@ -181,6 +187,7 @@ hooks(HostType) ->
{offline_message_hook, HostType, ?MODULE, inspect_packet, 50},
{resend_offline_messages_hook, HostType, ?MODULE, pop_offline_messages, 50},
{remove_user, HostType, ?MODULE, remove_user, 50},
{remove_domain, HostType, ?MODULE, remove_domain, 50},
{anonymous_purge_hook, HostType, ?MODULE, remove_user, 50},
{disco_sm_features, HostType, ?MODULE, disco_features, 50},
{disco_local_features, HostType, ?MODULE, disco_features, 50},
Expand Down Expand Up @@ -497,6 +504,18 @@ remove_user(Acc, LUser, LServer) ->
mod_offline_backend:remove_user(HostType, LUser, LServer),
Acc.

-spec remove_domain(mongoose_hooks:simple_acc(),
mongooseim:host_type(), jid:lserver()) ->
mongoose_hooks:simple_acc().
remove_domain(Acc, HostType, Domain) ->
case backend_module:is_exported(mod_offline_backend, remove_domain, 2) of
true ->
mod_offline_backend:remove_domain(HostType, Domain);
false ->
ok
end,
Acc.

-spec disco_features(mongoose_disco:feature_acc()) -> mongoose_disco:feature_acc().
disco_features(Acc = #{node := <<>>}) ->
mongoose_disco:add_features([?NS_FEATURE_MSGOFFLINE], Acc);
Expand Down
11 changes: 10 additions & 1 deletion src/offline/mod_offline_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
count_offline_messages/4,
remove_expired_messages/2,
remove_old_messages/3,
remove_user/3]).
remove_user/3,
remove_domain/2]).

-import(mongoose_rdbms, [prepare/4, execute_successfully/3]).

Expand Down Expand Up @@ -69,6 +70,9 @@ prepare_queries() ->
[server, username],
<<"DELETE FROM offline_message "
"WHERE server = ? AND username = ?">>),
prepare(offline_remove_domain, offline_message,
[server],
<<"DELETE FROM offline_message WHERE server = ?">>),
prepare(offline_delete_old, offline_message,
[server, timestamp],
<<"DELETE FROM offline_message WHERE server = ? AND timestamp < ?">>),
Expand Down Expand Up @@ -184,6 +188,11 @@ remove_user(HostType, LUser, LServer) ->
execute_offline_delete(HostType, LUser, LServer),
ok.

-spec remove_domain(mongooseim:host_type(), jid:lserver()) -> ok.
remove_domain(HostType, Domain) ->
mongoose_rdbms:execute_successfully(HostType, offline_remove_domain, [Domain]),
ok.

%% Pure helper functions
record_to_row(LUser, LServer,
#offline_msg{timestamp = TimeStamp, expire = Expire, from = From,
Expand Down