- msgList = Core::requestMsgs(coreSession()->user(), bufferId, -1, last, additional);
- msgIter = msgList.constBegin();
- 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 seemlessly
+ // 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);
+ });
+ }