Browse Source

refactor(history): Use abstract Chat in ChatHistory rather than Friend

ChatHistory will be used by Groups for their history as well.
reviewable/pr6561/r50
Anthony Bilinski 3 years ago
parent
commit
de09bf655e
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
  1. 32
      src/model/chathistory.cpp
  2. 4
      src/model/chathistory.h

32
src/model/chathistory.cpp

@ -71,10 +71,10 @@ bool handleActionPrefix(QString& content)
} }
} // namespace } // namespace
ChatHistory::ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& coreIdHandler_, ChatHistory::ChatHistory(Chat& chat_, History* history_, const ICoreIdHandler& coreIdHandler_,
const Settings& settings_, IMessageDispatcher& messageDispatcher, const Settings& settings_, IMessageDispatcher& messageDispatcher,
FriendList& friendList) FriendList& friendList)
: f(f_) : chat(chat_)
, history(history_) , history(history_)
, settings(settings_) , settings(settings_)
, coreIdHandler(coreIdHandler_) , 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 // If the double disk access is real bad we can optimize this by adding
// another function to history // another function to history
auto dateWherePhraseFound = auto dateWherePhraseFound =
history->getDateWhereFindPhrase(f.getPublicKey(), earliestMessageDate, phrase, history->getDateWhereFindPhrase(chat.getPersistentId(), earliestMessageDate, phrase,
parameter); parameter);
auto loadIdx = history->getNumMessagesForChatBeforeDate(f.getPublicKey(), dateWherePhraseFound); auto loadIdx = history->getNumMessagesForChatBeforeDate(chat.getPersistentId(), dateWherePhraseFound);
loadHistoryIntoSessionChatLog(ChatLogIdx(loadIdx)); loadHistoryIntoSessionChatLog(ChatLogIdx(loadIdx));
// Reset search pos to the message we just loaded to avoid a double search // Reset search pos to the message we just loaded to avoid a double search
@ -189,7 +189,7 @@ std::vector<IChatLog::DateChatLogIdxPair> ChatHistory::getDateIdxs(const QDate&
size_t maxDates) const size_t maxDates) const
{ {
if (canUseHistory()) { if (canUseHistory()) {
auto counts = history->getNumMessagesForChatBeforeDateBoundaries(f.getPublicKey(), auto counts = history->getNumMessagesForChatBeforeDateBoundaries(chat.getPersistentId(),
startDate, maxDates); startDate, maxDates);
std::vector<IChatLog::DateChatLogIdxPair> ret; std::vector<IChatLog::DateChatLogIdxPair> ret;
@ -211,7 +211,7 @@ std::vector<IChatLog::DateChatLogIdxPair> ChatHistory::getDateIdxs(const QDate&
void ChatHistory::addSystemMessage(const SystemMessage& message) void ChatHistory::addSystemMessage(const SystemMessage& message)
{ {
if (canUseHistory()) { if (canUseHistory()) {
history->addNewSystemMessage(f.getPublicKey(), message); history->addNewSystemMessage(chat.getPersistentId(), message);
} }
sessionChatLog.addSystemMessage(message); sessionChatLog.addSystemMessage(message);
@ -224,13 +224,13 @@ void ChatHistory::onFileUpdated(const ToxPk& sender, const ToxFile& file)
switch (file.status) { switch (file.status) {
case ToxFile::INITIALIZING: { case ToxFile::INITIALIZING: {
auto selfPk = coreIdHandler.getSelfPublicKey(); 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 // 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 // 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 // initializing. If this is changed in the session chat log we'll end up
// with a different order when loading from history // 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, file.filePath, file.progress.getFileSize(), sender,
QDateTime::currentDateTime(), username); QDateTime::currentDateTime(), username);
break; break;
@ -267,14 +267,14 @@ void ChatHistory::onFileTransferBrokenUnbroken(const ToxPk& sender, const ToxFil
void ChatHistory::onMessageReceived(const ToxPk& sender, const Message& message) void ChatHistory::onMessageReceived(const ToxPk& sender, const Message& message)
{ {
if (canUseHistory()) { if (canUseHistory()) {
auto friendPk = f.getPublicKey(); auto& chatId = chat.getPersistentId();
auto displayName = f.getDisplayedName(); auto displayName = chat.getDisplayedName(sender);
auto content = message.content; auto content = message.content;
if (message.isAction) { if (message.isAction) {
content = ChatForm::ACTION_PREFIX + content; 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); sessionChatLog.onMessageReceived(sender, message);
@ -284,7 +284,7 @@ void ChatHistory::onMessageSent(DispatchedMessageId id, const Message& message)
{ {
if (canUseHistory()) { if (canUseHistory()) {
auto selfPk = coreIdHandler.getSelfPublicKey(); auto selfPk = coreIdHandler.getSelfPublicKey();
auto friendPk = f.getPublicKey(); auto& chatId = chat.getPersistentId();
auto content = message.content; auto content = message.content;
if (message.isAction) { if (message.isAction) {
@ -295,7 +295,7 @@ void ChatHistory::onMessageSent(DispatchedMessageId id, const Message& message)
auto onInsertion = [this, id](RowId historyId) { handleDispatchedMessage(id, historyId); }; 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); 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 // We know that both history and us have a start index of 0 so the type
// conversion should be safe // conversion should be safe
assert(getFirstIdx() == ChatLogIdx(0)); 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<int>(end.get() - start.get())); assert(messages.size() == static_cast<int>(end.get() - start.get()));
ChatLogIdx nextIdx = start; ChatLogIdx nextIdx = start;
@ -425,7 +425,7 @@ void ChatHistory::loadHistoryIntoSessionChatLog(ChatLogIdx start) const
*/ */
void ChatHistory::dispatchUnsentMessages(IMessageDispatcher& messageDispatcher) void ChatHistory::dispatchUnsentMessages(IMessageDispatcher& messageDispatcher)
{ {
auto unsentMessages = history->getUndeliveredMessagesForChat(f.getPublicKey()); auto unsentMessages = history->getUndeliveredMessagesForChat(chat.getPersistentId());
auto requiredExtensions = std::accumulate( auto requiredExtensions = std::accumulate(
unsentMessages.begin(), unsentMessages.end(), unsentMessages.begin(), unsentMessages.end(),
@ -521,7 +521,7 @@ bool ChatHistory::canUseHistory() const
ChatLogIdx ChatHistory::getInitialChatLogIdx() const ChatLogIdx ChatHistory::getInitialChatLogIdx() const
{ {
if (canUseHistory()) { if (canUseHistory()) {
return ChatLogIdx(history->getNumMessagesForChat(f.getPublicKey())); return ChatLogIdx(history->getNumMessagesForChat(chat.getPersistentId()));
} }
return ChatLogIdx(0); return ChatLogIdx(0);
} }

4
src/model/chathistory.h

@ -33,7 +33,7 @@ class ChatHistory : public IChatLog
{ {
Q_OBJECT Q_OBJECT
public: public:
ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& coreIdHandler_, ChatHistory(Chat& chat_, History* history_, const ICoreIdHandler& coreIdHandler_,
const Settings& settings_, IMessageDispatcher& messageDispatcher, const Settings& settings_, IMessageDispatcher& messageDispatcher,
FriendList& friendList); FriendList& friendList);
const ChatLogItem& at(ChatLogIdx idx) const override; const ChatLogItem& at(ChatLogIdx idx) const override;
@ -68,7 +68,7 @@ private:
bool canUseHistory() const; bool canUseHistory() const;
ChatLogIdx getInitialChatLogIdx() const; ChatLogIdx getInitialChatLogIdx() const;
Friend& f; Chat& chat;
History* history; History* history;
const Settings& settings; const Settings& settings;
const ICoreIdHandler& coreIdHandler; const ICoreIdHandler& coreIdHandler;

Loading…
Cancel
Save