From 7d745dca7bdd328fd96acdf53f15f4a5cd7cf484 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Thu, 22 Jun 2017 17:36:23 +0900 Subject: 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) --- events/roommessageevent.cpp | 30 ++++++++++++++++++------------ events/roommessageevent.h | 28 +++++++++++++--------------- 2 files changed, 31 insertions(+), 27 deletions(-) (limited to 'events') 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 msgTypes = , { QStringLiteral("m.audio"), MsgType::Audio, make } }; -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 _content; -- cgit v1.2.3