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

Handle remove_domain in mod_muc_light #3107

Merged
merged 5 commits into from
May 4, 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
80 changes: 76 additions & 4 deletions big_tests/tests/domain_removal_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

-export([mam_pm_removal/1,
mam_muc_removal/1,
inbox_removal/1]).
inbox_removal/1,
muc_light_removal/1,
muc_light_blocking_removal/1]).

-import(distributed_helper, [mim/0, rpc/4]).

Expand All @@ -24,13 +26,16 @@

all() ->
[{group, mam_removal},
{group, inbox_removal}].
{group, inbox_removal},
{group, muc_light_removal}].

groups() ->
[
{mam_removal, [], [mam_pm_removal,
mam_muc_removal]},
{inbox_removal, [], [inbox_removal]}
{inbox_removal, [], [inbox_removal]},
{muc_light_removal, [], [muc_light_removal,
muc_light_blocking_removal]}
].

domain() ->
Expand Down Expand Up @@ -71,6 +76,8 @@ group_to_modules(mam_removal) ->
MH = muc_light_helper:muc_host(),
[{mod_mam_meta, [{backend, rdbms}, {pm, []}, {muc, [{host, MH}]}]},
{mod_muc_light, []}];
group_to_modules(muc_light_removal) ->
[{mod_muc_light, [{backend, rdbms}]}];
group_to_modules(inbox_removal) ->
[{mod_inbox, []}].

Expand Down Expand Up @@ -105,7 +112,7 @@ mam_muc_removal(Config0) ->
Room = muc_helper:fresh_room_name(),
MucHost = muc_light_helper:muc_host(),
muc_light_helper:create_room(Room, MucHost, alice,
[alice], Config, muc_light_helper:ver(1)),
[], Config, muc_light_helper:ver(1)),
RoomAddr = <<Room/binary, "@", MucHost/binary>>,
escalus:send(Alice, escalus_stanza:groupchat_to(RoomAddr, <<"text">>)),
escalus:wait_for_stanza(Alice),
Expand All @@ -126,5 +133,70 @@ inbox_removal(Config) ->
inbox_helper:get_inbox(Bob, #{count => 0, unread_messages => 0, active_conversations => 0})
end).

muc_light_removal(Config0) ->
F = fun(Config, Alice) ->
%% GIVEN a room
Room = muc_helper:fresh_room_name(),
MucHost = muc_light_helper:muc_host(),
muc_light_helper:create_room(Room, MucHost, alice,
[], Config, muc_light_helper:ver(1)),
RoomAddr = <<Room/binary, "@", MucHost/binary>>,
escalus:send(Alice, escalus_stanza:groupchat_to(RoomAddr, <<"text">>)),
escalus:wait_for_stanza(Alice),
RoomID = select_room_id(domain(), Room, MucHost),
{selected, [_]} = select_affs_by_room_id(domain(), RoomID),
{selected, [_|_]} = select_config_by_room_id(domain(), RoomID),
{ok, _RoomConfig, _AffUsers, _Version} = get_room_info(Room, MucHost),
%% WHEN domain hook called
run_remove_domain(),
%% THEN Room info not available
{error, not_exists} = get_room_info(Room, MucHost),
%% THEN Tables are empty
{selected, []} = select_affs_by_room_id(domain(), RoomID),
{selected, []} = select_config_by_room_id(domain(), RoomID)
end,
escalus_fresh:story_with_config(Config0, [{alice, 1}], F).

muc_light_blocking_removal(Config0) ->
F = fun(Config, Alice, Bob) ->
%% GIVEN a room
Room = muc_helper:fresh_room_name(),
MucHost = muc_light_helper:muc_host(),
muc_light_helper:create_room(Room, MucHost, alice,
[], Config, muc_light_helper:ver(1)),
block_muclight_user(Bob, Alice),
[_] = get_blocking(Bob, MucHost),
%% WHEN domain hook called
run_remove_domain(),
[] = get_blocking(Bob, MucHost)
end,
escalus_fresh:story_with_config(Config0, [{alice, 1}, {bob, 1}], F).

run_remove_domain() ->
rpc(mim(), mongoose_hooks, remove_domain, [domain(), domain()]).

get_room_info(RoomU, RoomS) ->
rpc(mim(), mod_muc_light_db_backend, get_info, [{RoomU, RoomS}]).

select_room_id(MainHost, RoomU, RoomS) ->
{selected, [{DbRoomID}]} =
rpc(mim(), mod_muc_light_db_rdbms, select_room_id, [MainHost, RoomU, RoomS]),
rpc(mim(), mongoose_rdbms, result_to_integer, [DbRoomID]).

select_affs_by_room_id(MainHost, RoomID) ->
rpc(mim(), mod_muc_light_db_rdbms, select_affs_by_room_id, [MainHost, RoomID]).

select_config_by_room_id(MainHost, RoomID) ->
rpc(mim(), mod_muc_light_db_rdbms, select_config_by_room_id, [MainHost, RoomID]).

get_blocking(User, MUCServer) ->
Jid = jid:from_binary(escalus_client:short_jid(User)),
{LUser, LServer, _} = jid:to_lower(Jid),
rpc(mim(), mod_muc_light_db_rdbms, get_blocking, [{LUser, LServer}, MUCServer]).

block_muclight_user(Bob, Alice) ->
%% Bob blocks Alice
AliceJIDBin = escalus_client:short_jid(Alice),
BlocklistChange = [{user, deny, AliceJIDBin}],
escalus:send(Bob, muc_light_helper:stanza_blocking_set(BlocklistChange)),
escalus:assert(is_iq_result, escalus:wait_for_stanza(Bob)).
14 changes: 3 additions & 11 deletions big_tests/tests/muc_light_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
stanza_aff_set/2,
default_config/0,
user_leave/3,
set_mod_config/3
set_mod_config/3,
stanza_blocking_set/1
]).

-include("muc_light.hrl").
Expand All @@ -103,7 +104,7 @@

-type ct_aff_user() :: {EscalusClient :: escalus:client(), Aff :: atom()}.
-type ct_aff_users() :: [ct_aff_user()].
-type ct_block_item() :: {What :: atom(), Action :: atom(), Who :: binary()}.
-type ct_block_item() :: muc_light_helper:ct_block_item().
-type verify_fun() :: muc_helper:verify_fun().
-type xmlel() :: exml:element().

Expand Down Expand Up @@ -952,15 +953,6 @@ stanza_aff_get(Room, Ver) ->
%% IQ setters
%%--------------------------------------------------------------------

-spec stanza_blocking_set(BlocklistChanges :: [ct_block_item()]) -> xmlel().
stanza_blocking_set(BlocklistChanges) ->
Items = [#xmlel{ name = list_to_binary(atom_to_list(What)),
attrs = [{<<"action">>, list_to_binary(atom_to_list(Action))}],
children = [#xmlcdata{ content = Who }] }
|| {What, Action, Who} <- BlocklistChanges],
escalus_stanza:to(escalus_stanza:iq_set(?NS_MUC_LIGHT_BLOCKING, Items), ?MUCHOST).


-spec stanza_destroy_room(Room :: binary()) -> xmlel().
stanza_destroy_room(Room) ->
escalus_stanza:to(escalus_stanza:iq_set(?NS_MUC_LIGHT_DESTROY, []), room_bin_jid(Room)).
Expand Down
21 changes: 21 additions & 0 deletions big_tests/tests/muc_light_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

-type ct_aff_user() :: {EscalusClient :: escalus:client(), Aff :: atom()}.
-type ct_aff_users() :: [ct_aff_user()].
-type ct_block_item() :: {What :: atom(), Action :: atom(), Who :: binary()}.

-export_type([ct_block_item/0]).

-spec room_bin_jid(Room :: binary()) -> binary().
room_bin_jid(Room) ->
Expand All @@ -27,6 +30,7 @@ create_room(RoomU, MUCHost, Owner, Members, Config, Version) ->
RoomUS = {RoomU, MUCHost},
AffUsers = [{to_lus(Owner, Config), owner}
| [ {to_lus(Member, Config), member} || Member <- Members ]],
assert_no_aff_duplicates(AffUsers),
AffUsersSort = lists:sort(AffUsers),
{ok, _RoomUS} = rpc(mim(), mod_muc_light_db_backend, create_room,
[RoomUS, DefaultConfig, AffUsersSort, Version]).
Expand Down Expand Up @@ -257,3 +261,20 @@ ver(Int) ->
set_mod_config(K, V, Host) ->
true = rpc(mim(), gen_mod, set_module_opt_by_subhost, [Host, mod_muc_light, K, V]).

assert_no_aff_duplicates(AffUsers) ->
Users = [US || {US, _} <- AffUsers],
case lists:sort(Users) =:= lists:usort(Users) of
true ->
ok;
false ->
ct:fail(#{what => assert_no_aff_duplicates,
aff_users => AffUsers})
end.

-spec stanza_blocking_set(BlocklistChanges :: [ct_block_item()]) -> exml:element().
stanza_blocking_set(BlocklistChanges) ->
Items = [#xmlel{ name = list_to_binary(atom_to_list(What)),
attrs = [{<<"action">>, list_to_binary(atom_to_list(Action))}],
children = [#xmlcdata{ content = Who }] }
|| {What, Action, Who} <- BlocklistChanges],
escalus_stanza:to(escalus_stanza:iq_set(?NS_MUC_LIGHT_BLOCKING, Items), muc_host()).
30 changes: 29 additions & 1 deletion doc/migrations/4.2.0_4.3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ ALTER TABLE inbox ADD PRIMARY KEY USING BTREE(lserver, luser, remote_bare_jid);
COMMIT;

CREATE INDEX i_inbox_timestamp ON inbox(lserver, luser, timestamp);
DROP INDEX i_inbox;
DROP INDEX i_inbox ON inbox;
```

For MSSQL:
Expand All @@ -50,3 +50,31 @@ ALTER TABLE inbox ADD CONSTRAINT PK_inbox PRIMARY KEY CLUSTERED(
lserver ASC, luser ASC, remote_bare_jid ASC);
GO
```

### MUC-light indexes

Order of fields in `i_muc_light_blocking` has changed.

For Postgres:

```sql
CREATE INDEX i_muc_light_blocking_su ON muc_light_blocking (lserver, luser);
DROP INDEX i_muc_light_blocking;
```

For MySQL:

```sql
CREATE INDEX i_muc_light_blocking_su USING BTREE ON muc_light_blocking (lserver, luser);
DROP INDEX i_muc_light_blocking ON muc_light_blocking;;
```

For MSSQL:

```sql
CREATE INDEX i_muc_light_blocking_su ON muc_light_blocking (lserver, luser);
GO

DROP INDEX i_muc_light_blocking ON muc_light_blocking;
GO
```
2 changes: 1 addition & 1 deletion priv/mssql2012.sql
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ CREATE TABLE dbo.muc_light_blocking(
)
GO

CREATE INDEX i_muc_light_blocking ON muc_light_blocking(luser, lserver);
CREATE INDEX i_muc_light_blocking_su ON muc_light_blocking(lserver, luser);
GO

-- luser, lserver and remote_bare_jid have 250 characters in MySQL
Expand Down
2 changes: 1 addition & 1 deletion priv/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ CREATE TABLE muc_light_blocking(
) CHARACTER SET utf8mb4
ROW_FORMAT=DYNAMIC;

CREATE INDEX i_muc_light_blocking USING HASH ON muc_light_blocking(luser, lserver);
CREATE INDEX i_muc_light_blocking_su USING BTREE ON muc_light_blocking(lserver, luser);

CREATE TABLE muc_rooms(
id SERIAL,
Expand Down
2 changes: 1 addition & 1 deletion priv/pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ CREATE TABLE muc_light_blocking(
who VARCHAR(500) NOT NULL
);

CREATE INDEX i_muc_light_blocking ON muc_light_blocking (luser, lserver);
CREATE INDEX i_muc_light_blocking_su ON muc_light_blocking (lserver, luser);

CREATE TABLE inbox (
luser VARCHAR(250) NOT NULL,
Expand Down
14 changes: 13 additions & 1 deletion src/muc_light/mod_muc_light.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
-export([prevent_service_unavailable/4,
get_muc_service/5,
remove_user/3,
remove_domain/3,
add_rooms_to_roster/2,
process_iq_get/5,
process_iq_set/4,
Expand Down Expand Up @@ -149,7 +150,8 @@ start(Host, Opts) ->
true -> ejabberd_hooks:add(roster_get, Host, ?MODULE, add_rooms_to_roster, 50)
end,

TrackedDBFuns = [create_room, destroy_room, room_exists, get_user_rooms, remove_user,
TrackedDBFuns = [create_room, destroy_room, room_exists, get_user_rooms,
remove_user, remove_domain,
get_config, set_config, get_blocking, set_blocking,
get_aff_users, modify_aff_users],
gen_mod:start_backend_module(mod_muc_light_db, Opts, TrackedDBFuns),
Expand Down Expand Up @@ -252,6 +254,7 @@ hooks(Host, MUCHost) ->

{offline_groupchat_message_hook, Host, ?MODULE, prevent_service_unavailable, 90},
{remove_user, Host, ?MODULE, remove_user, 50},
{remove_domain, Host, ?MODULE, remove_domain, 50},
{disco_local_items, Host, ?MODULE, get_muc_service, 50}].

%%====================================================================
Expand Down Expand Up @@ -363,6 +366,15 @@ remove_user(Acc, User, Server) ->
Acc
end.

-spec remove_domain(mongoose_hooks:simple_acc(),
mongooseim:host_type(), jid:lserver()) ->
mongoose_hooks:simple_acc().
remove_domain(Acc, HostType, Domain) ->
MUCHost = gen_mod:get_module_opt_subhost(Domain, ?MODULE, default_host()),
?LOG_ERROR(#{what => remove_domain_muc, host_type => HostType, domain => Domain}),
mod_muc_light_db_backend:remove_domain(HostType, MUCHost, Domain),
Acc.

-spec add_rooms_to_roster(Acc :: mongoose_acc:t(), UserJID :: jid:jid()) -> mongoose_acc:t().
add_rooms_to_roster(Acc, UserJID) ->
UserUS = jid:to_lus(UserJID),
Expand Down
3 changes: 3 additions & 0 deletions src/muc_light/mod_muc_light_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
-callback remove_user(UserUS :: jid:simple_bare_jid(), Version :: binary()) ->
remove_user_return() | {error, term()}.

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

%% ------------------------ Configuration manipulation ------------------------

-callback get_config(RoomUS :: jid:simple_bare_jid()) ->
Expand Down
5 changes: 5 additions & 0 deletions src/muc_light/mod_muc_light_db_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
get_user_rooms/2,
get_user_rooms_count/2,
remove_user/2,
remove_domain/3,

get_config/1,
set_config/3,
Expand Down Expand Up @@ -131,6 +132,10 @@ remove_user(UserUS, Version) ->
{atomic, Res} = mnesia:transaction(fun remove_user_transaction/2, [UserUS, Version]),
Res.

-spec remove_domain(mongooseim:host_type(), jid:lserver(), jid:lserver()) -> ok.
remove_domain(_HostType, _RoomS, _LServer) ->
ok.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note: we might mark Mnesia support as TODO somewhere not to forget about it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will not forget :D


%% ------------------------ Configuration manipulation ------------------------

-spec get_config(RoomUS :: jid:simple_bare_jid()) ->
Expand Down
Loading