From 161b5dbbf43d505b727e0ed3cae15458a72147f9 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 8 May 2018 18:36:51 +0200 Subject: Only allow owner of message to delete it --- depends/odhtdb | 2 +- include/Channel.hpp | 4 ++-- include/MessageBoard.hpp | 3 ++- src/Channel.cpp | 8 ++++---- src/MessageBoard.cpp | 24 +++++++++++++++++++----- src/main.cpp | 2 +- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/depends/odhtdb b/depends/odhtdb index e3b6538..95f683e 160000 --- a/depends/odhtdb +++ b/depends/odhtdb @@ -1 +1 @@ -Subproject commit e3b6538c17884b5985a78e3d7c507d02b7af2e78 +Subproject commit 95f683eae7a004860eae2bca43f56f98b769cf80 diff --git a/include/Channel.hpp b/include/Channel.hpp index 7d4cb08..93f3725 100644 --- a/include/Channel.hpp +++ b/include/Channel.hpp @@ -47,8 +47,8 @@ namespace dchat void addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds = 0); void addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds, const odhtdb::Hash &id); void addMessage(const std::string &msg); - void deleteLocalMessage(const odhtdb::Hash &id); - void deleteMessage(const odhtdb::Hash &id); + void deleteLocalMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser); + void deleteMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser); void addUserLocally(User *user); bool addUser(const odhtdb::Signature::PublicKey &userId, const std::string &groupId); diff --git a/include/MessageBoard.hpp b/include/MessageBoard.hpp index a6867d2..9025e48 100644 --- a/include/MessageBoard.hpp +++ b/include/MessageBoard.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -26,7 +27,7 @@ namespace dchat private: void updateStaticContentTexture(const sf::Vector2u &newSize); void addMessage(Message *message, const odhtdb::Hash &id); - void deleteMessage(const odhtdb::Hash &id); + void deleteMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser); void drawDefault(sf::RenderWindow &window, Cache &cache); void drawSimple(sf::RenderWindow &window, Cache &cache); diff --git a/src/Channel.cpp b/src/Channel.cpp index 678dddd..ed037a4 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -111,12 +111,12 @@ namespace dchat addLocalMessage(msg, localUser, 0, odhtdb::Hash()); } - void Channel::deleteLocalMessage(const odhtdb::Hash &id) + void Channel::deleteLocalMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser) { - messageBoard.deleteMessage(id); + messageBoard.deleteMessage(id, requestedByUser); } - void Channel::deleteMessage(const odhtdb::Hash &id) + void Channel::deleteMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser) { if(database && localUser->type == User::Type::ONLINE) { @@ -131,7 +131,7 @@ namespace dchat database->commit(); } else - deleteLocalMessage(id); + deleteLocalMessage(id, requestedByUser); } void Channel::addUserLocally(User *user) diff --git a/src/MessageBoard.cpp b/src/MessageBoard.cpp index bdaccf3..f2796e5 100644 --- a/src/MessageBoard.cpp +++ b/src/MessageBoard.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include using namespace std; @@ -76,11 +77,20 @@ namespace dchat scrollToBottom = true; } - void MessageBoard::deleteMessage(const odhtdb::Hash &id) + void MessageBoard::deleteMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser) { lock_guard lock(messageProcessMutex); auto it = messageIdMap.find(id); if(it == messageIdMap.end()) return; + if(it->second->user->type == User::Type::ONLINE) + { + auto onlineUser = static_cast(it->second->user); + if(onlineUser->databaseUser->getPublicKey() != requestedByUser) + { + fprintf(stderr, "Warning: user %s requested to delete a message owned by user %s, ignoring request\n", requestedByUser.toString().c_str(), onlineUser->databaseUser->getPublicKey().toString().c_str()); + return; + } + } for(usize i = 0; i < messages.size(); ++i) { @@ -282,20 +292,24 @@ namespace dchat message->text.processEvent(event); } - if(event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Button::Right) + OnlineUser *localOnlineUser = nullptr; + if(channel->getLocalUser()->type == User::Type::ONLINE) + localOnlineUser = static_cast(channel->getLocalUser()); + + if(localOnlineUser && event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Button::Right) { for(auto it : messageIdMap) { it.second->text.processEvent(event); auto textPos = it.second->text.getPosition(); - if(event.mouseButton.x >= textPos.x && event.mouseButton.x <= textPos.x + it.second->text.getMaxWidth() && event.mouseButton.y >= textPos.y && event.mouseButton.y <= textPos.y + it.second->text.getHeight()) + if(it.second->user == channel->getLocalUser() && event.mouseButton.x >= textPos.x && event.mouseButton.x <= textPos.x + it.second->text.getMaxWidth() && event.mouseButton.y >= textPos.y && event.mouseButton.y <= textPos.y + it.second->text.getHeight()) { auto contextMenu = GlobalContextMenu::getEditMessageContextMenu(); contextMenu->setPosition(sf::Vector2f(event.mouseButton.x, event.mouseButton.y)); contextMenu->setVisible(true); - GlobalContextMenu::setClickDeleteMessageCallbackFunc([this, it](ContextMenuItem *menuItem) + GlobalContextMenu::setClickDeleteMessageCallbackFunc([this, it, localOnlineUser](ContextMenuItem *menuItem) { - channel->deleteMessage(it.first); + channel->deleteMessage(it.first, localOnlineUser->databaseUser->getPublicKey()); GlobalContextMenu::setClickDeleteMessageCallbackFunc(nullptr); }); return; diff --git a/src/main.cpp b/src/main.cpp index 83ef11a..910724e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -102,7 +102,7 @@ void channelAddStoredMessage(Channel *channel, const odhtdb::Hash &requestHash, sibs::SafeDeserializer deserializer((const u8*)decryptedData.data, decryptedData.size); odhtdb::Hash messageId; deserializer.extract((u8*)messageId.getData(), odhtdb::HASH_BYTE_SIZE); - channel->deleteLocalMessage(messageId); + channel->deleteLocalMessage(messageId, creatorPublicKey); break; } case ChannelDataType::NICKNAME_CHANGE: -- cgit v1.2.3