fix(@desktop/cpp): Implement StatusGoQt::sendMessage() function

Issue #7848
This commit is contained in:
Michal Iskierko 2022-11-18 18:25:17 +01:00 committed by Michał Iskierko
parent e2697ae5aa
commit d57f493443
11 changed files with 169 additions and 4 deletions

View File

@ -24,6 +24,7 @@ public:
Q_INVOKABLE void init(const QString& sectionId); Q_INVOKABLE void init(const QString& sectionId);
Q_INVOKABLE void setCurrentChatIndex(int index); Q_INVOKABLE void setCurrentChatIndex(int index);
Q_INVOKABLE void sendMessage(const QString &message) const;
signals: signals:
void chatsModelChanged(); void chatsModelChanged();

View File

@ -8,6 +8,7 @@ Item {
id: root id: root
required property var selectedChat required property var selectedChat
required property var chatSectionController
ColumnLayout { ColumnLayout {
anchors.left: parent.left anchors.left: parent.left
@ -37,5 +38,18 @@ Item {
Label { Label {
text: "is muted: %1".arg(root.selectedChat.muted) text: "is muted: %1".arg(root.selectedChat.muted)
} }
Row {
TextField {
id: chatInput
width: root.width / 2
}
Button {
id: sendMessageButton
text: "Send"
onClicked: chatSectionController.sendMessage(chatInput.text)
}
}
} }
} }

View File

@ -46,6 +46,7 @@ PanelAndContentBase {
Layout.fillHeight: true Layout.fillHeight: true
selectedChat: chatSectionController.currentChat selectedChat: chatSectionController.currentChat
chatSectionController: chatSectionController
} }
} }
} }

View File

@ -1,5 +1,9 @@
#include "Status/ChatSection/ChatSectionController.h" #include "Status/ChatSection/ChatSectionController.h"
#include <StatusGo/Messages/InputMessage.h>
#include <StatusGo/Messages/MessagesApi.h>
#include <StatusGo/Metadata/api_response.h>
using namespace Status::ChatSection; using namespace Status::ChatSection;
ChatSectionController::ChatSectionController() ChatSectionController::ChatSectionController()
@ -39,3 +43,18 @@ void ChatSectionController::setCurrentChatIndex(int index)
m_currentChat = chat; m_currentChat = chat;
emit currentChatChanged(); emit currentChatChanged();
} }
void ChatSectionController::sendMessage(const QString &message) const
{
namespace Messages = StatusGo::Messages;
auto chatMessage = Messages::InputMessage::createTextMessage(message, m_currentChat->id());
try {
Messages::sendMessage(chatMessage);
}
catch(const StatusGo::CallPrivateRpcError& rpcError)
{
qWarning() << "Can't send message " << message
<< " to id " << m_currentChat->id()
<< ", error: " << rpcError.errorResponse().error.message.c_str();
}
}

View File

@ -106,6 +106,12 @@ target_sources(${PROJECT_NAME}
src/StatusGo/Chat/ChatDto.h src/StatusGo/Chat/ChatDto.h
src/StatusGo/Chat/ChatDto.cpp src/StatusGo/Chat/ChatDto.cpp
src/StatusGo/Messages/InputMessage.h
src/StatusGo/Messages/InputMessage.cpp
src/StatusGo/Messages/MessagesApi.h
src/StatusGo/Messages/MessagesApi.cpp
src/StatusGo/Messages/MessageDto.h
src/StatusGo/Messenger/Service.h src/StatusGo/Messenger/Service.h
src/StatusGo/Messenger/Service.cpp src/StatusGo/Messenger/Service.cpp

View File

@ -0,0 +1,31 @@
#include "InputMessage.h"
#include <Helpers/JsonMacros.h>
#include <Helpers/conversions.h>
using namespace Status::StatusGo;
Messages::InputMessage Messages::InputMessage::createTextMessage(const QString &message, const QString &chatId)
{
return {message, chatId, ContentType::Text};
}
void Messages::to_json(json &j, const InputMessage &d)
{
j = {
{"chatId", d.chatId},
{"text", d.messageText},
{"responseTo", d.replyTo},
{"ensName", d.ensName},
{"contentType", d.contentType},
};
}
void Messages::from_json(const json &j, InputMessage &d)
{
STATUS_READ_NLOHMAN_JSON_PROPERTY(chatId)
STATUS_READ_NLOHMAN_JSON_PROPERTY(messageText, "text")
STATUS_READ_NLOHMAN_JSON_PROPERTY(replyTo, "responseTo")
STATUS_READ_NLOHMAN_JSON_PROPERTY(ensName)
STATUS_READ_NLOHMAN_JSON_PROPERTY(contentType)
}

View File

@ -0,0 +1,28 @@
#pragma once
#include "MessageDto.h"
#include <nlohmann/json.hpp>
#include <QString>
using json = nlohmann::json;
namespace Status::StatusGo::Messages
{
struct InputMessage
{
QString messageText;
QString chatId;
ContentType contentType = ContentType::Unknown;
QString replyTo; // Id of the message that we are replying to
QString ensName; // Ens name of the sender
static InputMessage createTextMessage(const QString &message, const QString &chatId);
};
void to_json(json& j, const InputMessage& d);
void from_json(const json& j, InputMessage& d);
} // namespace Status::StatusGo::Messages

View File

@ -0,0 +1,25 @@
#pragma once
namespace Status::StatusGo::Messages
{
/// @see status-go's protocol/protobuf/chat_message.proto ContentType
enum class ContentType
{
Unknown = 0,
Text = 1,
Sticker = 2,
Status = 3,
Emoji = 4,
TransactionCommand = 5,
// 6 - private
Image = 7,
Audio = 8,
Community = 9,
// 10 - private
ContactRequest = 11,
DiscordMessage = 12,
IdentityVerification = 13
};
} // namespace Status::StatusGo::Messages

View File

@ -0,0 +1,21 @@
#include "MessagesApi.h"
#include "Metadata/api_response.h"
#include "InputMessage.h"
#include "Utils.h"
#include <Helpers/conversions.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace Status::StatusGo;
void Messages::sendMessage(const InputMessage &message)
{
std::vector<json> params{message};
json inputJson = {{"jsonrpc", "2.0"}, {"method", "wakuext_sendChatMessage"}, {"params", params}};
const auto result = Utils::statusGoCallPrivateRPC(inputJson.dump().c_str());
const auto resultJson = json::parse(result);
checkPrivateRpcCallResultAndReportError(resultJson);
}

View File

@ -0,0 +1,10 @@
#pragma once
namespace Status::StatusGo::Messages
{
class InputMessage;
/// \brief Sends chat message
void sendMessage(const InputMessage &message);
} // namespace Status::StatusGo::Messages

View File

@ -1,11 +1,13 @@
#include <StatusGo/Messenger/Service.h>
#include <Onboarding/Accounts/AccountsService.h> #include <Onboarding/Accounts/AccountsService.h>
#include <StatusGo/SignalsManager.h>
#include <ScopedTestAccount.h> #include <ScopedTestAccount.h>
#include <StatusGo/Messenger/Service.h>
#include <StatusGo/Messages/InputMessage.h>
#include <StatusGo/Messages/MessagesApi.h>
#include <StatusGo/Metadata/api_response.h>
#include <StatusGo/SignalsManager.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -36,4 +38,11 @@ TEST(MessagingApi, TestStartMessaging)
ASSERT_TRUE(nodeReady); ASSERT_TRUE(nodeReady);
} }
/// Simple test to validate sendChatMessage rpc function. \todo Expand it later.
TEST(MessagingApi, TestSendMessage)
{
const auto message = StatusGo::Messages::InputMessage::createTextMessage("Hello Status", "someChatId");
EXPECT_THROW( StatusGo::Messages::sendMessage(message), StatusGo::CallPrivateRpcError );
}
} // namespace } // namespace