diff --git a/.circleci/template.yml b/.circleci/template.yml index 257a2cc105..bd6f1a58b6 100644 --- a/.circleci/template.yml +++ b/.circleci/template.yml @@ -178,6 +178,7 @@ executors: otp_25: docker: - image: *OTP25 + resource_class: medium+ otp_24_redis: docker: - image: *OTP24 diff --git a/big_tests/tests/graphql_muc_SUITE.erl b/big_tests/tests/graphql_muc_SUITE.erl index fd03d23441..59db3c9405 100644 --- a/big_tests/tests/graphql_muc_SUITE.erl +++ b/big_tests/tests/graphql_muc_SUITE.erl @@ -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]. @@ -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; @@ -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(). @@ -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). @@ -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). @@ -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}, @@ -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). diff --git a/big_tests/tests/graphql_muc_light_SUITE.erl b/big_tests/tests/graphql_muc_light_SUITE.erl index cd0df8df8e..ad1b22702e 100644 --- a/big_tests/tests/graphql_muc_light_SUITE.erl +++ b/big_tests/tests/graphql_muc_light_SUITE.erl @@ -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, @@ -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"} @@ -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(). @@ -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). @@ -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), @@ -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). diff --git a/src/mam/mod_mam_muc.erl b/src/mam/mod_mam_muc.erl index 34334e932b..f12aa58714 100644 --- a/src/mam/mod_mam_muc.erl +++ b/src/mam/mod_mam_muc.erl @@ -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">>, @@ -178,13 +178,14 @@ archive_room_packet(HostType, Packet, FromNick, FromJID = #jid{}, ArcID = archive_id_int(HostType, RoomJID), %% Occupant JID 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), diff --git a/src/mam/mod_mam_utils.erl b/src/mam/mod_mam_utils.erl index e7f4939280..31e7494f3d 100644 --- a/src/mam/mod_mam_utils.erl +++ b/src/mam/mod_mam_utils.erl @@ -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, @@ -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.