Chatterino
Message.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "common/QLogging.hpp"
4 
5 #include <QJsonDocument>
6 #include <QJsonObject>
7 #include <QString>
8 
9 #include <boost/optional.hpp>
10 
11 namespace chatterino {
12 
14  QString nonce;
15  QString topic;
16 
17  QJsonObject messageObject;
18 
19  PubSubMessageMessage(QString _nonce, const QJsonObject &data)
20  : nonce(std::move(_nonce))
21  , topic(data.value("topic").toString())
22  {
23  auto messagePayload = data.value("message").toString().toUtf8();
24 
25  auto messageDoc = QJsonDocument::fromJson(messagePayload);
26 
27  if (messageDoc.isNull())
28  {
29  qCWarning(chatterinoPubSub) << "PubSub message (type MESSAGE) "
30  "missing inner message payload";
31  return;
32  }
33 
34  if (!messageDoc.isObject())
35  {
36  qCWarning(chatterinoPubSub)
37  << "PubSub message (type MESSAGE) inner message payload is not "
38  "an object";
39  return;
40  }
41 
42  this->messageObject = messageDoc.object();
43  }
44 
45  template <class InnerClass>
46  boost::optional<InnerClass> toInner() const;
47 };
48 
49 template <class InnerClass>
50 boost::optional<InnerClass> PubSubMessageMessage::toInner() const
51 {
52  if (this->messageObject.empty())
53  {
54  return boost::none;
55  }
56 
57  return InnerClass{this->messageObject};
58 }
59 
60 } // namespace chatterino
Definition: SeventvEventAPISubscription.hpp:67
Definition: Message.hpp:13
Definition: Application.cpp:48
QJsonObject messageObject
Definition: Message.hpp:17
QString topic
Definition: Message.hpp:15
PubSubMessageMessage(QString _nonce, const QJsonObject &data)
Definition: Message.hpp:19
boost::optional< InnerClass > toInner() const
Definition: Message.hpp:50
QString nonce
Definition: Message.hpp:14