Wavelength
Privacy-focused, cross-platform, and open-source communication application
Loading...
Searching...
No Matches
message_handler.h
Go to the documentation of this file.
1#ifndef MESSAGE_HANDLER_H
2#define MESSAGE_HANDLER_H
3
4#include <QJsonObject>
5#include <QObject>
6#include <QUuid>
7
8class QWebSocket;
9
17class MessageHandler final : public QObject {
18 Q_OBJECT
19
20public:
26 static MessageHandler instance;
27 return &instance;
28 }
29
34 static QString GenerateMessageId() {
35 return QUuid::createUuid().toString(QUuid::WithoutBraces);
36 }
37
47 static bool SendSystemCommand(QWebSocket *socket, const QString &command,
48 const QJsonObject &params = QJsonObject());
49
55 bool IsMessageProcessed(const QString &message_id) const {
56 return !message_id.isEmpty() && processed_message_ids_.contains(message_id);
57 }
58
64 void MarkMessageAsProcessed(const QString &message_id);
65
72 static QJsonObject ParseMessage(const QString &message, bool *ok = nullptr);
73
79 static QString GetMessageType(const QJsonObject &message_object) {
80 return message_object["type"].toString();
81 }
82
88 static QString GetMessageContent(const QJsonObject &message_object) {
89 return message_object["content"].toString();
90 }
91
97 static QString GetMessageFrequency(const QJsonObject &message_object) {
98 return message_object["frequency"].toString();
99 }
100
106 static QString GetMessageSenderId(const QJsonObject &message_object) {
107 return message_object["senderId"].toString();
108 }
109
115 static QString GetMessageId(const QJsonObject &message_object) {
116 return message_object["messageId"].toString();
117 }
118
126 static QJsonObject CreateAuthRequest(const QString &frequency, const QString &password, const QString &client_id);
127
136 static QJsonObject CreateRegisterRequest(const QString &frequency, bool is_password_protected,
137 const QString &password, const QString &host_id);
138
145 static QJsonObject CreateLeaveRequest(const QString &frequency, bool is_host);
146
153
154private:
159 explicit MessageHandler(QObject *parent = nullptr) : QObject(parent) {
160 }
161
165 ~MessageHandler() override = default;
166
171
176
180 static constexpr int kMaxCachedMessageIds = 200;
181};
182
183#endif // MESSAGE_HANDLER_H
void ClearProcessedMessages()
Clears the internal cache of processed message IDs.
Definition message_handler.h:150
static QString GenerateMessageId()
Generates a unique message identifier using UUID.
Definition message_handler.h:34
MessageHandler(const MessageHandler &)=delete
Deleted copy constructor to prevent copying.
~MessageHandler() override=default
Private destructor.
bool IsMessageProcessed(const QString &message_id) const
Checks if a message with the given ID has already been processed.
Definition message_handler.h:55
static QString GetMessageType(const QJsonObject &message_object)
Extracts the message type string from a parsed message object.
Definition message_handler.h:79
static QString GetMessageFrequency(const QJsonObject &message_object)
Extracts the frequency string from a parsed message object.
Definition message_handler.h:97
static QJsonObject CreateLeaveRequest(const QString &frequency, bool is_host)
Creates a JSON object representing a request to leave or close a frequency.
Definition message_handler.cpp:72
static QJsonObject CreateAuthRequest(const QString &frequency, const QString &password, const QString &client_id)
Creates a JSON object representing an authentication request.
Definition message_handler.cpp:51
MessageHandler & operator=(const MessageHandler &)=delete
Deleted assignment operator to prevent assignment.
static QJsonObject ParseMessage(const QString &message, bool *ok=nullptr)
Parses a JSON string message into a QJsonObject.
Definition message_handler.cpp:39
QSet< QString > processed_message_ids_
Set storing the IDs of messages that have already been processed to prevent duplicates.
Definition message_handler.h:178
static bool SendSystemCommand(QWebSocket *socket, const QString &command, const QJsonObject &params=QJsonObject())
Sends a JSON-formatted system command over the specified WebSocket. Constructs a JSON object with the...
Definition message_handler.cpp:6
MessageHandler(QObject *parent=nullptr)
Private constructor to enforce the singleton pattern.
Definition message_handler.h:159
static QString GetMessageId(const QJsonObject &message_object)
Extracts the message ID string from a parsed message object.
Definition message_handler.h:115
static constexpr int kMaxCachedMessageIds
Maximum number of message IDs to keep in the processed cache before removing older ones.
Definition message_handler.h:180
void MarkMessageAsProcessed(const QString &message_id)
Marks a message ID as processed by adding it to the internal set. Also manages the size of the proces...
Definition message_handler.cpp:25
static QString GetMessageSenderId(const QJsonObject &message_object)
Extracts the sender ID string from a parsed message object.
Definition message_handler.h:106
static QString GetMessageContent(const QJsonObject &message_object)
Extracts the message content string from a parsed message object.
Definition message_handler.h:88
static MessageHandler * GetInstance()
Gets the singleton instance of the MessageHandler.
Definition message_handler.h:25
static QJsonObject CreateRegisterRequest(const QString &frequency, bool is_password_protected, const QString &password, const QString &host_id)
Creates a JSON object representing a request to register a new frequency.
Definition message_handler.cpp:61