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

Fix message timestamp=0 when archiving MUC legacy messages #4294

Merged
merged 4 commits into from
Jun 5, 2024
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
63 changes: 62 additions & 1 deletion big_tests/tests/mam_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ basic_groups() ->
[{muc04, [parallel], muc_cases() ++ muc_text_search_cases()},
{muc06, [parallel], muc_cases() ++ muc_stanzaid_cases() ++ muc_retract_cases()
++ muc_metadata_cases() ++ muc_fetch_specific_msgs_cases()},
{muc_seq, [], [muc_validate_mam_id]},
{muc_configurable_archiveid, [], muc_configurable_archiveid_cases()},
{muc_rsm_all, [parallel],
[{muc_rsm04, [parallel], muc_rsm_cases()}]}]},
Expand Down Expand Up @@ -492,6 +493,7 @@ suite() ->

init_per_suite(Config) ->
muc_helper:load_muc(),
mongoose_helper:inject_module(mim(), ?MODULE, reload),
mam_helper:prepare_for_suite(
increase_limits(
delete_users([{escalus_user_db, {module, escalus_ejabberd}}
Expand Down Expand Up @@ -552,6 +554,8 @@ init_per_group(muc04, Config) ->
[{props, mam04_props()}, {with_rsm, true}|Config];
init_per_group(muc06, Config) ->
[{props, mam06_props()}, {with_rsm, true}|Config];
init_per_group(muc_seq, Config) ->
[{props, mam04_props()}, {with_rsm, true}|Config];

init_per_group(muc_configurable_archiveid, Config) ->
dynamic_modules:save_modules(host_type(), Config);
Expand Down Expand Up @@ -590,7 +594,7 @@ do_init_per_group(C, ConfigIn) ->
end_per_group(G, Config) when G == rsm_all; G == nostore;
G == mam04; G == rsm04; G == with_rsm04; G == muc04; G == muc_rsm04; G == rsm04_comp;
G == muc06; G == mam06;
G == archived; G == mam_metrics ->
G == archived; G == mam_metrics; G == muc_seq ->
Config;
end_per_group(muc_configurable_archiveid, Config) ->
dynamic_modules:restore_modules(Config),
Expand Down Expand Up @@ -835,6 +839,9 @@ init_metrics(muc_archive_request, Config) ->
init_metrics(_CaseName, Config) ->
Config.

end_per_testcase(CaseName = muc_validate_mam_id, Config) ->
unmock_mongoose_mam_id(mim()),
escalus:end_per_testcase(CaseName, Config);
end_per_testcase(CaseName, Config) ->
maybe_destroy_room(CaseName, Config),
escalus:end_per_testcase(CaseName, Config).
Expand Down Expand Up @@ -2194,6 +2201,47 @@ muc_archive_request(Config) ->
end,
escalus:story(Config, [{alice, 1}, {bob, 1}], F).

muc_validate_mam_id(Config0) ->
P = ?config(props, Config0),
F = fun(Config, Alice, Bob) ->
mock_mongoose_mam_id(mim()),
StartTS = erlang:system_time(microsecond),
Room = ?config(room, Config),
RoomAddr = room_address(Room),
escalus:send(Alice, stanza_muc_enter_room(Room, nick(Alice))),
escalus:send(Bob, stanza_muc_enter_room(Room, nick(Bob))),

%% Bob received presences.
escalus:wait_for_stanzas(Bob, 2),

%% Bob received the room's subject.
escalus:wait_for_stanzas(Bob, 1),

%% Alice sends to the chat room.
escalus:send(Alice, escalus_stanza:groupchat_to(RoomAddr, <<"Hi">>)),

%% Bob received the message "Hi".
%% This message will be archived (by alicesroom@localhost).
escalus:wait_for_stanza(Bob),
mam_helper:wait_for_room_archive_size(muc_host(), Room, 1),

%% Bob requests the room's archive.
escalus:send(Bob, stanza_to_room(stanza_archive_request(P, <<"q1">>), Room)),
[ArcMsg] = respond_messages(assert_respond_size(1, wait_archive_respond(Bob))),
#forwarded_message{result_id=ArcId} = parse_forwarded_message(ArcMsg),

%% Check that timestamp is greater than the initial one
MessId = rpc_apply(mod_mam_utils, external_binary_to_mess_id, [ArcId]),
{ArcTS, _} = rpc_apply(mod_mam_utils, decode_compact_uuid, [MessId]),
case ArcTS < StartTS of
true ->
ct:fail({bad_mam_id, ArcId, ArcTS, StartTS, MessId});
false ->
ok
end
end,
muc_helper:story_with_room(Config0, [{persistent, true}], [{alice, 1}, {bob, 1}], F).

muc_multiple_devices(Config) ->
P = ?config(props, Config),
F = fun(Alice1, Alice2, Bob) ->
Expand Down Expand Up @@ -3624,3 +3672,16 @@ assert_failed_to_decode_message(ArcMsg) ->
attrs = [{<<"code">>, <<"500">>}, {<<"type">>,<<"wait">>}],
children = [#xmlel{name = <<"internal-server-error">>},
#xmlel{name = <<"text">>, children = [#xmlcdata{content = Err}]}]}, Msg).

mock_mongoose_mam_id(Node) ->
ok = rpc(Node, meck, new, [mongoose_mam_id, [passthrough, no_link]]),
ok = rpc(Node, meck, expect, [mongoose_mam_id, next_unique, 1,
fun(X) ->
P = persistent_term:get(mock_mongoose_mam_id, 0),
N = max(X, P + 1),
persistent_term:put(mock_mongoose_mam_id, N),
N
end]).

unmock_mongoose_mam_id(Node) ->
ok = rpc(Node, meck, unload, [mongoose_mam_id]).
2 changes: 1 addition & 1 deletion src/muc/mod_muc_room.erl
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ can_send_broadcasts(Role, StateData) ->

broadcast_room_packet(From, FromNick, Role, Packet, StateData) ->
Activity = get_user_activity(From, StateData),
TS = Activity#activity.message_time,
TS = erlang:system_time(microsecond),
Affiliation = get_affiliation(From, StateData),
EventData = #{from_nick => FromNick, from_jid => From,
room_jid => StateData#state.jid, role => Role,
Expand Down