-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_plugin.cpp
More file actions
120 lines (98 loc) · 3.49 KB
/
Copy pathchat_plugin.cpp
File metadata and controls
120 lines (98 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "chat_plugin.h"
#include <QTimer>
#include <QDateTime>
#include <QDebug>
#include <QString>
#include "logos_api_client.h"
ChatPlugin::ChatPlugin() : currentRelayTopic("/waku/2/rs/16/32") {
}
ChatPlugin::~ChatPlugin() {
if (logos) {
delete logos;
logos = nullptr;
}
if (logosAPI) {
delete logosAPI;
logosAPI = nullptr;
}
}
bool ChatPlugin::ensureLogosContext(const char* caller) const {
const QString context = QString::fromLatin1(caller ? caller : "unknown");
if (!logosAPI) {
qWarning() << "ChatPlugin:" << context << "- LogosAPI not initialized";
return false;
}
if (!logos) {
qWarning() << "ChatPlugin:" << context << "- LogosModules not initialized";
return false;
}
return true;
}
bool ChatPlugin::initialize() {
if (!ensureLogosContext("initialize")) {
return false;
}
MessageCallback actualCallback = [this](const std::string& timestamp, const std::string& nick, const std::string& message) {
QVariantList data;
data << QString::fromStdString(timestamp) << QString::fromStdString(nick) << QString::fromStdString(message);
emitEvent(QStringLiteral("chatMessage"), data);
};
void* result = ::initAndStart(logosAPI, logos, currentRelayTopic, actualCallback);
return (result != nullptr);
}
bool ChatPlugin::joinChannel(const QString& channelName) {
if (!ensureLogosContext("joinChannel")) {
return false;
}
return ::joinChannel(logosAPI, logos, channelName.toStdString(), currentRelayTopic);
}
void ChatPlugin::sendMessage(const QString& channelName, const QString& username, const QString& message) {
if (!ensureLogosContext("sendMessage")) {
return;
}
std::cout << "ChatPlugin::sendMessage called with channelName: " << channelName.toStdString()
<< ", username: " << username.toStdString()
<< ", message: " << message.toStdString() << std::endl;
::sendMessage(logosAPI, logos, channelName.toStdString(), username.toStdString(), message.toStdString());
}
bool ChatPlugin::retrieveHistory(const std::string& channelName) {
if (!ensureLogosContext("retrieveHistory")) {
return false;
}
MessageCallback actualCallback = [this](const std::string& timestamp, const std::string& nick, const std::string& message) {
QVariantList data;
data << QString::fromStdString(timestamp) << QString::fromStdString(nick) << QString::fromStdString(message);
emitEvent(QStringLiteral("historyMessage"), data);
};
::retrieveHistory(logosAPI, logos, channelName, actualCallback);
return true;
}
bool ChatPlugin::retrieveHistory(const QString& channelName) {
return retrieveHistory(channelName.toStdString());
}
void ChatPlugin::initLogos(LogosAPI* logosAPIInstance) {
if (logos) {
delete logos;
logos = nullptr;
}
if (logosAPI) {
delete logosAPI;
logosAPI = nullptr;
}
logosAPI = logosAPIInstance;
if (logosAPI) {
logos = new LogosModules(logosAPI);
}
}
void ChatPlugin::emitEvent(const QString& eventName, const QVariantList& data) {
if (!logosAPI) {
qWarning() << "ChatPlugin: LogosAPI not available, cannot emit" << eventName;
return;
}
LogosAPIClient* client = logosAPI->getClient("chat");
if (!client) {
qWarning() << "ChatPlugin: Failed to get chat client for event" << eventName;
return;
}
client->onEventResponse(this, eventName, data);
}