diff --git a/src/mod_mam_sql.erl b/src/mod_mam_sql.erl index 695542387..a0dd6b6d1 100644 --- a/src/mod_mam_sql.erl +++ b/src/mod_mam_sql.erl @@ -441,7 +441,7 @@ make_sql_query(User, LServer, MAMQuery, RSM, ExtraUsernames) -> true -> [] end, - SubOrderClause = if LimitClause /= [], TopClause /= [] -> + SubOrderClause = if LimitClause /= []; TopClause /= [] -> <<" ORDER BY timestamp DESC ">>; true -> [] diff --git a/test/mam_tests.erl b/test/mam_tests.erl index 30b0179a6..ed2fecae9 100644 --- a/test/mam_tests.erl +++ b/test/mam_tests.erl @@ -642,7 +642,8 @@ query_rsm_after(Config, From, To, NS) -> query_rsm_before(Config, From, To) -> lists:foreach( fun(NS) -> - query_rsm_before(Config, From, To, NS) + query_rsm_before(Config, From, To, NS), + query_last_message(Config, From, To, NS) end, ?VERSIONS). query_rsm_before(Config, From, To, NS) -> @@ -661,6 +662,16 @@ query_rsm_before(Config, From, To, NS) -> Last end, <<"">>, lists:reverse([lists:seq(1, N) || N <- lists:seq(0, 5)])). +query_last_message(Config, From, To, NS) -> + ct:comment("Retrieving last message", []), + QID = p1_rand:get_string(), + Query = #mam_query{xmlns = NS, id = QID, + rsm = #rsm_set{before = <<>>, max = 1}}, + ID = send_query(Config, Query), + recv_archived_messages(Config, From, To, QID, [5]), + RSM = ?match(#rsm_set{} = RSM, recv_fin(Config, ID, QID, NS, false), RSM), + match_rsm_count(RSM, 5). + match_rsm_count(#rsm_set{count = undefined}, _) -> %% The backend doesn't support counting ok;