forked from blue/squawk
now it's possible to fix your messages
This commit is contained in:
parent
bf4a27f35d
commit
788c6ca556
13 changed files with 204 additions and 85 deletions
|
@ -935,3 +935,7 @@ void Core::Account::requestChangeMessage(const QString& jid, const QString& mess
|
|||
|
||||
void Core::Account::resendMessage(const QString& jid, const QString& id) {
|
||||
mh->resendMessage(jid, id);}
|
||||
|
||||
void Core::Account::replaceMessage(const QString& originalId, const Shared::Message& data) {
|
||||
mh->sendMessage(data, false, originalId);}
|
||||
|
||||
|
|
|
@ -104,6 +104,7 @@ public:
|
|||
void addRoomRequest(const QString& jid, const QString& nick, const QString& password, bool autoJoin);
|
||||
void uploadVCard(const Shared::VCard& card);
|
||||
void resendMessage(const QString& jid, const QString& id);
|
||||
void replaceMessage(const QString& originalId, const Shared::Message& data);
|
||||
|
||||
public slots:
|
||||
void connect();
|
||||
|
|
|
@ -41,10 +41,10 @@ void Core::MessageHandler::onMessageReceived(const QXmppMessage& msg)
|
|||
handled = handleGroupMessage(msg);
|
||||
break;
|
||||
case QXmppMessage::Error: {
|
||||
QString id = msg.id();
|
||||
std::map<QString, QString>::const_iterator itr = pendingStateMessages.find(id);
|
||||
if (itr != pendingStateMessages.end()) {
|
||||
QString jid = itr->second;
|
||||
std::tuple<bool, QString, QString> ids = getOriginalPendingMessageId(msg.id());
|
||||
if (std::get<0>(ids)) {
|
||||
QString id = std::get<1>(ids);
|
||||
QString jid = std::get<2>(ids);
|
||||
RosterItem* cnt = acc->rh->getRosterItem(jid);
|
||||
QMap<QString, QVariant> cData = {
|
||||
{"state", static_cast<uint>(Shared::Message::State::error)},
|
||||
|
@ -53,9 +53,7 @@ void Core::MessageHandler::onMessageReceived(const QXmppMessage& msg)
|
|||
if (cnt != 0) {
|
||||
cnt->changeMessage(id, cData);
|
||||
}
|
||||
;
|
||||
emit acc->changeMessage(jid, id, cData);
|
||||
pendingStateMessages.erase(itr);
|
||||
handled = true;
|
||||
} else {
|
||||
qDebug() << "received a message with type \"Error\", not sure what to do with it now, skipping";
|
||||
|
@ -111,7 +109,6 @@ bool Core::MessageHandler::handleGroupMessage(const QXmppMessage& msg, bool outg
|
|||
{
|
||||
const QString& body(msg.body());
|
||||
if (body.size() != 0) {
|
||||
QString id = msg.id();
|
||||
|
||||
Shared::Message sMsg(Shared::Message::groupChat);
|
||||
initializeMessage(sMsg, msg, outgoing, forwarded, guessing);
|
||||
|
@ -121,12 +118,11 @@ bool Core::MessageHandler::handleGroupMessage(const QXmppMessage& msg, bool outg
|
|||
return false;
|
||||
}
|
||||
|
||||
std::map<QString, QString>::const_iterator pItr = pendingStateMessages.find(id);
|
||||
if (pItr != pendingStateMessages.end()) {
|
||||
std::tuple<bool, QString, QString> ids = getOriginalPendingMessageId(msg.id());
|
||||
if (std::get<0>(ids)) {
|
||||
QMap<QString, QVariant> cData = {{"state", static_cast<uint>(Shared::Message::State::delivered)}};
|
||||
cnt->changeMessage(id, cData);
|
||||
pendingStateMessages.erase(pItr);
|
||||
emit acc->changeMessage(jid, id, cData);
|
||||
cnt->changeMessage(std::get<1>(ids), cData);
|
||||
emit acc->changeMessage(std::get<2>(ids), std::get<1>(ids), cData);
|
||||
} else {
|
||||
QString oId = msg.replaceId();
|
||||
if (oId.size() > 0) {
|
||||
|
@ -227,53 +223,70 @@ void Core::MessageHandler::onCarbonMessageSent(const QXmppMessage& msg)
|
|||
handleChatMessage(msg, true, true);
|
||||
}
|
||||
|
||||
void Core::MessageHandler::onReceiptReceived(const QString& jid, const QString& id)
|
||||
std::tuple<bool, QString, QString> Core::MessageHandler::getOriginalPendingMessageId(const QString& id)
|
||||
{
|
||||
std::tuple<bool, QString, QString> result({false, "", ""});
|
||||
std::map<QString, QString>::const_iterator itr = pendingStateMessages.find(id);
|
||||
if (itr != pendingStateMessages.end()) {
|
||||
QMap<QString, QVariant> cData = {{"state", static_cast<uint>(Shared::Message::State::delivered)}};
|
||||
RosterItem* ri = acc->rh->getRosterItem(itr->second);
|
||||
if (ri != 0) {
|
||||
ri->changeMessage(id, cData);
|
||||
std::get<0>(result) = true;
|
||||
std::get<2>(result) = itr->second;
|
||||
|
||||
std::map<QString, QString>::const_iterator itrC = pendingCorrectionMessages.find(id);
|
||||
if (itrC != pendingCorrectionMessages.end()) {
|
||||
std::get<1>(result) = itrC->second;
|
||||
pendingCorrectionMessages.erase(itrC);
|
||||
} else {
|
||||
std::get<1>(result) = itr->first;
|
||||
}
|
||||
emit acc->changeMessage(itr->second, id, cData);
|
||||
|
||||
pendingStateMessages.erase(itr);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Core::MessageHandler::sendMessage(const Shared::Message& data, bool newMessage)
|
||||
void Core::MessageHandler::onReceiptReceived(const QString& jid, const QString& id)
|
||||
{
|
||||
if (data.getOutOfBandUrl().size() == 0 && data.getAttachPath().size() > 0) {
|
||||
prepareUpload(data, newMessage);
|
||||
} else {
|
||||
performSending(data, newMessage);
|
||||
std::tuple<bool, QString, QString> ids = getOriginalPendingMessageId(id);
|
||||
if (std::get<0>(ids)) {
|
||||
QMap<QString, QVariant> cData = {{"state", static_cast<uint>(Shared::Message::State::delivered)}};
|
||||
RosterItem* ri = acc->rh->getRosterItem(std::get<2>(ids));
|
||||
|
||||
if (ri != 0) {
|
||||
ri->changeMessage(std::get<1>(ids), cData);
|
||||
}
|
||||
emit acc->changeMessage(std::get<2>(ids), std::get<1>(ids), cData);
|
||||
}
|
||||
}
|
||||
|
||||
void Core::MessageHandler::performSending(Shared::Message data, bool newMessage)
|
||||
void Core::MessageHandler::sendMessage(const Shared::Message& data, bool newMessage, QString originalId)
|
||||
{
|
||||
if (data.getOutOfBandUrl().size() == 0 && data.getAttachPath().size() > 0) {
|
||||
pendingCorrectionMessages.insert(std::make_pair(data.getId(), originalId));
|
||||
prepareUpload(data, newMessage);
|
||||
} else {
|
||||
performSending(data, originalId, newMessage);
|
||||
}
|
||||
}
|
||||
|
||||
void Core::MessageHandler::performSending(Shared::Message data, const QString& originalId, bool newMessage)
|
||||
{
|
||||
QString jid = data.getPenPalJid();
|
||||
QString id = data.getId();
|
||||
QString oob = data.getOutOfBandUrl();
|
||||
qDebug() << "Sending message with id:" << id;
|
||||
if (originalId.size() > 0) {
|
||||
qDebug() << "To replace one with id:" << originalId;
|
||||
}
|
||||
RosterItem* ri = acc->rh->getRosterItem(jid);
|
||||
bool sent = false;
|
||||
QMap<QString, QVariant> changes;
|
||||
if (newMessage && originalId.size() > 0) {
|
||||
newMessage = false;
|
||||
}
|
||||
QDateTime sendTime = QDateTime::currentDateTimeUtc();
|
||||
if (acc->state == Shared::ConnectionState::connected) {
|
||||
QXmppMessage msg(acc->getFullJid(), data.getTo(), data.getBody(), data.getThread());
|
||||
|
||||
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 3, 0)
|
||||
msg.setOriginId(id);
|
||||
#endif
|
||||
msg.setId(id);
|
||||
msg.setType(static_cast<QXmppMessage::Type>(data.getType())); //it is safe here, my type is compatible
|
||||
msg.setOutOfBandUrl(oob);
|
||||
msg.setReceiptRequested(true);
|
||||
msg.setStamp(sendTime);
|
||||
QXmppMessage msg(createPacket(data, sendTime, originalId));
|
||||
|
||||
sent = acc->client.sendPacket(msg);
|
||||
//sent = false;
|
||||
|
||||
if (sent) {
|
||||
data.setState(Shared::Message::State::sent);
|
||||
} else {
|
||||
|
@ -286,6 +299,39 @@ void Core::MessageHandler::performSending(Shared::Message data, bool newMessage)
|
|||
data.setErrorText("You are is offline or reconnecting");
|
||||
}
|
||||
|
||||
QMap<QString, QVariant> changes(getChanges(data, sendTime, newMessage, originalId));
|
||||
|
||||
QString realId;
|
||||
if (originalId.size() > 0) {
|
||||
realId = originalId;
|
||||
} else {
|
||||
realId = id;
|
||||
}
|
||||
if (ri != 0) {
|
||||
if (newMessage) {
|
||||
ri->appendMessageToArchive(data);
|
||||
} else {
|
||||
ri->changeMessage(realId, changes);
|
||||
}
|
||||
if (sent) {
|
||||
pendingStateMessages.insert(std::make_pair(id, jid));
|
||||
if (originalId.size() > 0) {
|
||||
pendingCorrectionMessages.insert(std::make_pair(id, originalId));
|
||||
}
|
||||
} else {
|
||||
pendingStateMessages.erase(id);
|
||||
pendingCorrectionMessages.erase(id);
|
||||
}
|
||||
}
|
||||
|
||||
emit acc->changeMessage(jid, realId, changes);
|
||||
}
|
||||
|
||||
QMap<QString, QVariant> Core::MessageHandler::getChanges(Shared::Message& data, const QDateTime& time, bool newMessage, const QString& originalId) const
|
||||
{
|
||||
QMap<QString, QVariant> changes;
|
||||
|
||||
QString oob = data.getOutOfBandUrl();
|
||||
Shared::Message::State mstate = data.getState();
|
||||
changes.insert("state", static_cast<uint>(mstate));
|
||||
if (mstate == Shared::Message::State::error) {
|
||||
|
@ -295,9 +341,12 @@ void Core::MessageHandler::performSending(Shared::Message data, bool newMessage)
|
|||
changes.insert("outOfBandUrl", oob);
|
||||
}
|
||||
if (newMessage) {
|
||||
data.setTime(sendTime);
|
||||
data.setTime(time);
|
||||
}
|
||||
changes.insert("stamp", sendTime);
|
||||
if (originalId.size() > 0) {
|
||||
changes.insert("body", data.getBody());
|
||||
}
|
||||
changes.insert("stamp", time);
|
||||
|
||||
//sometimes (when the image is pasted with ctrl+v)
|
||||
//I start sending message with one path, then copy it to downloads directory
|
||||
|
@ -310,21 +359,29 @@ void Core::MessageHandler::performSending(Shared::Message data, bool newMessage)
|
|||
data.setAttachPath(squawkified);
|
||||
}
|
||||
}
|
||||
|
||||
if (ri != 0) {
|
||||
if (newMessage) {
|
||||
ri->appendMessageToArchive(data);
|
||||
} else {
|
||||
ri->changeMessage(id, changes);
|
||||
}
|
||||
if (sent) {
|
||||
pendingStateMessages.insert(std::make_pair(id, jid));
|
||||
} else {
|
||||
pendingStateMessages.erase(id);
|
||||
}
|
||||
|
||||
return changes;
|
||||
}
|
||||
|
||||
QXmppMessage Core::MessageHandler::createPacket(const Shared::Message& data, const QDateTime& time, const QString& originalId) const
|
||||
{
|
||||
QXmppMessage msg(acc->getFullJid(), data.getTo(), data.getBody(), data.getThread());
|
||||
QString id(data.getId());
|
||||
|
||||
if (originalId.size() > 0) {
|
||||
msg.setReplaceId(originalId);
|
||||
}
|
||||
|
||||
emit acc->changeMessage(jid, id, changes);
|
||||
|
||||
#if (QXMPP_VERSION) >= QT_VERSION_CHECK(1, 3, 0)
|
||||
msg.setOriginId(id);
|
||||
#endif
|
||||
msg.setId(id);
|
||||
msg.setType(static_cast<QXmppMessage::Type>(data.getType())); //it is safe here, my type is compatible
|
||||
msg.setOutOfBandUrl(data.getOutOfBandUrl());
|
||||
msg.setReceiptRequested(true);
|
||||
msg.setStamp(time);
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
void Core::MessageHandler::prepareUpload(const Shared::Message& data, bool newMessage)
|
||||
|
@ -444,7 +501,8 @@ void Core::MessageHandler::onLoadFileError(const std::list<Shared::MessageInfo>&
|
|||
void Core::MessageHandler::handleUploadError(const QString& jid, const QString& messageId, const QString& errorText)
|
||||
{
|
||||
emit acc->uploadFileError(jid, messageId, "Error requesting slot to upload file: " + errorText);
|
||||
pendingStateMessages.erase(jid);
|
||||
pendingStateMessages.erase(messageId);
|
||||
pendingCorrectionMessages.erase(messageId);
|
||||
requestChangeMessage(jid, messageId, {
|
||||
{"state", static_cast<uint>(Shared::Message::State::error)},
|
||||
{"errorText", errorText}
|
||||
|
@ -473,11 +531,11 @@ void Core::MessageHandler::sendMessageWithLocalUploadedFile(Shared::Message msg,
|
|||
if (msg.getBody().size() == 0) { //not sure why, but most messages do that
|
||||
msg.setBody(url); //they duplicate oob in body, some of them wouldn't even show an attachment if you don't do that
|
||||
}
|
||||
performSending(msg, newMessage);
|
||||
performSending(msg, pendingCorrectionMessages.at(msg.getId()), newMessage);
|
||||
//TODO removal/progress update
|
||||
}
|
||||
|
||||
static const std::set<QString> allowerToChangeKeys({
|
||||
static const std::set<QString> allowedToChangeKeys({
|
||||
"attachPath",
|
||||
"outOfBandUrl",
|
||||
"state",
|
||||
|
@ -490,12 +548,12 @@ void Core::MessageHandler::requestChangeMessage(const QString& jid, const QStrin
|
|||
if (cnt != 0) {
|
||||
bool allSupported = true;
|
||||
QString unsupportedString;
|
||||
for (QMap<QString, QVariant>::const_iterator itr = data.begin(); itr != data.end(); ++itr) { //I need all this madness
|
||||
if (allowerToChangeKeys.count(itr.key()) != 1) { //to not allow this method
|
||||
allSupported = false; //to make a message to look like if it was edited
|
||||
unsupportedString = itr.key(); //basically I needed to control who exaclty calls this method
|
||||
break; //because the underlying tech assumes that the change is initiated by user
|
||||
} //not by system
|
||||
for (QMap<QString, QVariant>::const_iterator itr = data.begin(); itr != data.end(); ++itr) { //I need all this madness
|
||||
if (allowedToChangeKeys.count(itr.key()) != 1) { //to not allow this method
|
||||
allSupported = false; //to make a message to look like if it was edited
|
||||
unsupportedString = itr.key(); //basically I needed to control who exaclty calls this method
|
||||
break; //because the underlying tech assumes that
|
||||
} //the change is initiated by user, not by system
|
||||
}
|
||||
if (allSupported) {
|
||||
cnt->changeMessage(messageId, data);
|
||||
|
@ -514,7 +572,13 @@ void Core::MessageHandler::resendMessage(const QString& jid, const QString& id)
|
|||
try {
|
||||
Shared::Message msg = cnt->getMessage(id);
|
||||
if (msg.getState() == Shared::Message::State::error) {
|
||||
sendMessage(msg, false);
|
||||
if (msg.getEdited()){
|
||||
QString originalId = msg.getId();
|
||||
msg.generateRandomId();
|
||||
sendMessage(msg, false, originalId);
|
||||
} else {
|
||||
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";
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ public:
|
|||
MessageHandler(Account* account);
|
||||
|
||||
public:
|
||||
void sendMessage(const Shared::Message& data, bool newMessage = true);
|
||||
void sendMessage(const Shared::Message& data, bool newMessage = true, QString originalId = "");
|
||||
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);
|
||||
|
||||
|
@ -67,13 +67,17 @@ private:
|
|||
bool handleGroupMessage(const QXmppMessage& msg, bool outgoing = false, bool forwarded = false, bool guessing = false);
|
||||
void logMessage(const QXmppMessage& msg, const QString& reason = "Message wasn't handled: ");
|
||||
void sendMessageWithLocalUploadedFile(Shared::Message msg, const QString& url, bool newMessage = true);
|
||||
void performSending(Shared::Message data, bool newMessage = true);
|
||||
void performSending(Shared::Message data, const QString& originalId, bool newMessage = true);
|
||||
void prepareUpload(const Shared::Message& data, bool newMessage = true);
|
||||
void handleUploadError(const QString& jid, const QString& messageId, const QString& errorText);
|
||||
QXmppMessage createPacket(const Shared::Message& data, const QDateTime& time, const QString& originalId) const;
|
||||
QMap<QString, QVariant> getChanges(Shared::Message& data, const QDateTime& time, bool newMessage, const QString& originalId) const;
|
||||
std::tuple<bool, QString, QString> getOriginalPendingMessageId(const QString& id);
|
||||
|
||||
private:
|
||||
Account* acc;
|
||||
std::map<QString, QString> pendingStateMessages; //key is message id, value is JID
|
||||
std::map<QString, QString> pendingCorrectionMessages; //key is new mesage, value is originalOne
|
||||
std::deque<std::pair<QString, QString>> uploadingSlotsQueue;
|
||||
};
|
||||
|
||||
|
|
|
@ -142,6 +142,7 @@ int main(int argc, char *argv[])
|
|||
QObject::connect(&w, &Squawk::disconnectAccount, squawk, &Core::Squawk::disconnectAccount);
|
||||
QObject::connect(&w, &Squawk::changeState, squawk, &Core::Squawk::changeState);
|
||||
QObject::connect(&w, &Squawk::sendMessage, squawk,&Core::Squawk::sendMessage);
|
||||
QObject::connect(&w, &Squawk::replaceMessage, squawk,&Core::Squawk::replaceMessage);
|
||||
QObject::connect(&w, &Squawk::resendMessage, squawk,&Core::Squawk::resendMessage);
|
||||
QObject::connect(&w, &Squawk::requestArchive, squawk, &Core::Squawk::requestArchive);
|
||||
QObject::connect(&w, &Squawk::subscribeContact, squawk, &Core::Squawk::subscribeContact);
|
||||
|
|
|
@ -341,6 +341,17 @@ void Core::Squawk::sendMessage(const QString& account, const Shared::Message& da
|
|||
itr->second->sendMessage(data);
|
||||
}
|
||||
|
||||
void Core::Squawk::replaceMessage(const QString& account, const QString& originalId, const Shared::Message& data)
|
||||
{
|
||||
AccountsMap::const_iterator itr = amap.find(account);
|
||||
if (itr == amap.end()) {
|
||||
qDebug() << "An attempt to replace a message with non existing account" << account << ", skipping";
|
||||
return;
|
||||
}
|
||||
|
||||
itr->second->replaceMessage(originalId, data);
|
||||
}
|
||||
|
||||
void Core::Squawk::resendMessage(const QString& account, const QString& jid, const QString& id)
|
||||
{
|
||||
AccountsMap::const_iterator itr = amap.find(account);
|
||||
|
|
|
@ -101,6 +101,7 @@ public slots:
|
|||
void changeState(Shared::Availability state);
|
||||
|
||||
void sendMessage(const QString& account, const Shared::Message& data);
|
||||
void replaceMessage(const QString& account, const QString& originalId, 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);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue