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

Check MUC domain in mod_mam_muc #3936

Merged
merged 2 commits into from
Jan 27, 2023
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
1 change: 1 addition & 0 deletions .circleci/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ executors:
otp_25:
docker:
- image: *OTP25
resource_class: medium+
otp_24_redis:
docker:
- image: *OTP24
Expand Down
58 changes: 57 additions & 1 deletion big_tests/tests/graphql_muc_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ user_muc_tests() ->

user_muc_with_mam_tests() ->
[user_get_room_messages,
user_shouldnt_store_messages_in_muc_light,
user_try_get_nonexistent_room_messages,
user_try_get_room_messages_without_permission].

Expand Down Expand Up @@ -250,7 +251,7 @@ init_per_group(Group, Config) when Group =:= admin_muc_and_mam_configured;
case maybe_enable_mam() of
true ->
ensure_muc_started(),
Config;
ensure_muc_light_started(Config);
false ->
{skip, "No MAM backend available"}
end;
Expand Down Expand Up @@ -287,11 +288,34 @@ ensure_muc_stopped() ->
muc_helper:unload_muc(),
muc_helper:unload_muc(SecondaryHostType).

ensure_muc_light_started(Config) ->
MucLightOpts = config_parser_helper:mod_config(mod_muc_light,
#{rooms_in_rosters => true, config_schema => custom_schema()}),
HostType = domain_helper:host_type(),
dynamic_modules:ensure_modules(HostType, [{mod_muc_light, MucLightOpts}]),
[{muc_light_host, muc_light_helper:muc_host()} | Config].

ensure_muc_light_stopped() ->
HostType = domain_helper:host_type(),
dynamic_modules:ensure_modules(HostType, [{mod_muc_light, stopped}]).

custom_schema() ->
%% Should be sorted
[{<<"background">>, <<>>, background, binary},
{<<"music">>, <<>>, music, binary},
%% Default fields
{<<"roomname">>, <<>>, roomname, binary},
{<<"subject">>, <<"Test">>, subject, binary}].

end_per_group(Group, _Config) when Group =:= user;
Group =:= admin_http;
Group =:= domain_admin_muc;
Group =:= admin_cli ->
graphql_helper:clean();
end_per_group(Group, _Config) when Group =:= admin_muc_and_mam_configured;
Group =:= user_muc_and_mam_configured ->
ensure_muc_light_stopped(),
escalus_fresh:clean();
end_per_group(_Group, _Config) ->
escalus_fresh:clean().

Expand Down Expand Up @@ -1387,6 +1411,26 @@ user_get_room_messages_story(Config, Alice, Bob) ->
get_ok_value(?GET_MESSAGES_PATH, Res),
?assertMatch({ok, #xmlel{name = <<"message">>}}, exml:parse(StanzaXML)).

user_shouldnt_store_messages_in_muc_light(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}],
fun user_shouldnt_store_messages_in_muc_light_story/2).

user_shouldnt_store_messages_in_muc_light_story(Config, Alice) ->
%% Create a MUC Light room
MUCServer = ?config(muc_light_host, Config),
AliceBin = escalus_client:short_jid(Alice),
{ok, #{jid := RoomJID}} =
create_muc_light_room(MUCServer, <<"first room">>, <<"subject">>, AliceBin),
%% Send a message
Message = <<"Hello friends">>,
send_message_to_muc_light_room(RoomJID, jid:from_binary(AliceBin), Message),
escalus:wait_for_stanza(Alice),
%% Try to get a MUC Light message
Limit = 1,
Res = user_get_muc_light_room_messages(Alice, jid:to_binary(RoomJID), Limit, null, Config),
#{<<"stanzas">> := [], <<"limit">> := Limit} =
get_ok_value([data, muc_light, getRoomMessages], Res).

user_try_get_nonexistent_room_messages(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}],
fun user_try_get_nonexistent_room_messages_story/2).
Expand Down Expand Up @@ -1881,6 +1925,11 @@ get_room_name() ->

%% Commands

create_muc_light_room(Domain, Name, Subject, CreatorBin) ->
CreatorJID = jid:from_binary(CreatorBin),
Config = #{<<"roomname">> => Name, <<"subject">> => Subject},
rpc(mim(), mod_muc_light_api, create_room, [Domain, CreatorJID, Config]).

create_instant_room(Room, Owner, Nick, Config) ->
Vars = #{room => room_to_bin(Room), owner => user_to_bin(Owner), nick => Nick},
execute_command(<<"muc">>, <<"createInstantRoom">>, Vars, Config).
Expand All @@ -1903,6 +1952,9 @@ send_private_message(Room, From, ToNick, Body, Config) ->
toNick => ToNick, body => Body},
execute_command(<<"muc">>, <<"sendPrivateMessage">>, Vars, Config).

send_message_to_muc_light_room(RoomJID, SenderJID, Message) ->
rpc(mim(), mod_muc_light_api, send_message, [RoomJID, SenderJID, Message]).

enter_room(Room, User, Nick, Password, Config) ->
Vars = #{room => jid:to_binary(Room), user => user_to_full_bin(User),
nick => Nick, password => Password},
Expand Down Expand Up @@ -1964,6 +2016,10 @@ user_get_room_messages(User, RoomJID, PageSize, Before, Config) ->
<<"before">> => Before},
execute_user_command(<<"muc">>, <<"getRoomMessages">>, User, Vars, Config).

user_get_muc_light_room_messages(User, RoomJID, PageSize, Before, Config) ->
Vars = #{<<"room">> => RoomJID, <<"pageSize">> => PageSize, <<"before">> => Before},
execute_user_command(<<"muc_light">>, <<"getRoomMessages">>, User, Vars, Config).

user_delete_room(User, Room, Reason, Config) ->
Vars = #{room => jid:to_binary(Room), reason => Reason},
execute_user_command(<<"muc">>, <<"deleteRoom">>, User, Vars, Config).
Expand Down
51 changes: 49 additions & 2 deletions big_tests/tests/graphql_muc_light_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ user_muc_light_tests() ->
].

user_muc_light_with_mam_tests() ->
[user_get_room_messages].
[user_get_room_messages,
user_shouldnt_store_messages_in_muc].

user_muc_light_not_configured_tests() ->
[user_create_room_muc_light_not_configured,
Expand Down Expand Up @@ -236,6 +237,7 @@ init_per_group(Group, Config) when Group =:= user_muc_light_with_mam;
Group =:= domain_admin_muc_light_with_mam ->
case maybe_enable_mam() of
true ->
ensure_muc_started(),
ensure_muc_light_started(Config);
false ->
{skip, "No MAM backend available"}
Expand Down Expand Up @@ -279,11 +281,23 @@ ensure_muc_light_stopped(Config) ->
dynamic_modules:ensure_modules(SecondaryHostType, [{mod_muc_light, stopped}]),
[{muc_light_host, <<"NON_EXISTING">>} | Config].

ensure_muc_started() ->
muc_helper:load_muc(),
mongoose_helper:ensure_muc_clean().

ensure_muc_stopped() ->
muc_helper:unload_muc().

end_per_group(Group, _Config) when Group =:= user;
Group =:= admin_http;
Group =:= admin_cli;
Group =:= domain_admin ->
graphql_helper:clean();
end_per_group(Group, _Config) when Group =:= user_muc_light_with_mam;
Group =:= admin_muc_light_with_mam;
Group =:= domain_admin_muc_light_with_mam ->
ensure_muc_stopped(),
escalus_fresh:clean();
end_per_group(_Group, _Config) ->
escalus_fresh:clean().

Expand Down Expand Up @@ -622,6 +636,24 @@ user_get_room_messages_story(Config, Alice, Bob) ->
Res5 = user_get_room_messages(Bob, jid:to_binary(RoomJID), Limit, null, Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res5), <<"not occupy this room">>)).

user_shouldnt_store_messages_in_muc(Config) ->
muc_helper:story_with_room(Config, [], [{alice, 1}],
fun user_shouldnt_store_messages_in_muc_story/2).

user_shouldnt_store_messages_in_muc_story(Config, Alice) ->
%% Enter a MUC room
MUCRoomJID = jid:from_binary(?config(room_jid, Config)),
enter_muc_room(MUCRoomJID, Alice, <<"Ali">>),
escalus:wait_for_stanza(Alice),
%% Send a message
Message = <<"Hello friends">>,
send_message_to_muc_room(Alice, MUCRoomJID, Message, null, Config),
escalus:wait_for_stanza(Alice),
%% Try to get a MUC message
Limit = 1,
Res3 = user_get_muc_room_messages(Alice, jid:to_binary(MUCRoomJID), Limit, null, Config),
#{<<"stanzas">> := [], <<"limit">> := Limit} = get_ok_value([data, muc, getRoomMessages], Res3).

user_list_rooms(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}], fun user_list_rooms_story/2).

Expand Down Expand Up @@ -1441,7 +1473,7 @@ admin_get_room_config_story(Config, Alice) ->
MUCServer = ?config(muc_light_host, Config),
RoomName = <<"first room">>,
RoomSubject = <<"Room about nothing">>,
{ok, #{jid := #jid{luser = RoomID} = RoomJID}} =
{ok, #{jid := #jid{luser = _RoomID} = RoomJID}} =
create_room(MUCServer, RoomName, RoomSubject, AliceBin),
RoomJIDBin = jid:to_binary(RoomJID),
Res = get_room_config(RoomJIDBin, Config),
Expand Down Expand Up @@ -1825,3 +1857,18 @@ user_get_blocking(User, Config) ->
user_set_blocking(User, Items, Config) ->
Vars = #{<<"items">> => prepare_blocking_items_for_query(Items)},
execute_user_command(<<"muc_light">>, <<"setBlockingList">>, User, Vars, Config).

user_get_muc_room_messages(User, RoomJID, PageSize, Before, Config) ->
Vars = #{<<"room">> => jid:to_binary(RoomJID), <<"pageSize">> => PageSize,
<<"before">> => Before},
execute_user_command(<<"muc">>, <<"getRoomMessages">>, User, Vars, Config).

send_message_to_muc_room(User, Room, Body, Resource, Config) ->
Vars = #{room => jid:to_binary(Room), body => Body, resource => Resource},
execute_user_command(<<"muc">>, <<"sendMessageToRoom">>, User, Vars, Config).

enter_muc_room(RoomJID, User, Nick) ->
JID = jid:to_binary(jid:replace_resource(RoomJID, Nick)),
Pres = escalus_stanza:to(escalus_stanza:presence(<<"available">>, []), JID),
escalus:send(User, Pres),
escalus:wait_for_stanza(User).
9 changes: 5 additions & 4 deletions src/mam/mod_mam_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ disco_muc_features(Acc, _Params, _Extra) ->
{ok, Acc}.

%% @doc Handle public MUC-message.
-spec filter_room_packet(Acc, Params, Extra) -> {ok, Acc} when
Acc :: exml:element(),
Params :: mod_muc:room_event_data(),
-spec filter_room_packet(Packet, EventData, Extra) -> {ok, Packet} when
Packet :: exml:element(),
EventData :: mod_muc:room_event_data(),
Extra :: gen_hook:extra().
filter_room_packet(Packet, EventData, #{host_type := HostType}) ->
?LOG_DEBUG(#{what => mam_room_packet, text => <<"Incoming room packet">>,
Expand All @@ -178,13 +178,14 @@ archive_room_packet(HostType, Packet, FromNick, FromJID = #jid{},
ArcID = archive_id_int(HostType, RoomJID),
%% Occupant JID <room@service/nick>
SrcJID = jid:replace_resource(RoomJID, FromNick),
IsMamMucEnabled = mod_mam_utils:is_mam_muc_enabled(RoomJID#jid.lserver, HostType),
IsInteresting =
case get_behaviour(HostType, ArcID, RoomJID, SrcJID) of
always -> true;
never -> false;
roster -> true
end,
case IsInteresting of
case IsInteresting andalso IsMamMucEnabled of
true ->
MessID = mod_mam_utils:generate_message_id(TS),
Packet1 = mod_mam_utils:replace_x_user_element(FromJID, Role, Affiliation, Packet),
Expand Down
10 changes: 9 additions & 1 deletion src/mam/mod_mam_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
calculate_msg_id_borders/4,
maybe_encode_compact_uuid/2,
wait_shaper/4,
check_for_item_not_found/3]).
check_for_item_not_found/3,
is_mam_muc_enabled/2]).

%% Ejabberd
-export([send_message/4,
Expand Down Expand Up @@ -1246,3 +1247,10 @@ is_removing_done({updated, N}, Limit) when N < Limit ->
{done, N};
is_removing_done({updated, N}, _)->
{remove_more, N}.

-spec is_mam_muc_enabled(jid:lserver(), mongooseim:host_type()) -> boolean().
is_mam_muc_enabled(MucDomain, HostType) ->
HostPattern = mongoose_config:get_opt([{modules, HostType}, mod_mam_muc, host]),
{ok, #{subdomain_pattern := SubDomainPattern}} =
mongoose_domain_api:get_subdomain_info(MucDomain),
HostPattern =:= SubDomainPattern.