diff --git a/src/model/chatlogitem.cpp b/src/model/chatlogitem.cpp index a755d8d2e..07d67154b 100644 --- a/src/model/chatlogitem.cpp +++ b/src/model/chatlogitem.cpp @@ -18,9 +18,6 @@ */ #include "chatlogitem.h" -#include "src/core/core.h" -#include "src/friendlist.h" -#include "src/grouplist.h" #include "src/model/friend.h" #include "src/model/group.h" @@ -39,56 +36,23 @@ struct ChatLogItemDeleter delete static_cast(ptr); } }; - -QString resolveToxPk(const ToxPk& pk) -{ - Friend* f = FriendList::findFriend(pk); - if (f) { - return f->getDisplayedName(); - } - - for (Group* it : GroupList::getAllGroups()) { - QString res = it->resolveToxId(pk); - if (!res.isEmpty()) { - return res; - } - } - - return pk.toString(); -} - -QString resolveSenderNameFromSender(const ToxPk& sender) -{ - // TODO: Remove core instance - const Core* core = Core::getInstance(); - - // In unit tests we don't have a core instance so we just stringize the key - if (!core) { - return sender.toString(); - } - - bool isSelf = sender == core->getSelfId().getPublicKey(); - QString myNickName = core->getUsername().isEmpty() ? sender.toString() : core->getUsername(); - - return isSelf ? myNickName : resolveToxPk(sender); -} } // namespace -ChatLogItem::ChatLogItem(ToxPk sender_, ChatLogFile file_) - : ChatLogItem(std::move(sender_), ContentType::fileTransfer, +ChatLogItem::ChatLogItem(ToxPk sender_, const QString& displayName, ChatLogFile file_) + : ChatLogItem(std::move(sender_), displayName, ContentType::fileTransfer, ContentPtr(new ChatLogFile(std::move(file_)), ChatLogItemDeleter::doDelete)) {} -ChatLogItem::ChatLogItem(ToxPk sender_, ChatLogMessage message_) - : ChatLogItem(sender_, ContentType::message, +ChatLogItem::ChatLogItem(ToxPk sender_, const QString& displayName, ChatLogMessage message_) + : ChatLogItem(sender_, displayName, ContentType::message, ContentPtr(new ChatLogMessage(std::move(message_)), ChatLogItemDeleter::doDelete)) {} -ChatLogItem::ChatLogItem(ToxPk sender_, ContentType contentType_, ContentPtr content_) +ChatLogItem::ChatLogItem(ToxPk sender_, const QString& displayName_, ContentType contentType_, ContentPtr content_) : sender(std::move(sender_)) - , displayName(resolveSenderNameFromSender(sender)) + , displayName(displayName_) , contentType(contentType_) , content(std::move(content_)) {} diff --git a/src/model/chatlogitem.h b/src/model/chatlogitem.h index 96c748001..c0e75f58e 100644 --- a/src/model/chatlogitem.h +++ b/src/model/chatlogitem.h @@ -50,8 +50,8 @@ public: fileTransfer, }; - ChatLogItem(ToxPk sender, ChatLogFile file); - ChatLogItem(ToxPk sender, ChatLogMessage message); + ChatLogItem(ToxPk sender, const QString& displayName, ChatLogFile file); + ChatLogItem(ToxPk sender, const QString& displayName, ChatLogMessage message); const ToxPk& getSender() const; ContentType getContentType() const; ChatLogFile& getContentAsFile(); @@ -63,7 +63,7 @@ public: const QString& getDisplayName() const; private: - ChatLogItem(ToxPk sender, ContentType contentType, ContentPtr content); + ChatLogItem(ToxPk sender, const QString &displayName_, ContentType contentType, ContentPtr content); ToxPk sender; QString displayName; diff --git a/src/model/sessionchatlog.cpp b/src/model/sessionchatlog.cpp index 04fc8b121..204b7522e 100644 --- a/src/model/sessionchatlog.cpp +++ b/src/model/sessionchatlog.cpp @@ -19,6 +19,7 @@ #include "sessionchatlog.h" #include "src/friendlist.h" +#include "src/grouplist.h" #include #include @@ -105,6 +106,23 @@ firstItemAfterDate(QDate date, const std::map& items) return a.timestamp.date() < b.timestamp.date(); }); } + +QString resolveToxPk(const ToxPk& pk) +{ + Friend* f = FriendList::findFriend(pk); + if (f) { + return f->getDisplayedName(); + } + + for (Group* it : GroupList::getAllGroups()) { + QString res = it->resolveToxId(pk); + if (!res.isEmpty()) { + return res; + } + } + + return pk.toString(); +} } // namespace SessionChatLog::SessionChatLog(const ICoreIdHandler& coreIdHandler) @@ -121,6 +139,14 @@ SessionChatLog::SessionChatLog(ChatLogIdx initialIdx, const ICoreIdHandler& core SessionChatLog::~SessionChatLog() = default; +QString SessionChatLog::resolveSenderNameFromSender(const ToxPk& sender) +{ + bool isSelf = sender == coreIdHandler.getSelfPublicKey(); + QString myNickName = coreIdHandler.getUsername().isEmpty() ? sender.toString() : coreIdHandler.getUsername(); + + return isSelf ? myNickName : resolveToxPk(sender); +} + const ChatLogItem& SessionChatLog::at(ChatLogIdx idx) const { auto item = items.find(idx); @@ -293,29 +319,21 @@ std::vector SessionChatLog::getDateIdxs(const QDat return ret; } -void SessionChatLog::insertCompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName, +void SessionChatLog::insertCompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, QString senderName, const ChatLogMessage& message) { - auto item = ChatLogItem(sender, message); - - if (!senderName.isEmpty()) { - item.setDisplayName(senderName); - } + auto item = ChatLogItem(sender, senderName, message); assert(message.state == MessageState::complete); items.emplace(idx, std::move(item)); } -void SessionChatLog::insertIncompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName, +void SessionChatLog::insertIncompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, QString senderName, const ChatLogMessage& message, DispatchedMessageId dispatchId) { - auto item = ChatLogItem(sender, message); - - if (!senderName.isEmpty()) { - item.setDisplayName(senderName); - } + auto item = ChatLogItem(sender, senderName, message); assert(message.state == MessageState::pending); @@ -323,27 +341,19 @@ void SessionChatLog::insertIncompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& s outgoingMessages.insert(dispatchId, idx); } -void SessionChatLog::insertBrokenMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName, +void SessionChatLog::insertBrokenMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, QString senderName, const ChatLogMessage& message) { - auto item = ChatLogItem(sender, message); - - if (!senderName.isEmpty()) { - item.setDisplayName(senderName); - } + auto item = ChatLogItem(sender, senderName, message); assert(message.state == MessageState::broken); items.emplace(idx, std::move(item)); } -void SessionChatLog::insertFileAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName, const ChatLogFile& file) +void SessionChatLog::insertFileAtIdx(ChatLogIdx idx, const ToxPk& sender, QString senderName, const ChatLogFile& file) { - auto item = ChatLogItem(sender, file); - - if (!senderName.isEmpty()) { - item.setDisplayName(senderName); - } + auto item = ChatLogItem(sender, senderName, file); items.emplace(idx, std::move(item)); } @@ -359,7 +369,7 @@ void SessionChatLog::onMessageReceived(const ToxPk& sender, const Message& messa ChatLogMessage chatLogMessage; chatLogMessage.state = MessageState::complete; chatLogMessage.message = message; - items.emplace(messageIdx, ChatLogItem(sender, chatLogMessage)); + items.emplace(messageIdx, ChatLogItem(sender, resolveSenderNameFromSender(sender), chatLogMessage)); emit this->itemUpdated(messageIdx); } @@ -375,7 +385,9 @@ void SessionChatLog::onMessageSent(DispatchedMessageId id, const Message& messag ChatLogMessage chatLogMessage; chatLogMessage.state = MessageState::pending; chatLogMessage.message = message; - items.emplace(messageIdx, ChatLogItem(coreIdHandler.getSelfPublicKey(), chatLogMessage)); + const ToxPk selfPk = coreIdHandler.getSelfPublicKey(); + const QString selfName = resolveSenderNameFromSender(selfPk); + items.emplace(messageIdx, ChatLogItem(selfPk, selfName, chatLogMessage)); outgoingMessages.insert(id, messageIdx); @@ -428,7 +440,7 @@ void SessionChatLog::onFileUpdated(const ToxPk& sender, const ToxFile& file) currentFileTransfers.push_back(currentTransfer); const auto chatLogFile = ChatLogFile{QDateTime::currentDateTime(), file}; - items.emplace(currentTransfer.idx, ChatLogItem(sender, chatLogFile)); + items.emplace(currentTransfer.idx, ChatLogItem(sender, resolveSenderNameFromSender(sender), chatLogFile)); messageIdx = currentTransfer.idx; } else if (fileIt != currentFileTransfers.end()) { messageIdx = fileIt->idx; diff --git a/src/model/sessionchatlog.h b/src/model/sessionchatlog.h index 95f67a18b..96bcfb321 100644 --- a/src/model/sessionchatlog.h +++ b/src/model/sessionchatlog.h @@ -45,13 +45,13 @@ public: ChatLogIdx getNextIdx() const override; std::vector getDateIdxs(const QDate& startDate, size_t maxDates) const override; - void insertCompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName, + void insertCompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, QString senderName, const ChatLogMessage& message); - void insertIncompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName, + void insertIncompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, QString senderName, const ChatLogMessage& message, DispatchedMessageId dispatchId); - void insertBrokenMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName, + void insertBrokenMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, QString senderName, const ChatLogMessage& message); - void insertFileAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName, const ChatLogFile& file); + void insertFileAtIdx(ChatLogIdx idx, const ToxPk& sender, QString senderName, const ChatLogFile& file); public slots: void onMessageReceived(const ToxPk& sender, const Message& message); @@ -62,6 +62,10 @@ public slots: void onFileTransferRemotePausedUnpaused(const ToxPk& sender, const ToxFile& file, bool paused); void onFileTransferBrokenUnbroken(const ToxPk& sender, const ToxFile& file, bool broken); +private: + QString resolveSenderNameFromSender(const ToxPk &sender); + + private: const ICoreIdHandler& coreIdHandler; diff --git a/test/model/sessionchatlog_test.cpp b/test/model/sessionchatlog_test.cpp index a1af35530..248a1946a 100644 --- a/test/model/sessionchatlog_test.cpp +++ b/test/model/sessionchatlog_test.cpp @@ -24,6 +24,8 @@ #include namespace { +static const QString TEST_USERNAME = "qTox Tester #1"; + Message createMessage(const QString& content) { Message message; @@ -50,8 +52,7 @@ public: QString getUsername() const override { - std::terminate(); - return QString(); + return TEST_USERNAME; } }; } // namespace