From de09bf655ed18a29790102352a75c5215c71e630 Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Mon, 7 Mar 2022 23:48:18 -0800 Subject: [PATCH] refactor(history): Use abstract Chat in ChatHistory rather than Friend ChatHistory will be used by Groups for their history as well. --- src/model/chathistory.cpp | 32 ++++++++++++++++---------------- src/model/chathistory.h | 4 ++-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/model/chathistory.cpp b/src/model/chathistory.cpp index 2dab9fd36..9ee97bc15 100644 --- a/src/model/chathistory.cpp +++ b/src/model/chathistory.cpp @@ -71,10 +71,10 @@ bool handleActionPrefix(QString& content) } } // namespace -ChatHistory::ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& coreIdHandler_, +ChatHistory::ChatHistory(Chat& chat_, History* history_, const ICoreIdHandler& coreIdHandler_, const Settings& settings_, IMessageDispatcher& messageDispatcher, FriendList& friendList) - : f(f_) + : chat(chat_) , history(history_) , settings(settings_) , coreIdHandler(coreIdHandler_) @@ -159,10 +159,10 @@ SearchResult ChatHistory::searchBackward(SearchPos startIdx, const QString& phra // If the double disk access is real bad we can optimize this by adding // another function to history auto dateWherePhraseFound = - history->getDateWhereFindPhrase(f.getPublicKey(), earliestMessageDate, phrase, + history->getDateWhereFindPhrase(chat.getPersistentId(), earliestMessageDate, phrase, parameter); - auto loadIdx = history->getNumMessagesForChatBeforeDate(f.getPublicKey(), dateWherePhraseFound); + auto loadIdx = history->getNumMessagesForChatBeforeDate(chat.getPersistentId(), dateWherePhraseFound); loadHistoryIntoSessionChatLog(ChatLogIdx(loadIdx)); // Reset search pos to the message we just loaded to avoid a double search @@ -189,7 +189,7 @@ std::vector ChatHistory::getDateIdxs(const QDate& size_t maxDates) const { if (canUseHistory()) { - auto counts = history->getNumMessagesForChatBeforeDateBoundaries(f.getPublicKey(), + auto counts = history->getNumMessagesForChatBeforeDateBoundaries(chat.getPersistentId(), startDate, maxDates); std::vector ret; @@ -211,7 +211,7 @@ std::vector ChatHistory::getDateIdxs(const QDate& void ChatHistory::addSystemMessage(const SystemMessage& message) { if (canUseHistory()) { - history->addNewSystemMessage(f.getPublicKey(), message); + history->addNewSystemMessage(chat.getPersistentId(), message); } sessionChatLog.addSystemMessage(message); @@ -224,13 +224,13 @@ void ChatHistory::onFileUpdated(const ToxPk& sender, const ToxFile& file) switch (file.status) { case ToxFile::INITIALIZING: { auto selfPk = coreIdHandler.getSelfPublicKey(); - QString username(selfPk == sender ? coreIdHandler.getUsername() : f.getDisplayedName()); + QString username(selfPk == sender ? coreIdHandler.getUsername() : chat.getDisplayedName(sender)); // Note: There is some implcit coupling between history and the current // chat log. Both rely on generating a new id based on the state of // initializing. If this is changed in the session chat log we'll end up // with a different order when loading from history - history->addNewFileMessage(f.getPublicKey(), file.resumeFileId, file.fileName, + history->addNewFileMessage(chat.getPersistentId(), file.resumeFileId, file.fileName, file.filePath, file.progress.getFileSize(), sender, QDateTime::currentDateTime(), username); break; @@ -267,14 +267,14 @@ void ChatHistory::onFileTransferBrokenUnbroken(const ToxPk& sender, const ToxFil void ChatHistory::onMessageReceived(const ToxPk& sender, const Message& message) { if (canUseHistory()) { - auto friendPk = f.getPublicKey(); - auto displayName = f.getDisplayedName(); + auto& chatId = chat.getPersistentId(); + auto displayName = chat.getDisplayedName(sender); auto content = message.content; if (message.isAction) { content = ChatForm::ACTION_PREFIX + content; } - history->addNewMessage(friendPk, content, friendPk, message.timestamp, true, message.extensionSet, displayName); + history->addNewMessage(chatId, content, sender, message.timestamp, true, message.extensionSet, displayName); } sessionChatLog.onMessageReceived(sender, message); @@ -284,7 +284,7 @@ void ChatHistory::onMessageSent(DispatchedMessageId id, const Message& message) { if (canUseHistory()) { auto selfPk = coreIdHandler.getSelfPublicKey(); - auto friendPk = f.getPublicKey(); + auto& chatId = chat.getPersistentId(); auto content = message.content; if (message.isAction) { @@ -295,7 +295,7 @@ void ChatHistory::onMessageSent(DispatchedMessageId id, const Message& message) auto onInsertion = [this, id](RowId historyId) { handleDispatchedMessage(id, historyId); }; - history->addNewMessage(friendPk, content, selfPk, message.timestamp, false, message.extensionSet, username, + history->addNewMessage(chatId, content, selfPk, message.timestamp, false, message.extensionSet, username, onInsertion); } @@ -354,7 +354,7 @@ void ChatHistory::loadHistoryIntoSessionChatLog(ChatLogIdx start) const // We know that both history and us have a start index of 0 so the type // conversion should be safe assert(getFirstIdx() == ChatLogIdx(0)); - auto messages = history->getMessagesForChat(f.getPublicKey(), start.get(), end.get()); + auto messages = history->getMessagesForChat(chat.getPersistentId(), start.get(), end.get()); assert(messages.size() == static_cast(end.get() - start.get())); ChatLogIdx nextIdx = start; @@ -425,7 +425,7 @@ void ChatHistory::loadHistoryIntoSessionChatLog(ChatLogIdx start) const */ void ChatHistory::dispatchUnsentMessages(IMessageDispatcher& messageDispatcher) { - auto unsentMessages = history->getUndeliveredMessagesForChat(f.getPublicKey()); + auto unsentMessages = history->getUndeliveredMessagesForChat(chat.getPersistentId()); auto requiredExtensions = std::accumulate( unsentMessages.begin(), unsentMessages.end(), @@ -521,7 +521,7 @@ bool ChatHistory::canUseHistory() const ChatLogIdx ChatHistory::getInitialChatLogIdx() const { if (canUseHistory()) { - return ChatLogIdx(history->getNumMessagesForChat(f.getPublicKey())); + return ChatLogIdx(history->getNumMessagesForChat(chat.getPersistentId())); } return ChatLogIdx(0); } diff --git a/src/model/chathistory.h b/src/model/chathistory.h index bcc9927cc..85754e599 100644 --- a/src/model/chathistory.h +++ b/src/model/chathistory.h @@ -33,7 +33,7 @@ class ChatHistory : public IChatLog { Q_OBJECT public: - ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& coreIdHandler_, + ChatHistory(Chat& chat_, History* history_, const ICoreIdHandler& coreIdHandler_, const Settings& settings_, IMessageDispatcher& messageDispatcher, FriendList& friendList); const ChatLogItem& at(ChatLogIdx idx) const override; @@ -68,7 +68,7 @@ private: bool canUseHistory() const; ChatLogIdx getInitialChatLogIdx() const; - Friend& f; + Chat& chat; History* history; const Settings& settings; const ICoreIdHandler& coreIdHandler;