From 99389eefba1883753c15c6f411fb8543c93f58a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sat, 10 Feb 2018 17:36:33 +0100 Subject: Always return the oldest matching messages from MAM, even if no date is set --- src/bridge/bridge.cpp | 2 +- src/database/database.cpp | 25 ++++++++++++++++++++++++- src/database/database.hpp | 9 +++++++++ src/xmpp/biboumi_component.cpp | 2 +- 4 files changed, 35 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/bridge/bridge.cpp b/src/bridge/bridge.cpp index 54bee84..acb8e18 100644 --- a/src/bridge/bridge.cpp +++ b/src/bridge/bridge.cpp @@ -1020,7 +1020,7 @@ void Bridge::send_room_history(const std::string& hostname, std::string chan_nam auto limit = coptions.col(); if (history_limit.stanzas >= 0 && history_limit.stanzas < limit) limit = history_limit.stanzas; - const auto lines = Database::get_muc_logs(this->user_jid, chan_name, hostname, limit, history_limit.since); + const auto lines = Database::get_muc_most_recent_logs(this->user_jid, chan_name, hostname, limit, history_limit.since); chan_name.append(utils::empty_if_fixed_server("%" + hostname)); for (const auto& line: lines) { diff --git a/src/database/database.cpp b/src/database/database.cpp index 3622963..c43ace4 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -185,13 +185,36 @@ std::vector Database::get_muc_logs(const std::string& owne request << " and " << Database::Date{} << "<=" << end_time; } + if (limit >= 0) + request.limit() << limit; + + auto result = request.execute(*Database::db); + + return {result.cbegin(), result.cend()}; +} + +std::vector Database::get_muc_most_recent_logs(const std::string& owner, const std::string& chan_name, const std::string& server, + int limit, const std::string& start) +{ + auto request = Database::muc_log_lines.select(); + request.where() << Database::Owner{} << "=" << owner << \ + " and " << Database::IrcChanName{} << "=" << chan_name << \ + " and " << Database::IrcServerName{} << "=" << server; + + if (!start.empty()) + { + const auto start_time = utils::parse_datetime(start); + if (start_time != -1) + request << " and " << Database::Date{} << ">=" << start_time; + } + request.order_by() << Id{} << " DESC "; if (limit >= 0) request.limit() << limit; auto result = request.execute(*Database::db); - + return {result.crbegin(), result.crend()}; } diff --git a/src/database/database.hpp b/src/database/database.hpp index ec44543..f9aed2b 100644 --- a/src/database/database.hpp +++ b/src/database/database.hpp @@ -118,8 +118,17 @@ class Database static IrcChannelOptions get_irc_channel_options_with_server_and_global_default(const std::string& owner, const std::string& server, const std::string& channel); + /** + * Get all the lines between (optional) start and end dates, with a (optional) limit. + */ static std::vector get_muc_logs(const std::string& owner, const std::string& chan_name, const std::string& server, int limit=-1, const std::string& start="", const std::string& end=""); + + /** + * Get the most recent messages from the archive, with optional limit and start date + */ + static std::vector get_muc_most_recent_logs(const std::string& owner, const std::string& chan_name, const std::string& server, + int limit=-1, const std::string& start=""); static std::string store_muc_message(const std::string& owner, const std::string& chan_name, const std::string& server_name, time_point date, const std::string& body, const std::string& nick); diff --git a/src/xmpp/biboumi_component.cpp b/src/xmpp/biboumi_component.cpp index 481ebb9..250007e 100644 --- a/src/xmpp/biboumi_component.cpp +++ b/src/xmpp/biboumi_component.cpp @@ -721,7 +721,7 @@ bool BiboumiComponent::handle_mam_request(const Stanza& stanza) if (max) limit = std::atoi(max->get_inner().data()); } - // Do send more than 100 messages, even if the client asked for more, + // Do not send more than 100 messages, even if the client asked for more, // or if it didn’t specify any limit. // 101 is just a trick to know if there are more available messages. // If our query returns 101 message, we know it’s incomplete, but we -- cgit v1.2.3