X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fcorebacklogmanager.cpp;h=6a61ba66be3396538dbdf0196ad7b16b01c3b4f4;hp=c7bc8e72cfd45d479bdfcb0b00c1144a82ee0ca8;hb=HEAD;hpb=26b9300ccab24e526a9f43bef95a2a70f59161df diff --git a/src/core/corebacklogmanager.cpp b/src/core/corebacklogmanager.cpp index c7bc8e72..d26d2787 100644 --- a/src/core/corebacklogmanager.cpp +++ b/src/core/corebacklogmanager.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel IRC Team * + * Copyright (C) 2005-2022 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,31 +15,170 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ #include "corebacklogmanager.h" + +#include +#include + +#include + #include "core.h" #include "coresession.h" -#include +CoreBacklogManager::CoreBacklogManager(CoreSession* coreSession) + : BacklogManager(coreSession) + , _coreSession(coreSession) +{} -CoreBacklogManager::CoreBacklogManager(CoreSession *coreSession) - : BacklogManager(coreSession), - _coreSession(coreSession) +QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional) { + QVariantList backlog; + auto msgList = Core::requestMsgs(coreSession()->user(), bufferId, first, last, limit); + + std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) { + return QVariant::fromValue(msg); + }); + + if (additional && limit != 0) { + MsgId oldestMessage = first; + if (!msgList.empty()) { + if (msgList.front().msgId() < msgList.back().msgId()) + oldestMessage = msgList.front().msgId(); + else + oldestMessage = msgList.back().msgId(); + } + + if (first != -1) { + last = first; + } + else { + last = oldestMessage; + } + + // only fetch additional messages if they continue seamlessly + // that is, if the list of messages is not truncated by the limit + if (last == oldestMessage) { + msgList = Core::requestMsgs(coreSession()->user(), bufferId, -1, last, additional); + std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) { + return QVariant::fromValue(msg); + }); + } + } + + return backlog; } -QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, int limit, int offset) { - QVariantList backlog; - QList msgList; - msgList = Core::requestMsgs(coreSession()->user(), bufferId, limit, offset); - - QList::const_iterator msgIter = msgList.constBegin(); - QList::const_iterator msgListEnd = msgList.constEnd(); - while(msgIter != msgListEnd) { - backlog << qVariantFromValue(*msgIter); - msgIter++; - } - return backlog; +QVariantList CoreBacklogManager::requestBacklogFiltered(BufferId bufferId, MsgId first, MsgId last, int limit, int additional, int type, int flags) +{ + QVariantList backlog; + auto msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, first, last, limit, Message::Types{type}, Message::Flags{flags}); + + std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) { + return QVariant::fromValue(msg); + }); + + if (additional && limit != 0) { + MsgId oldestMessage = first; + if (!msgList.empty()) { + if (msgList.front().msgId() < msgList.back().msgId()) + oldestMessage = msgList.front().msgId(); + else + oldestMessage = msgList.back().msgId(); + } + + if (first != -1) { + last = first; + } + else { + last = oldestMessage; + } + + // only fetch additional messages if they continue seamlessly + // that is, if the list of messages is not truncated by the limit + if (last == oldestMessage) { + msgList = Core::requestMsgsFiltered(coreSession()->user(), bufferId, -1, last, additional, Message::Types{type}, Message::Flags{flags}); + std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) { + return QVariant::fromValue(msg); + }); + } + } + + return backlog; +} + +QVariantList CoreBacklogManager::requestBacklogForward(BufferId bufferId, MsgId first, MsgId last, int limit, int type, int flags) +{ + QVariantList backlog; + auto msgList = Core::requestMsgsForward(coreSession()->user(), bufferId, first, last, limit, Message::Types{type}, Message::Flags{flags}); + + std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) { + return QVariant::fromValue(msg); + }); + + return backlog; +} + +QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional) +{ + QVariantList backlog; + auto msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit); + + std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) { + return QVariant::fromValue(msg); + }); + + if (additional) { + if (first != -1) { + last = first; + } + else { + last = -1; + if (!msgList.empty()) { + if (msgList.front().msgId() < msgList.back().msgId()) + last = msgList.front().msgId(); + else + last = msgList.back().msgId(); + } + } + msgList = Core::requestAllMsgs(coreSession()->user(), -1, last, additional); + std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) { + return QVariant::fromValue(msg); + }); + } + + return backlog; +} + +QVariantList CoreBacklogManager::requestBacklogAllFiltered(MsgId first, MsgId last, int limit, int additional, int type, int flags) +{ + QVariantList backlog; + auto msgList = Core::requestAllMsgsFiltered(coreSession()->user(), first, last, limit, Message::Types{type}, Message::Flags{flags}); + + std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) { + return QVariant::fromValue(msg); + }); + + if (additional) { + if (first != -1) { + last = first; + } + else { + last = -1; + if (!msgList.empty()) { + if (msgList.front().msgId() < msgList.back().msgId()) + last = msgList.front().msgId(); + else + last = msgList.back().msgId(); + } + } + msgList = Core::requestAllMsgsFiltered(coreSession()->user(), -1, last, additional, Message::Types{type}, Message::Flags{flags}); + std::transform(msgList.cbegin(), msgList.cend(), std::back_inserter(backlog), [](auto&& msg) { + return QVariant::fromValue(msg); + }); + } + + return backlog; }