From fa03e232b7ae6d73f191d538ae185f25900a996b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Blin?= Date: Mon, 31 Oct 2022 10:23:37 -0400 Subject: [PATCH] swarm: add reaction support This is a pretty trivial patch as all the necessary logics are already supported by the deamon. A client can use sendMessage with flag = 2 to add a reaction. This only adds a "react-to" in the body of the message that the client can interpret. To remove a reaction, the client can use editMessage (and set the body to "" for the id of the reaction), also there is no limit on the content of the reaction and multiple reactions can be added to the same message. For non compatible clients, it will be shown as a simple text message as it's the same type. Change-Id: I7b13d32771109118b94ed17d0b918e66487e94bb --- src/client/conversation_interface.cpp | 2 + src/jamidht/conversation_module.cpp | 13 ++++++ src/jamidht/conversation_module.h | 3 ++ test/unitTest/conversation/conversation.cpp | 46 +++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/src/client/conversation_interface.cpp b/src/client/conversation_interface.cpp index f67f7f7b0..7feadfb48 100644 --- a/src/client/conversation_interface.cpp +++ b/src/client/conversation_interface.cpp @@ -178,6 +178,8 @@ sendMessage(const std::string& accountId, convModule->sendMessage(conversationId, message, commitId); } else if (flag == 1 /* message edition */) { convModule->editMessage(conversationId, message, commitId); + } else if (flag == 2 /* reaction */) { + convModule->reactToMessage(conversationId, message, commitId); } } } diff --git a/src/jamidht/conversation_module.cpp b/src/jamidht/conversation_module.cpp index 43773bad6..513075aad 100644 --- a/src/jamidht/conversation_module.cpp +++ b/src/jamidht/conversation_module.cpp @@ -1368,6 +1368,19 @@ ConversationModule::editMessage(const std::string& conversationId, pimpl_->editMessage(conversationId, newBody, editedId); } +void +ConversationModule::reactToMessage(const std::string& conversationId, + const std::string& newBody, + const std::string& reactToId) +{ + // Commit message edition + Json::Value json; + json["body"] = newBody; + json["react-to"] = reactToId; + json["type"] = "text/plain"; + pimpl_->sendMessage(conversationId, std::move(json)); +} + void ConversationModule::addCallHistoryMessage(const std::string& uri, uint64_t duration_ms) { diff --git a/src/jamidht/conversation_module.h b/src/jamidht/conversation_module.h index c45f8a57c..27f5bb4ce 100644 --- a/src/jamidht/conversation_module.h +++ b/src/jamidht/conversation_module.h @@ -167,6 +167,9 @@ public: void editMessage(const std::string& conversationId, const std::string& newBody, const std::string& editedId); + void reactToMessage(const std::string& conversationId, + const std::string& newBody, + const std::string& reactToId); /** * Add to the related conversation the call history message diff --git a/test/unitTest/conversation/conversation.cpp b/test/unitTest/conversation/conversation.cpp index 29991f629..4fadcd887 100644 --- a/test/unitTest/conversation/conversation.cpp +++ b/test/unitTest/conversation/conversation.cpp @@ -117,6 +117,7 @@ private: void testFixContactDetails(); void testRemoveOneToOneNotInDetails(); void testMessageEdition(); + void testMessageReaction(); CPPUNIT_TEST_SUITE(ConversationTest); CPPUNIT_TEST(testCreateConversation); @@ -164,6 +165,7 @@ private: CPPUNIT_TEST(testFixContactDetails); CPPUNIT_TEST(testRemoveOneToOneNotInDetails); CPPUNIT_TEST(testMessageEdition); + CPPUNIT_TEST(testMessageReaction); CPPUNIT_TEST_SUITE_END(); }; @@ -3518,6 +3520,50 @@ ConversationTest::testMessageEdition() CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&]() { return errorDetected; })); } +void +ConversationTest::testMessageReaction() +{ + auto aliceAccount = Manager::instance().getAccount(aliceId); + auto bobAccount = Manager::instance().getAccount(bobId); + auto bobUri = bobAccount->getUsername(); + std::mutex mtx; + std::unique_lock lk {mtx}; + std::condition_variable cv; + std::map> confHandlers; + std::vector> messageAliceReceived; + bool conversationReady = false; + confHandlers.insert(libjami::exportable_callback( + [&](const std::string& accountId, + const std::string& /* conversationId */, + std::map message) { + if (accountId == aliceId) + messageAliceReceived.emplace_back(message); + cv.notify_one(); + })); + confHandlers.insert(libjami::exportable_callback( + [&](const std::string& accountId, const std::string& /* conversationId */) { + if (accountId == aliceId) + conversationReady = true; + cv.notify_one(); + })); + libjami::registerSignalHandlers(confHandlers); + auto convId = libjami::startConversation(aliceId); + CPPUNIT_ASSERT(cv.wait_for(lk, 30s, [&]() { return conversationReady; })); + auto msgSize = messageAliceReceived.size(); + libjami::sendMessage(aliceId, convId, "hi"s, ""); + CPPUNIT_ASSERT( + cv.wait_for(lk, 30s, [&]() { return messageAliceReceived.size() == msgSize + 1; })); + msgSize = messageAliceReceived.size(); + + auto reactId = messageAliceReceived.rbegin()->at("id"); + + libjami::sendMessage(aliceId, convId, "👋"s, reactId, 2); + CPPUNIT_ASSERT( + cv.wait_for(lk, 10s, [&]() { return messageAliceReceived.size() == msgSize + 1; })); + CPPUNIT_ASSERT(messageAliceReceived.rbegin()->at("react-to") == reactId); + CPPUNIT_ASSERT(messageAliceReceived.rbegin()->at("body") == "👋"); +} + } // namespace test } // namespace jami