aboutsummaryrefslogtreecommitdiff
path: root/events
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-06-22 17:36:23 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-06-22 17:36:23 +0900
commit7d745dca7bdd328fd96acdf53f15f4a5cd7cf484 (patch)
tree772e4afb6d99d080b293415c73269b977cb5150e /events
parent631b322ba2b7e7ffc44b2a1ab653b851be86fd33 (diff)
downloadlibquotient-7d745dca7bdd328fd96acdf53f15f4a5cd7cf484.tar.gz
libquotient-7d745dca7bdd328fd96acdf53f15f4a5cd7cf484.zip
RoomMessageEvent: Simplify constructors, use QString msgType internally
QString msgType allows non-standard types (we don't want to restrict clients to types from the spec)
Diffstat (limited to 'events')
-rw-r--r--events/roommessageevent.cpp30
-rw-r--r--events/roommessageevent.h28
2 files changed, 31 insertions, 27 deletions
diff --git a/events/roommessageevent.cpp b/events/roommessageevent.cpp
index ccaa6226..179eac77 100644
--- a/events/roommessageevent.cpp
+++ b/events/roommessageevent.cpp
@@ -51,7 +51,7 @@ const std::vector<MsgTypeDesc> msgTypes =
, { QStringLiteral("m.audio"), MsgType::Audio, make<AudioContent> }
};
-QJsonValue msgTypeToJson(MsgType enumType)
+QString msgTypeToJson(MsgType enumType)
{
auto it = std::find_if(msgTypes.begin(), msgTypes.end(),
[=](const MsgTypeDesc& mtd) { return mtd.enumType == enumType; });
@@ -73,28 +73,29 @@ MsgType jsonToMsgType(const QString& jsonType)
return {};
}
+RoomMessageEvent::RoomMessageEvent(const QString& plainBody,
+ MsgType msgType, Base* content)
+ : RoomMessageEvent(plainBody, msgTypeToJson(msgType), content)
+{ }
+
RoomMessageEvent::RoomMessageEvent(const QJsonObject& obj)
- : RoomEvent(Type::RoomMessage, obj), _msgtype(MsgType::Unknown)
- , _content(nullptr)
+ : RoomEvent(Type::RoomMessage, obj), _content(nullptr)
{
const QJsonObject content = contentJson();
if ( content.contains("msgtype") && content.contains("body") )
{
_plainBody = content["body"].toString();
- auto msgtype = content["msgtype"].toString();
+ _msgtype = content["msgtype"].toString();
for (auto mt: msgTypes)
- if (mt.jsonType == msgtype)
- {
- _msgtype = mt.enumType;
+ if (mt.jsonType == _msgtype)
_content.reset(mt.maker(content));
- }
- if (_msgtype == MsgType::Unknown)
+ if (!_content)
{
- qCDebug(EVENTS) << "RoomMessageEvent: couldn't load content,"
- << " full content dump follows";
- qCDebug(EVENTS) << formatJson << content;
+ qCWarning(EVENTS) << "RoomMessageEvent: couldn't load content,"
+ << " full content dump follows";
+ qCWarning(EVENTS) << formatJson << content;
}
}
else
@@ -104,6 +105,11 @@ RoomMessageEvent::RoomMessageEvent(const QJsonObject& obj)
}
}
+RoomMessageEvent::MsgType RoomMessageEvent::msgtype() const
+{
+ return jsonToMsgType(_msgtype);
+}
+
QMimeType RoomMessageEvent::mimeType() const
{
return _content ? _content->mimeType :
diff --git a/events/roommessageevent.h b/events/roommessageevent.h
index edc11bee..74e0defb 100644
--- a/events/roommessageevent.h
+++ b/events/roommessageevent.h
@@ -92,24 +92,22 @@ namespace QMatrixClient
Text, Emote, Notice, Image, File, Location, Video, Audio, Unknown
};
- RoomMessageEvent(const QString& roomId, const QString& fromUserId,
- const QString& plainMessage,
- MsgType msgType = MsgType::Text)
- : RoomEvent(Type::RoomMessage, roomId, fromUserId)
- , _msgtype(msgType), _plainBody(plainMessage), _content(nullptr)
- { }
- RoomMessageEvent(const QString& roomId, const QString& fromUserId,
- const QString& plainBody,
- MessageEventContent::Base* content,
- MsgType msgType)
- : RoomEvent(Type::RoomMessage, roomId, fromUserId)
- , _msgtype(msgType), _plainBody(plainBody), _content(content)
+ RoomMessageEvent(const QString& plainBody,
+ const QString& jsonMsgType,
+ MessageEventContent::Base* content = nullptr)
+ : RoomEvent(Type::RoomMessage)
+ , _msgtype(jsonMsgType), _plainBody(plainBody), _content(content)
{ }
+ explicit RoomMessageEvent(const QString& plainBody,
+ MsgType msgType = MsgType::Text,
+ MessageEventContent::Base* content = nullptr);
explicit RoomMessageEvent(const QJsonObject& obj);
- MsgType msgtype() const { return _msgtype; }
+ MsgType msgtype() const;
+ QString rawMsgtype() const { return _msgtype; }
const QString& plainBody() const { return _plainBody; }
- const MessageEventContent::Base* content() const { return _content.data(); }
+ const MessageEventContent::Base* content() const
+ { return _content.data(); }
QMimeType mimeType() const;
QJsonObject toJson() const;
@@ -117,7 +115,7 @@ namespace QMatrixClient
static constexpr const char* TypeId = "m.room.message";
private:
- MsgType _msgtype;
+ QString _msgtype;
QString _plainBody;
QScopedPointer<MessageEventContent::Base> _content;