doovers for failed messages, some corner cases fixes with handling errors during message sending

This commit is contained in:
Blue 2021-05-23 01:03:14 +03:00
parent ddfaa63a24
commit 3f1fba4de2
Signed by: blue
GPG Key ID: 9B203B252A63EE38
12 changed files with 89 additions and 19 deletions

View File

@ -923,3 +923,6 @@ void Core::Account::onContactHistoryResponse(const std::list<Shared::Message>& l
void Core::Account::requestChangeMessage(const QString& jid, const QString& messageId, const QMap<QString, QVariant>& data){ void Core::Account::requestChangeMessage(const QString& jid, const QString& messageId, const QMap<QString, QVariant>& data){
mh->requestChangeMessage(jid, messageId, data);} mh->requestChangeMessage(jid, messageId, data);}
void Core::Account::resendMessage(const QString& jid, const QString& id) {
mh->resendMessage(jid, id);}

View File

@ -103,6 +103,7 @@ public:
void removeRoomRequest(const QString& jid); void removeRoomRequest(const QString& jid);
void addRoomRequest(const QString& jid, const QString& nick, const QString& password, bool autoJoin); void addRoomRequest(const QString& jid, const QString& nick, const QString& password, bool autoJoin);
void uploadVCard(const Shared::VCard& card); void uploadVCard(const Shared::VCard& card);
void resendMessage(const QString& jid, const QString& id);
public slots: public slots:
void connect(); void connect();

View File

@ -308,8 +308,9 @@ void Core::Archive::changeMessage(const QString& id, const QMap<QString, QVarian
} }
} }
if (msg.getStanzaId().size() > 0 && (idChange || !hadStanzaId)) { QString qsid = msg.getStanzaId();
const std::string& szid = msg.getStanzaId().toStdString(); if (qsid.size() > 0 && (idChange || !hadStanzaId)) {
std::string szid = qsid.toStdString();
lmdbData.mv_size = szid.size(); lmdbData.mv_size = szid.size();
lmdbData.mv_data = (char*)szid.c_str(); lmdbData.mv_data = (char*)szid.c_str();

View File

@ -73,8 +73,7 @@ void Core::MessageHandler::onMessageReceived(const QXmppMessage& msg)
bool Core::MessageHandler::handleChatMessage(const QXmppMessage& msg, bool outgoing, bool forwarded, bool guessing) bool Core::MessageHandler::handleChatMessage(const QXmppMessage& msg, bool outgoing, bool forwarded, bool guessing)
{ {
const QString& body(msg.body()); if (msg.body().size() != 0 || msg.outOfBandUrl().size() > 0) {
if (body.size() != 0) {
Shared::Message sMsg(Shared::Message::chat); Shared::Message sMsg(Shared::Message::chat);
initializeMessage(sMsg, msg, outgoing, forwarded, guessing); initializeMessage(sMsg, msg, outgoing, forwarded, guessing);
QString jid = sMsg.getPenPalJid(); QString jid = sMsg.getPenPalJid();
@ -234,17 +233,17 @@ void Core::MessageHandler::onReceiptReceived(const QString& jid, const QString&
if (ri != 0) { if (ri != 0) {
ri->changeMessage(id, cData); ri->changeMessage(id, cData);
} }
pendingStateMessages.erase(itr);
emit acc->changeMessage(itr->second, id, cData); emit acc->changeMessage(itr->second, id, cData);
pendingStateMessages.erase(itr);
} }
} }
void Core::MessageHandler::sendMessage(const Shared::Message& data) void Core::MessageHandler::sendMessage(const Shared::Message& data, bool newMessage)
{ {
if (data.getOutOfBandUrl().size() == 0 && data.getAttachPath().size() > 0) { if (data.getOutOfBandUrl().size() == 0 && data.getAttachPath().size() > 0) {
prepareUpload(data); prepareUpload(data, newMessage);
} else { } else {
performSending(data); performSending(data, newMessage);
} }
} }
@ -256,6 +255,7 @@ void Core::MessageHandler::performSending(Shared::Message data, bool newMessage)
RosterItem* ri = acc->rh->getRosterItem(jid); RosterItem* ri = acc->rh->getRosterItem(jid);
bool sent = false; bool sent = false;
QMap<QString, QVariant> changes; QMap<QString, QVariant> changes;
QDateTime sendTime = QDateTime::currentDateTimeUtc();
if (acc->state == Shared::ConnectionState::connected) { if (acc->state == Shared::ConnectionState::connected) {
QXmppMessage msg(acc->getFullJid(), data.getTo(), data.getBody(), data.getThread()); QXmppMessage msg(acc->getFullJid(), data.getTo(), data.getBody(), data.getThread());
@ -266,8 +266,10 @@ void Core::MessageHandler::performSending(Shared::Message data, bool newMessage)
msg.setType(static_cast<QXmppMessage::Type>(data.getType())); //it is safe here, my type is compatible msg.setType(static_cast<QXmppMessage::Type>(data.getType())); //it is safe here, my type is compatible
msg.setOutOfBandUrl(oob); msg.setOutOfBandUrl(oob);
msg.setReceiptRequested(true); msg.setReceiptRequested(true);
msg.setStamp(sendTime);
sent = acc->client.sendPacket(msg); sent = acc->client.sendPacket(msg);
//sent = false;
if (sent) { if (sent) {
data.setState(Shared::Message::State::sent); data.setState(Shared::Message::State::sent);
@ -289,9 +291,10 @@ void Core::MessageHandler::performSending(Shared::Message data, bool newMessage)
if (oob.size() > 0) { if (oob.size() > 0) {
changes.insert("outOfBandUrl", oob); changes.insert("outOfBandUrl", oob);
} }
if (!newMessage) { if (newMessage) {
changes.insert("stamp", data.getTime()); data.setTime(sendTime);
} }
changes.insert("stamp", sendTime);
if (ri != 0) { if (ri != 0) {
if (newMessage) { if (newMessage) {
@ -309,7 +312,7 @@ void Core::MessageHandler::performSending(Shared::Message data, bool newMessage)
emit acc->changeMessage(jid, id, changes); emit acc->changeMessage(jid, id, changes);
} }
void Core::MessageHandler::prepareUpload(const Shared::Message& data) void Core::MessageHandler::prepareUpload(const Shared::Message& data, bool newMessage)
{ {
if (acc->state == Shared::ConnectionState::connected) { if (acc->state == Shared::ConnectionState::connected) {
QString jid = data.getPenPalJid(); QString jid = data.getPenPalJid();
@ -322,16 +325,23 @@ void Core::MessageHandler::prepareUpload(const Shared::Message& data)
QString path = data.getAttachPath(); QString path = data.getAttachPath();
QString url = acc->network->getFileRemoteUrl(path); QString url = acc->network->getFileRemoteUrl(path);
if (url.size() != 0) { if (url.size() != 0) {
sendMessageWithLocalUploadedFile(data, url); sendMessageWithLocalUploadedFile(data, url, newMessage);
} else { } else {
if (acc->network->checkAndAddToUploading(acc->getName(), jid, id, path)) { pendingStateMessages.insert(std::make_pair(id, jid));
if (newMessage) {
ri->appendMessageToArchive(data); ri->appendMessageToArchive(data);
pendingStateMessages.insert(std::make_pair(id, jid));
} else { } else {
QMap<QString, QVariant> changes({
{"state", (uint)Shared::Message::State::pending}
});
ri->changeMessage(id, changes);
emit acc->changeMessage(jid, id, changes);
}
//this checks if the file is already uploading, and if so it subscribes to it's success, so, i need to do stuff only if the network knows nothing of this file
if (!acc->network->checkAndAddToUploading(acc->getName(), jid, id, path)) {
if (acc->um->serviceFound()) { if (acc->um->serviceFound()) {
QFileInfo file(path); QFileInfo file(path);
if (file.exists() && file.isReadable()) { if (file.exists() && file.isReadable()) {
ri->appendMessageToArchive(data);
pendingStateMessages.insert(std::make_pair(id, jid)); pendingStateMessages.insert(std::make_pair(id, jid));
uploadingSlotsQueue.emplace_back(path, id); uploadingSlotsQueue.emplace_back(path, id);
if (uploadingSlotsQueue.size() == 1) { if (uploadingSlotsQueue.size() == 1) {
@ -353,7 +363,6 @@ void Core::MessageHandler::prepareUpload(const Shared::Message& data)
} }
} }
void Core::MessageHandler::onUploadSlotReceived(const QXmppHttpUploadSlotIq& slot) void Core::MessageHandler::onUploadSlotReceived(const QXmppHttpUploadSlotIq& slot)
{ {
if (uploadingSlotsQueue.size() == 0) { if (uploadingSlotsQueue.size() == 0) {
@ -481,3 +490,22 @@ void Core::MessageHandler::requestChangeMessage(const QString& jid, const QStrin
} }
} }
} }
void Core::MessageHandler::resendMessage(const QString& jid, const QString& id)
{
RosterItem* cnt = acc->rh->getRosterItem(jid);
if (cnt != 0) {
try {
Shared::Message msg = cnt->getMessage(id);
if (msg.getState() == Shared::Message::State::error) {
sendMessage(msg, false);
} else {
qDebug() << "An attempt to resend a message to" << jid << "by account" << acc->getName() << ", but this message seems to have been normally sent, this method was made to retry sending failed to be sent messages, skipping";
}
} catch (const Archive::NotFound& err) {
qDebug() << "An attempt to resend a message to" << jid << "by account" << acc->getName() << ", but this message wasn't found in history, skipping";
}
} else {
qDebug() << "An attempt to resend a message to" << jid << "by account" << acc->getName() << ", but this jid isn't present in account roster, skipping";
}
}

View File

@ -45,8 +45,9 @@ public:
MessageHandler(Account* account); MessageHandler(Account* account);
public: public:
void sendMessage(const Shared::Message& data); void sendMessage(const Shared::Message& data, bool newMessage = true);
void initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing = false, bool forwarded = false, bool guessing = false) const; void initializeMessage(Shared::Message& target, const QXmppMessage& source, bool outgoing = false, bool forwarded = false, bool guessing = false) const;
void resendMessage(const QString& jid, const QString& id);
public slots: public slots:
void onMessageReceived(const QXmppMessage& message); void onMessageReceived(const QXmppMessage& message);
@ -66,7 +67,7 @@ private:
void logMessage(const QXmppMessage& msg, const QString& reason = "Message wasn't handled: "); void logMessage(const QXmppMessage& msg, const QString& reason = "Message wasn't handled: ");
void sendMessageWithLocalUploadedFile(Shared::Message msg, const QString& url, bool newMessage = true); void sendMessageWithLocalUploadedFile(Shared::Message msg, const QString& url, bool newMessage = true);
void performSending(Shared::Message data, bool newMessage = true); void performSending(Shared::Message data, bool newMessage = true);
void prepareUpload(const Shared::Message& data); void prepareUpload(const Shared::Message& data, bool newMessage = true);
void handleUploadError(const QString& jid, const QString& messageId, const QString& errorText); void handleUploadError(const QString& jid, const QString& messageId, const QString& errorText);
private: private:

View File

@ -100,6 +100,7 @@ int main(int argc, char *argv[])
QObject::connect(&w, &Squawk::disconnectAccount, squawk, &Core::Squawk::disconnectAccount); QObject::connect(&w, &Squawk::disconnectAccount, squawk, &Core::Squawk::disconnectAccount);
QObject::connect(&w, &Squawk::changeState, squawk, &Core::Squawk::changeState); QObject::connect(&w, &Squawk::changeState, squawk, &Core::Squawk::changeState);
QObject::connect(&w, &Squawk::sendMessage, squawk,&Core::Squawk::sendMessage); QObject::connect(&w, &Squawk::sendMessage, squawk,&Core::Squawk::sendMessage);
QObject::connect(&w, &Squawk::resendMessage, squawk,&Core::Squawk::resendMessage);
QObject::connect(&w, &Squawk::requestArchive, squawk, &Core::Squawk::requestArchive); QObject::connect(&w, &Squawk::requestArchive, squawk, &Core::Squawk::requestArchive);
QObject::connect(&w, &Squawk::subscribeContact, squawk, &Core::Squawk::subscribeContact); QObject::connect(&w, &Squawk::subscribeContact, squawk, &Core::Squawk::subscribeContact);
QObject::connect(&w, &Squawk::unsubscribeContact, squawk, &Core::Squawk::unsubscribeContact); QObject::connect(&w, &Squawk::unsubscribeContact, squawk, &Core::Squawk::unsubscribeContact);

View File

@ -328,13 +328,24 @@ void Core::Squawk::sendMessage(const QString& account, const Shared::Message& da
{ {
AccountsMap::const_iterator itr = amap.find(account); AccountsMap::const_iterator itr = amap.find(account);
if (itr == amap.end()) { if (itr == amap.end()) {
qDebug("An attempt to send a message with non existing account, skipping"); qDebug() << "An attempt to send a message with non existing account" << account << ", skipping";
return; return;
} }
itr->second->sendMessage(data); itr->second->sendMessage(data);
} }
void Core::Squawk::resendMessage(const QString& account, const QString& jid, const QString& id)
{
AccountsMap::const_iterator itr = amap.find(account);
if (itr == amap.end()) {
qDebug() << "An attempt to resend a message with non existing account" << account << ", skipping";
return;
}
itr->second->resendMessage(jid, id);
}
void Core::Squawk::requestArchive(const QString& account, const QString& jid, int count, const QString& before) void Core::Squawk::requestArchive(const QString& account, const QString& jid, int count, const QString& before)
{ {
AccountsMap::const_iterator itr = amap.find(account); AccountsMap::const_iterator itr = amap.find(account);

View File

@ -101,6 +101,7 @@ public slots:
void changeState(Shared::Availability state); void changeState(Shared::Availability state);
void sendMessage(const QString& account, const Shared::Message& data); void sendMessage(const QString& account, const Shared::Message& data);
void resendMessage(const QString& account, const QString& jid, const QString& id);
void requestArchive(const QString& account, const QString& jid, int count, const QString& before); void requestArchive(const QString& account, const QString& jid, int count, const QString& before);
void subscribeContact(const QString& account, const QString& jid, const QString& reason); void subscribeContact(const QString& account, const QString& jid, const QString& reason);

View File

@ -466,6 +466,15 @@ void Squawk::onConversationMessage(const Shared::Message& msg)
emit sendMessage(acc, msg); emit sendMessage(acc, msg);
} }
void Squawk::onConversationResend(const QString& id)
{
Conversation* conv = static_cast<Conversation*>(sender());
QString acc = conv->getAccount();
QString jid = conv->getJid();
emit resendMessage(acc, jid, id);
}
void Squawk::onRequestArchive(const QString& account, const QString& jid, const QString& before) void Squawk::onRequestArchive(const QString& account, const QString& jid, const QString& before)
{ {
emit requestArchive(account, jid, 20, before); //TODO amount as a settings value emit requestArchive(account, jid, 20, before); //TODO amount as a settings value
@ -914,6 +923,7 @@ void Squawk::subscribeConversation(Conversation* conv)
{ {
connect(conv, &Conversation::destroyed, this, &Squawk::onConversationClosed); connect(conv, &Conversation::destroyed, this, &Squawk::onConversationClosed);
connect(conv, &Conversation::sendMessage, this, &Squawk::onConversationMessage); connect(conv, &Conversation::sendMessage, this, &Squawk::onConversationMessage);
connect(conv, &Conversation::resendMessage, this, &Squawk::onConversationResend);
connect(conv, &Conversation::notifyableMessage, this, &Squawk::notify); connect(conv, &Conversation::notifyableMessage, this, &Squawk::notify);
} }

View File

@ -63,6 +63,7 @@ signals:
void disconnectAccount(const QString&); void disconnectAccount(const QString&);
void changeState(Shared::Availability state); void changeState(Shared::Availability state);
void sendMessage(const QString& account, const Shared::Message& data); void sendMessage(const QString& account, const Shared::Message& data);
void resendMessage(const QString& account, const QString& jid, const QString& id);
void requestArchive(const QString& account, const QString& jid, int count, const QString& before); void requestArchive(const QString& account, const QString& jid, int count, const QString& before);
void subscribeContact(const QString& account, const QString& jid, const QString& reason); void subscribeContact(const QString& account, const QString& jid, const QString& reason);
void unsubscribeContact(const QString& account, const QString& jid, const QString& reason); void unsubscribeContact(const QString& account, const QString& jid, const QString& reason);
@ -148,6 +149,7 @@ private slots:
void onComboboxActivated(int index); void onComboboxActivated(int index);
void onRosterItemDoubleClicked(const QModelIndex& item); void onRosterItemDoubleClicked(const QModelIndex& item);
void onConversationMessage(const Shared::Message& msg); void onConversationMessage(const Shared::Message& msg);
void onConversationResend(const QString& id);
void onRequestArchive(const QString& account, const QString& jid, const QString& before); void onRequestArchive(const QString& account, const QString& jid, const QString& before);
void onRosterContextMenu(const QPoint& point); void onRosterContextMenu(const QPoint& point);
void onItemCollepsed(const QModelIndex& index); void onItemCollepsed(const QModelIndex& index);

View File

@ -414,6 +414,16 @@ void Conversation::onFeedContext(const QPoint& pos)
contextMenu->clear(); contextMenu->clear();
bool showMenu = false; bool showMenu = false;
if (item->getState() == Shared::Message::State::error) {
showMenu = true;
QString id = item->getId();
QAction* resend = contextMenu->addAction(Shared::icon("view-refresh"), tr("Try sending again"));
connect(resend, &QAction::triggered, [this, id]() {
element->feed->registerUpload(id);
emit resendMessage(id);
});
}
QString path = item->getAttachPath(); QString path = item->getAttachPath();
if (path.size() > 0) { if (path.size() > 0) {
showMenu = true; showMenu = true;

View File

@ -80,6 +80,7 @@ public:
signals: signals:
void sendMessage(const Shared::Message& message); void sendMessage(const Shared::Message& message);
void resendMessage(const QString& id);
void requestArchive(const QString& before); void requestArchive(const QString& before);
void shown(); void shown();
void requestLocalFile(const QString& messageId, const QString& url); void requestLocalFile(const QString& messageId, const QString& url);