Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix(offlinemsg): fix offline message dispatching on history load
Browse files Browse the repository at this point in the history
* Fix callback hookup order in ChatHistory
* Add correct call to SessionChatLog to insert an unfinished message
* Fix incorrect logic in parsing History database response
  • Loading branch information
sphaerophoria authored and anthonybilinski committed Oct 20, 2019
1 parent 86b55a0 commit dbef0b7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
17 changes: 8 additions & 9 deletions src/model/chathistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ ChatHistory::ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& co
, settings(settings_)
, coreIdHandler(coreIdHandler)
{
connect(&messageDispatcher, &IMessageDispatcher::messageSent, this, &ChatHistory::onMessageSent);
connect(&messageDispatcher, &IMessageDispatcher::messageComplete, this,
&ChatHistory::onMessageComplete);
connect(&messageDispatcher, &IMessageDispatcher::messageReceived, this,
&ChatHistory::onMessageReceived);

if (canUseHistory()) {
// Defer messageSent callback until we finish firing off all our unsent messages.
Expand All @@ -105,8 +106,7 @@ ChatHistory::ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& co
}

// Now that we've fired off our unsent messages we can connect the message
connect(&messageDispatcher, &IMessageDispatcher::messageReceived, this,
&ChatHistory::onMessageReceived);
connect(&messageDispatcher, &IMessageDispatcher::messageSent, this, &ChatHistory::onMessageSent);

// NOTE: this has to be done _after_ sending all sent messages since initial
// state of the message has to be marked according to our dispatch state
Expand Down Expand Up @@ -395,14 +395,13 @@ void ChatHistory::loadHistoryIntoSessionChatLog(ChatLogIdx start) const
[&](RowId dispatchedId) { return dispatchedId == message.id; });

bool isComplete = dispatchedMessageIt == dispatchedMessageRowIdMap.end();

auto chatLogMessage = ChatLogMessage{isComplete, processedMessage};
if (isComplete) {
auto chatLogMessage = ChatLogMessage{true, processedMessage};
sessionChatLog.insertMessageAtIdx(currentIdx, sender, message.dispName, chatLogMessage);
sessionChatLog.insertCompleteMessageAtIdx(currentIdx, sender, message.dispName,
chatLogMessage);
} else {
// If the message is incomplete we have to pretend we sent it to ensure
// sessionChatLog state is correct
sessionChatLog.onMessageSent(dispatchedMessageIt.key(), processedMessage);
sessionChatLog.insertIncompleteMessageAtIdx(currentIdx, sender, message.dispName,
chatLogMessage, dispatchedMessageIt.key());
}
break;
}
Expand Down
24 changes: 21 additions & 3 deletions src/model/sessionchatlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,37 @@ std::size_t SessionChatLog::size() const
return items.size();
}

void SessionChatLog::insertMessageAtIdx(ChatLogIdx idx, ToxPk sender, QString senderName,
ChatLogMessage message)
void SessionChatLog::insertCompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName,
const ChatLogMessage& message)
{
auto item = ChatLogItem(sender, message);

if (!senderName.isEmpty()) {
item.setDisplayName(senderName);
}

assert(message.isComplete == true);

items.emplace(idx, std::move(item));
}

void SessionChatLog::insertIncompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName,
const ChatLogMessage& message,
DispatchedMessageId dispatchId)
{
auto item = ChatLogItem(sender, message);

if (!senderName.isEmpty()) {
item.setDisplayName(senderName);
}

assert(message.isComplete == false);

items.emplace(idx, std::move(item));
outgoingMessages.insert(dispatchId, idx);
}

void SessionChatLog::insertFileAtIdx(ChatLogIdx idx, ToxPk sender, QString senderName, ChatLogFile file)
void SessionChatLog::insertFileAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName, const ChatLogFile& file)
{
auto item = ChatLogItem(sender, file);

Expand Down
7 changes: 5 additions & 2 deletions src/model/sessionchatlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ class SessionChatLog : public IChatLog
std::vector<DateChatLogIdxPair> getDateIdxs(const QDate& startDate, size_t maxDates) const override;
std::size_t size() const override;

void insertMessageAtIdx(ChatLogIdx idx, ToxPk sender, QString senderName, ChatLogMessage message);
void insertFileAtIdx(ChatLogIdx idx, ToxPk sender, QString senderName, ChatLogFile file);
void insertCompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName,
const ChatLogMessage& message);
void insertIncompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName,
const ChatLogMessage& message, DispatchedMessageId dispatchId);
void insertFileAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName, const ChatLogFile& file);

public slots:
void onMessageReceived(const ToxPk& sender, const Message& message);
Expand Down
6 changes: 2 additions & 4 deletions src/persistence/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,8 @@ QList<History::HistMessage> History::getUnsentMessagesForFriend(const ToxPk& fri
auto friend_key = row[3].toString();
auto display_name = QString::fromUtf8(row[4].toByteArray().replace('\0', ""));
auto sender_key = row[5].toString();
if (row[6].isNull()) {
ret += {id, isOfflineMessage, timestamp, friend_key,
display_name, sender_key, row[6].toString()};
}
ret += {id, isOfflineMessage, timestamp, friend_key,
display_name, sender_key, row[6].toString()};
};

db->execNow({queryText, rowCallback});
Expand Down

0 comments on commit dbef0b7

Please sign in to comment.