Verified Commit fca12719 authored by LNJ's avatar LNJ 💬

add mam support

parent df891fd8
Pipeline #4694 passed with stages
in 30 minutes and 49 seconds
......@@ -61,7 +61,8 @@ ClientWorker::ClientWorker(Caches *caches, Kaidan *kaidan, bool enableLogging, Q
registrationManager = new RegistrationManager(kaidan, caches->settings);
rosterManager = new RosterManager(kaidan, client, caches->rosterModel,
caches->avatarStorage, vCardManager, this);
msgHandler = new MessageHandler(kaidan, client, caches->msgModel, this);
msgHandler = new MessageHandler(kaidan, client, caches->msgModel,
caches->settings, this);
discoManager = new DiscoveryManager(client, this);
uploadManager = new UploadManager(kaidan, client, caches->msgModel, rosterManager,
caches->transferCache, this);
......
......@@ -48,8 +48,9 @@
#include "Notifications.h"
MessageHandler::MessageHandler(Kaidan *kaidan, QXmppClient *client, MessageModel *model,
QObject *parent)
: QObject(parent), kaidan(kaidan), client(client), model(model)
QSettings *settings, QObject *parent)
: QObject(parent), kaidan(kaidan), client(client), model(model),
settings(settings)
{
connect(client, &QXmppClient::messageReceived, this, &MessageHandler::handleMessage);
connect(kaidan, &Kaidan::sendMessage, this, &MessageHandler::sendMessage);
......@@ -79,6 +80,16 @@ MessageHandler::MessageHandler(Kaidan *kaidan, QXmppClient *client, MessageModel
connect(discoManager, &QXmppDiscoveryManager::infoReceived,
this, &MessageHandler::handleDiscoInfo);
// MAM manager
mamManager = new QXmppMamManager;
client->addExtension(mamManager);
connect(mamManager, &QXmppMamManager::archivedMessageReceived,
this, &MessageHandler::handleArchiveMessage);
connect(mamManager, &QXmppMamManager::resultsRecieved,
this, &MessageHandler::handleArchiveResults);
connect(&client->rosterManager(), &QXmppRosterManager::rosterReceived,
this, &MessageHandler::requestLatestMessages);
#endif
}
......@@ -86,6 +97,7 @@ MessageHandler::~MessageHandler()
{
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
delete carbonManager;
delete mamManager;
#endif
}
......@@ -146,6 +158,12 @@ void MessageHandler::handleMessage(const QXmppMessage &msg)
? QDateTime::currentDateTimeUtc()
: msg.stamp().toUTC());
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
// if the received message is newer than the currently latest message, update
if (settings->value("cache/latest-message").toDateTime() < msg.stamp())
settings->setValue("cache/latest-message", msg.stamp());
#endif
// save the message to the database
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
// in case of message correction, replace old message
......@@ -237,6 +255,10 @@ void MessageHandler::sendMessage(const QString& toJid,
else
emit kaidan->passiveNotificationRequested(tr("Message could not be sent."));
// TODO: handle error
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
settings->setValue("cache/latest-message", msg.stamp());
#endif
}
void MessageHandler::correctMessage(const QString& toJid,
......@@ -289,3 +311,56 @@ void MessageHandler::handleDiscoInfo(const QXmppDiscoveryIq &info)
carbonManager->setCarbonsEnabled(true);
#endif
}
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
void MessageHandler::handleArchiveMessage(const QString&,
const QXmppMessage &message)
{
// emit client->messageReceived(message);
handleMessage(message);
}
void MessageHandler::handleArchiveResults(const QString &queryId,
const QXmppResultSetReply &resultSetReply,
bool complete)
{
QDateTime latestMessage = settings->value("cache/latest-message").toDateTime();
if (!jidsToQuery.isEmpty()) {
QTimer::singleShot(500, this, [this]() {
QXmppResultSetQuery queryLimit;
queryLimit.setBefore(""); // query last (newest) first
queryLimit.setMax(10);
QString jid = jidsToQuery.takeFirst();
mamManager->retrieveArchivedMessages(QString(), QString(), jid,
QDateTime(),
QDateTime::currentDateTimeUtc(),
queryLimit);
});
}
// if (resultSetReply.count() > 0)
// mamManager->retrieveArchivedMessages("", "", "", latestMessage, QDateTime({2100, 01, 01}, {00, 00}));
}
void MessageHandler::requestLatestMessages()
{
QDateTime latestMessage = settings->value("cache/latest-message").toDateTime();
if (latestMessage == QDateTime()) {
qDebug() << "[MessageHandler] Loading latest 20 messages for each "
"contact.";
QXmppResultSetQuery queryLimit;
queryLimit.setMax(10);
queryLimit.setBefore(""); // query last (newest) first
jidsToQuery = client->rosterManager().getRosterBareJids();
QString jid = jidsToQuery.takeFirst();
mamManager->retrieveArchivedMessages(QString(), QString(), jid,
QDateTime(),
QDateTime::currentDateTimeUtc(),
queryLimit);
} else {
//mamManager->retrieveArchivedMessages("", "", "", latestMessage, QDateTime({2100, 01, 01}, {00, 00}));
}
}
#endif
......@@ -36,11 +36,15 @@
// QXmpp
#include <QXmppGlobal.h>
#include <QXmppMessageReceiptManager.h>
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
#include <QXmppMamManager.h>
#endif
class Kaidan;
class MessageModel;
class QMimeType;
class QXmppMessage;
class QSettings;
class QXmppDiscoveryIq;
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
class QXmppCarbonManager;
......@@ -55,7 +59,7 @@ class MessageHandler : public QObject
public:
MessageHandler(Kaidan *kaidan, QXmppClient *client, MessageModel *model,
QObject *parent = nullptr);
QSettings *settings, QObject *parent = nullptr);
~MessageHandler();
......@@ -80,13 +84,35 @@ public slots:
*/
void handleDiscoInfo(const QXmppDiscoveryIq &);
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
/**
* Handles incoming archived messages
*/
void handleArchiveMessage(const QString &queryId, const QXmppMessage &message);
/**
* Called when an archive request was handled
*/
void handleArchiveResults(const QString &queryId,
const QXmppResultSetReply &resultSetReply,
bool complete);
/**
* @brief Requests all missed messages
*/
void requestLatestMessages();
#endif
private:
Kaidan *kaidan;
QXmppClient *client;
QXmppMessageReceiptManager receiptManager;
MessageModel *model;
QSettings *settings;
#if QXMPP_VERSION >= QT_VERSION_CHECK(1, 0, 0)
QXmppCarbonManager *carbonManager;
QXmppMamManager *mamManager;
QStringList jidsToQuery;
#endif
QString chatPartner;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment