Skip to content

Commit 685eea7

Browse files
authored
Merge pull request #4471 from esl/support-old-exml-format
Support old exml format
2 parents 1428ceb + 9b21949 commit 685eea7

File tree

6 files changed

+162
-169
lines changed

6 files changed

+162
-169
lines changed

big_tests/tests/mam_SUITE.erl

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ rdbms_configs(_, _) ->
183183
[].
184184

185185
cassandra_configs(true) ->
186-
[cassandra];
186+
[cassandra,
187+
cassandra_eterm];
187188
cassandra_configs(_) ->
188189
[].
189190

@@ -309,6 +310,7 @@ mam_cases() ->
309310
mam_service_discovery_to_different_client_bare_jid_results_in_error,
310311
archive_is_instrumented,
311312
easy_archive_request,
313+
easy_archive_request_old_xmlel_format,
312314
easy_archive_request_for_the_receiver,
313315
message_sent_to_yourself,
314316
range_archive_request,
@@ -637,7 +639,7 @@ init_per_group(Group, ConfigIn) ->
637639
C = configuration(Group),
638640
B = basic_group(Group),
639641
{ModulesToStart, Config0} = required_modules_for_group(C, B, ConfigIn),
640-
ct:pal("Init per group ~p; configuration ~p; basic group ~p", [Group, C, B]),
642+
ct:log("Init per group ~p; configuration ~p; basic group ~p", [Group, C, B]),
641643
Config01 = dynamic_modules:save_modules(host_type(), Config0),
642644
dynamic_modules:ensure_modules(host_type(), ModulesToStart),
643645
Config1 = do_init_per_group(C, Config01),
@@ -648,6 +650,8 @@ do_init_per_group(C, ConfigIn) ->
648650
case C of
649651
cassandra ->
650652
[{archive_wait, 1500} | Config0];
653+
cassandra_eterm ->
654+
[{archive_wait, 1500} | Config0];
651655
elasticsearch ->
652656
[{archive_wait, 2500} | Config0];
653657
_ ->
@@ -657,7 +661,8 @@ do_init_per_group(C, ConfigIn) ->
657661
setup_meck(_, elasticsearch) ->
658662
ok = rpc(mim(), meck, expect,
659663
[mongoose_elasticsearch, insert_document, 4, {error, simulated}]);
660-
setup_meck(_, cassandra) ->
664+
setup_meck(_, Config) when Config =:= cassandra_eterm;
665+
Config =:= cassandra ->
661666
ok = rpc(mim(), meck, expect,
662667
[mongoose_cassandra, cql_write_async, 5, {error, simulated}]);
663668
setup_meck(drop_msg, Config) when Config =:= rdbms_async_pool;
@@ -753,21 +758,15 @@ maybe_set_wait(C, Types, Config) when C =:= rdbms_async_pool;
753758
maybe_set_wait(_C, _, Config) ->
754759
Config.
755760

756-
mam_prefs_backend_module(rdbms, rdbms) ->
757-
mod_mam_rdbms_prefs;
758-
mam_prefs_backend_module(cassandra, cassandra) ->
759-
mod_mam_cassandra_prefs;
760-
mam_prefs_backend_module(_, mnesia) ->
761-
mod_mam_mnesia_prefs;
762-
mam_prefs_backend_module(_, _) ->
763-
{error, wrong_db}.
764-
765761
mam_opts_for_conf(elasticsearch) ->
766762
#{backend => elasticsearch,
767763
user_prefs_store => mnesia};
768764
mam_opts_for_conf(cassandra) ->
769765
#{backend => cassandra,
770766
user_prefs_store => cassandra};
767+
mam_opts_for_conf(cassandra_eterm) ->
768+
Opts = mam_opts_for_conf(cassandra),
769+
Opts#{db_message_format => mam_message_eterm};
771770
mam_opts_for_conf(rdbms_easy) ->
772771
EasyOpts = #{db_jid_format => mam_jid_rfc,
773772
db_message_format => mam_message_xml},
@@ -881,24 +880,37 @@ maybe_skip(C, Config) when C =:= muc_light_failed_to_decode_message_in_database;
881880
"elasticsearch does not support encodings");
882881
maybe_skip(C, Config) when C =:= muc_light_sql_query_failed;
883882
C =:= pm_sql_query_failed ->
884-
skip_if(?config(configuration, Config) =:= elasticsearch orelse
885-
?config(configuration, Config) =:= cassandra,
883+
Configuration = ?config(configuration, Config),
884+
skip_if(lists:member(Configuration, [elasticsearch, cassandra, cassandra_eterm]),
886885
"Not an SQL database");
887886
maybe_skip(C, Config) when C =:= muc_light_include_groupchat_filter;
888887
C =:= muc_light_no_pm_stored_include_groupchat_filter;
889888
C =:= muc_light_include_groupchat_messages_by_default ->
890-
skip_if(?config(configuration, Config) =:= cassandra,
889+
Configuration = ?config(configuration, Config),
890+
skip_if(lists:member(Configuration, [cassandra, cassandra_eterm]),
891891
"include_groupchat field is not supported for cassandra backend");
892892
maybe_skip(C, Config) when C =:= easy_text_search_request;
893893
C =:= long_text_search_request;
894894
C =:= save_unicode_messages;
895895
C =:= muc_text_search_request ->
896-
skip_if(?config(configuration, Config) =:= cassandra,
896+
Configuration = ?config(configuration, Config),
897+
skip_if(lists:member(Configuration, [cassandra, cassandra_eterm]),
897898
"full text search is not implemented for cassandra backend");
898899
maybe_skip(C, Config) when C =:= muc_light_async_pools_batch_flush;
899900
C =:= async_pools_batch_flush ->
900901
skip_if(?config(configuration, Config) =/= rdbms_async_pool,
901902
"only for async pool");
903+
maybe_skip(C, Config) when C =:= easy_archive_request_old_xmlel_format ->
904+
MamOpts = ?config(mam_meta_opts, Config),
905+
MamBackend = maps:get(backend, MamOpts, rdbms),
906+
DefaultFormat = case MamBackend of
907+
rdbms -> mam_message_compressed_eterm;
908+
_ -> mam_message_xml
909+
end,
910+
MessageFormat = maps:get(db_message_format, MamOpts, DefaultFormat),
911+
PmMamOpts = maps:get(pm, MamOpts, #{}),
912+
PmMessageFormat = maps:get(db_message_format, PmMamOpts, MessageFormat),
913+
skip_if(PmMessageFormat =:= mam_message_xml, "run only for eterm PM message format");
902914
maybe_skip(_C, _Config) ->
903915
ok.
904916

@@ -1238,13 +1250,6 @@ message_dropped(Config) ->
12381250
easy_archive_request(Config) ->
12391251
P = ?config(props, Config),
12401252
F = fun(Alice, Bob) ->
1241-
%% Alice sends "OH, HAI!" to Bob
1242-
%% {xmlel,<<"message">>,
1243-
%% [{<<"from">>,<<"alice@localhost/res1">>},
1244-
%% {<<"to">>,<<"bob@localhost/res1">>},
1245-
%% {<<"xml:lang">>,<<"en">>},
1246-
%% {<<"type">>,<<"chat">>}],
1247-
%% [{xmlel,<<"body">>,[],[{xmlcdata,<<"OH, HAI!">>}]}]}
12481253
escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"OH, HAI!">>)),
12491254
mam_helper:wait_for_archive_size(Alice, 1),
12501255
escalus:send(Alice, stanza_archive_request(P, <<"q1">>)),
@@ -1271,6 +1276,49 @@ easy_archive_request_for_the_receiver(Config) ->
12711276
end,
12721277
escalus_fresh:story(Config, [{alice, 1}, {bob, 1}], F).
12731278

1279+
easy_archive_request_old_xmlel_format(Config) ->
1280+
P = ?config(props, Config),
1281+
F = fun(Alice, Bob) ->
1282+
AArcId = rest_helper:make_arc_id(Alice),
1283+
{BobJid, _, _} = BArcId = rest_helper:make_arc_id(Bob),
1284+
DateTime = calendar:local_time(),
1285+
Msg = mam_helper:generate_msg_for_date_user(AArcId, BArcId, DateTime, <<"OH, HAI!">>),
1286+
Packet = erlang:element(5, Msg),
1287+
OldFormatPacket =
1288+
{xmlel,<<"message">>,
1289+
[{<<"to">>, BobJid}, {<<"type">>,<<"chat">>}],
1290+
[{xmlel,<<"body">>,[],[{xmlcdata,<<"OH, HAI!">>}]}]},
1291+
1292+
Msg1 = erlang:setelement(5, Msg, OldFormatPacket),
1293+
ct:log("Packet: ~p~n", [Packet]),
1294+
ct:log("OldFormatPacket: ~p~n", [OldFormatPacket]),
1295+
mam_helper:put_msg(Msg1),
1296+
mam_helper:wait_for_archive_size(Alice, 1),
1297+
escalus:send(Alice, stanza_archive_request(P, <<"q1">>)),
1298+
Res = wait_archive_respond(Alice),
1299+
assert_lookup_event(mod_mam_pm_lookup, escalus_utils:get_jid(Alice)),
1300+
assert_respond_size(1, Res),
1301+
assert_respond_query_id(P, <<"q1">>, parse_result_iq(Res)),
1302+
[RespMessage] = respond_messages(Res),
1303+
ct:log("ResPacket: ~p~n", [RespMessage]),
1304+
1305+
ArchivedMsg = exml_query:path(RespMessage, [{element, <<"result">>},
1306+
{element, <<"forwarded">>},
1307+
{element, <<"message">>}]),
1308+
ct:log("ArchivedMsg: ~p~n", [ArchivedMsg]),
1309+
assert_msg_match(Packet, ArchivedMsg),
1310+
ok
1311+
end,
1312+
escalus_fresh:story(Config, [{alice, 1}, {bob, 1}], F).
1313+
1314+
assert_msg_match(Pattern, Msg) ->
1315+
#xmlel{attrs = PatternAttrs, children = PatternChildren} = Pattern,
1316+
#xmlel{attrs = MsgAttrs, children = MsgChildren} = Msg,
1317+
[?assertEqual(Value, maps:get(Name, MsgAttrs, undefined),
1318+
<<"attribute ", Name/binary>>)
1319+
|| Name := Value <- PatternAttrs],
1320+
?assertEqual(PatternChildren, MsgChildren).
1321+
12741322
message_sent_to_yourself(Config) ->
12751323
P = ?config(props, Config),
12761324
F = fun(Alice) ->
@@ -4106,7 +4154,8 @@ message_retraction_is_enabled(Config) ->
41064154
BasicGroup = ?config(basic_group, Config),
41074155
BasicGroup =/= disabled_retraction andalso BasicGroup =/= muc_disabled_retraction.
41084156

4109-
check_include_groupchat_features(Stanza, cassandra, _BasicGroup) ->
4157+
check_include_groupchat_features(Stanza, Config, _BasicGroup) when Config =:= cassandra_eterm;
4158+
Config =:= cassandra ->
41104159
?assertNot(escalus_pred:has_feature(groupchat_field_ns(), Stanza)),
41114160
?assertNot(escalus_pred:has_feature(groupchat_available_ns(), Stanza));
41124161
check_include_groupchat_features(Stanza, _Configuration, muc_light) ->

big_tests/tests/mam_helper.erl

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -541,30 +541,6 @@ get_received_msgs_ids(Response) ->
541541
Parsed#forwarded_message.result_id
542542
end, Msgs).
543543

544-
%% @doc Result query iq.
545-
%%
546-
%% [{xmlel,<<"iq">>,
547-
%% [{<<"from">>,<<"alice@localhost">>},
548-
%% {<<"to">>,<<"alice@localhost/res1">>},
549-
%% {<<"id">>,<<"387862024ce65379b049e19751e4309e">>},
550-
%% {<<"type">>,<<"result">>}],
551-
%% []}]
552-
%%
553-
%%
554-
%% [{xmlel,<<"iq">>,
555-
%% [{<<"from">>,<<"alice@localhost">>},
556-
%% {<<"to">>,<<"alice@localhost/res1">>},
557-
%% {<<"id">>,<<"c256a18c4b720465e215a81362d41eb7">>},
558-
%% {<<"type">>,<<"result">>}],
559-
%% [{xmlel,<<"query">>,
560-
%% [{<<"xmlns">>,<<"urn:xmpp:mam:tmp">>}],
561-
%% [{xmlel,<<"set">>,
562-
%% [{<<"xmlns">>,<<"http://jabber.org/protocol/rsm">>}],
563-
%% [{xmlel,<<"first">>,
564-
%% [{<<"index">>,<<"10">>}],
565-
%% [{xmlcdata,<<"103439">>}]},
566-
%% {xmlel,<<"last">>,[],[{xmlcdata,<<"103447">>}]},
567-
%% {xmlel,<<"count">>,[],[{xmlcdata,<<"15">>}]}]}]}]}]
568544
parse_result_iq(#mam_archive_respond{respond_iq = IQ, respond_fin = undefined}) ->
569545
Fin = exml_query:subelement(IQ, <<"fin">>),
570546
Set = exml_query:subelement(Fin, <<"set">>),

big_tests/tests/muc_SUITE.erl

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5374,30 +5374,6 @@ has_muc(#xmlel{children = [ #xmlel{children = Services} ]}) ->
53745374
%% </query>
53755375
%% </iq>
53765376

5377-
%% is like this:
5378-
%% {xmlel,<<"iq">>,
5379-
%% [{<<"from">>,<<"localhost">>},
5380-
%% {<<"to">>,<<"alice@localhost/res1">>},
5381-
%% {<<"id">>,<<"a5eb1dc70826598893b15f1936b18a34">>},
5382-
%% {<<"type">>,<<"result">>}],
5383-
%% [{xmlel,<<"query">>,
5384-
%% [{<<"xmlns">>,
5385-
%% <<"http://jabber.org/protocol/disco#items">>}],
5386-
%% [{xmlel,<<"item">>,
5387-
%% [{<<"jid">>,<<"vjud.localhost">>}],
5388-
%% []},
5389-
%% {xmlel,<<"item">>,
5390-
%% [{<<"jid">>,<<"pubsub.localhost">>}],
5391-
%% []},
5392-
%% {xmlel,<<"item">>,
5393-
%% [{<<"jid">>,<<"muc.localhost">>}],
5394-
%% []},
5395-
%% {xmlel,<<"item">>,
5396-
%% [{<<"jid">>,<<"irc.localhost">>}],
5397-
%% []}]}]}
5398-
%% how to obtaing output like the above? simply put this in the test case:
5399-
%% S = escalus:wait_for_stanza(Alice),
5400-
%% error_logger:info_msg("~p~n", [S]),
54015377
IsMUC = fun(Item) ->
54025378
exml_query:attr(Item, <<"jid">>) == muc_host()
54035379
end,

src/mam/mam_message_compressed_eterm.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ encode(Packet) ->
99
term_to_binary(Packet, [compressed]).
1010

1111
decode(Bin) ->
12-
binary_to_term(Bin).
12+
mam_message_eterm:decode(Bin).

src/mam/mam_message_eterm.erl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,30 @@
55

66
-behaviour(mam_message).
77

8+
-include_lib("exml/include/exml.hrl").
9+
10+
-type old_xmlcdata_format() :: {xmlcdata, Content :: binary()}.
11+
-type old_xmlel_format() :: {xmlel, Name :: binary(), Attrs :: [{binary(),binary()}],
12+
Children :: [old_xmlel_format() | old_xmlcdata_format()]}.
813
encode(Packet) ->
914
term_to_binary(Packet).
1015

1116
decode(Bin) ->
12-
binary_to_term(Bin).
17+
Term = binary_to_term(Bin),
18+
maybe_convert_old_exml_format(Term).
19+
20+
-spec maybe_convert_old_exml_format(exml:element() | old_xmlel_format()) ->
21+
exml:element().
22+
maybe_convert_old_exml_format({xmlel, _, Attrs, _} = XmlEl) when is_list(Attrs) ->
23+
convert_old_exml_format(XmlEl);
24+
maybe_convert_old_exml_format(XmlEl) ->
25+
XmlEl.
26+
27+
-spec convert_old_exml_format(old_xmlcdata_format()) -> exml:cdata();
28+
(old_xmlel_format()) -> exml:element().
29+
convert_old_exml_format({xmlel, Name, Attrs, Children}) ->
30+
NewAttrs = maps:from_list(Attrs),
31+
NewChildren = [convert_old_exml_format(C) || C <- Children],
32+
#xmlel{name = Name, attrs = NewAttrs, children = NewChildren};
33+
convert_old_exml_format({xmlcdata, Content}) ->
34+
#xmlcdata{content = Content}.

0 commit comments

Comments
 (0)