From c633d67f627c23510681842f03522425db91fb91 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 30 May 2018 14:54:40 +0200 Subject: Do not get notification if loading messages from cache, or if message is not latest --- depends/odhtdb | 2 +- include/Channel.hpp | 1 + include/MessageBoard.hpp | 2 ++ src/Channel.cpp | 5 +++++ src/MessageBoard.cpp | 7 +++++++ src/main.cpp | 10 ++++++---- 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/depends/odhtdb b/depends/odhtdb index 0ce0eaf..9ef0241 160000 --- a/depends/odhtdb +++ b/depends/odhtdb @@ -1 +1 @@ -Subproject commit 0ce0eaf93dea198a6b0f812c724be9bad74270b2 +Subproject commit 9ef02410fda13bffd6ff2fc7172d19d1f7c25a94 diff --git a/include/Channel.hpp b/include/Channel.hpp index 5f5699e..af6cbf2 100644 --- a/include/Channel.hpp +++ b/include/Channel.hpp @@ -47,6 +47,7 @@ namespace dchat const std::vector getUsers() const; OnlineUser* getUserByPublicKey(const odhtdb::Signature::PublicKey &publicKey); const odhtdb::DatabaseNode& getNodeInfo() const; + Message* getLatestMessage(); // If timestamp is 0, then current time is used void addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds = 0); diff --git a/include/MessageBoard.hpp b/include/MessageBoard.hpp index 861d195..acdaad4 100644 --- a/include/MessageBoard.hpp +++ b/include/MessageBoard.hpp @@ -24,6 +24,8 @@ namespace dchat void processEvent(const sf::Event &event, Cache &cache); void draw(sf::RenderWindow &window, Cache &cache); + + Message* getLatestMessage(); private: usize findPositionToInsertMessageByTimestamp(Message *message); diff --git a/src/Channel.cpp b/src/Channel.cpp index adcb9be..75d805c 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -118,6 +118,11 @@ namespace dchat return databaseNodeInfo; } + Message* Channel::getLatestMessage() + { + return messageBoard.getLatestMessage(); + } + void Channel::addLocalMessage(const string &msg, User *owner, u64 timestampSeconds) { addLocalMessage(msg, owner, timestampSeconds, odhtdb::Hash()); diff --git a/src/MessageBoard.cpp b/src/MessageBoard.cpp index 3e6532c..74afab8 100644 --- a/src/MessageBoard.cpp +++ b/src/MessageBoard.cpp @@ -442,4 +442,11 @@ namespace dchat //textureSprite.setPosition(backgroundPos); //window.draw(textureSprite); } + + Message* MessageBoard::getLatestMessage() + { + if(!messages.empty()) + return messages.back(); + return nullptr; + } } diff --git a/src/main.cpp b/src/main.cpp index bc46240..ef4c20f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -107,7 +107,7 @@ static void channelChangeChannelName(Channel *channel, const StringView data, co // We dont care if there is more data to read (malicious packet), we already got all the data we need } -static void channelAddStoredMessage(Channel *channel, const odhtdb::Hash &requestHash, const odhtdb::Signature::PublicKey &creatorPublicKey, const StringView decryptedObject, u64 timestamp) +static void channelAddStoredMessage(Channel *channel, const odhtdb::Hash &requestHash, const odhtdb::Signature::PublicKey &creatorPublicKey, const StringView decryptedObject, u64 timestamp, bool loadedFromCache) { User *user = channel->getUserByPublicKey(creatorPublicKey); if(!user) @@ -125,8 +125,10 @@ static void channelAddStoredMessage(Channel *channel, const odhtdb::Hash &reques case ChannelDataType::ADD_MESSAGE: { string msg(decryptedData.data, decryptedData.size); - channel->addLocalMessage(msg, user, ntp::NtpTimestamp::fromCombined(timestamp).seconds, requestHash); - if(!windowFocused && channel->getLocalUser()->isOnlineUser()) + Message *latestMessage = channel->getLatestMessage(); + auto timestampSeconds = ntp::NtpTimestamp::fromCombined(timestamp).seconds; + channel->addLocalMessage(msg, user, timestampSeconds, requestHash); + if(!loadedFromCache && !windowFocused && channel->getLocalUser()->isOnlineUser() && (!latestMessage || (latestMessage && timestampSeconds >= latestMessage->timestampSeconds))) { auto onlineLocalUser = static_cast(channel->getLocalUser()); if(creatorPublicKey != onlineLocalUser->getPublicKey()) @@ -284,7 +286,7 @@ int main(int argc, char **argv) { if(*request.nodeHash == *channel->getNodeInfo().getRequestHash()) { - channelAddStoredMessage(channel, *request.requestHash, *request.creatorPublicKey, StringView((const char*)request.decryptedData.data, request.decryptedData.size), request.timestamp); + channelAddStoredMessage(channel, *request.requestHash, *request.creatorPublicKey, StringView((const char*)request.decryptedData.data, request.decryptedData.size), request.timestamp, request.loadedFromCache); if(channel == Channel::getCurrent()) lastFocusedTimer.restart(); return; -- cgit v1.2.3