diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-05-22 10:22:28 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-05-22 10:22:28 +0900 |
commit | be838b2f4f294a7e1b3f8a771f91d9d1eac14431 (patch) | |
tree | f720ae5a5a52bdd48fdaec17dea3c1c32fe10cd9 /events/roommessageevent.cpp | |
parent | 34764f3020c360ebc769cfe154e79b9e7e98f0f7 (diff) | |
download | libquotient-be838b2f4f294a7e1b3f8a771f91d9d1eac14431.tar.gz libquotient-be838b2f4f294a7e1b3f8a771f91d9d1eac14431.zip |
Refactored Events
The biggest change is we have no pimpls in Event objects anymore - because it's two new's instead of one per Event, and we have thousands and even more of Events created during initial sync. The other big change is introduction of RoomEvent, so that now the structure of events almost precisely reflects the CS API spec. The refactoring made UnknownEvent unnecessary as a separate class; a respective base class (either RoomEvent or Event) is used for this purpose now. All the other changes are consequences of these (mostly of RoomEvent introduction).
Diffstat (limited to 'events/roommessageevent.cpp')
-rw-r--r-- | events/roommessageevent.cpp | 121 |
1 files changed, 33 insertions, 88 deletions
diff --git a/events/roommessageevent.cpp b/events/roommessageevent.cpp index d5e960a1..49bb4053 100644 --- a/events/roommessageevent.cpp +++ b/events/roommessageevent.cpp @@ -19,64 +19,15 @@ #include "roommessageevent.h" #include "logging.h" -#include "util.h" #include <QtCore/QMimeDatabase> using namespace QMatrixClient; - -class RoomMessageEvent::Private -{ - public: - Private() : msgtype(MessageEventType::Unknown), content(nullptr) {} - ~Private() { if (content) delete content; } - - QString userId; - MessageEventType msgtype; - QString plainBody; - MessageEventContent::Base* content; -}; - -RoomMessageEvent::RoomMessageEvent() - : Event(EventType::RoomMessage) - , d(new Private) -{ } - -RoomMessageEvent::~RoomMessageEvent() -{ - delete d; -} - -QString RoomMessageEvent::userId() const -{ - return d->userId; -} - -MessageEventType RoomMessageEvent::msgtype() const -{ - return d->msgtype; -} - -QString RoomMessageEvent::plainBody() const -{ - return d->plainBody; -} - -QString RoomMessageEvent::body() const -{ - return plainBody(); -} - using namespace MessageEventContent; -Base* RoomMessageEvent::content() const -{ - return d->content; -} - -using ContentPair = std::pair<MessageEventType, MessageEventContent::Base*>; +using ContentPair = std::pair<CType, Base*>; -template <MessageEventType EnumType, typename ContentT> +template <CType EnumType, typename ContentT> ContentPair make(const QJsonObject& json) { return { EnumType, new ContentT(json) }; @@ -91,59 +42,53 @@ ContentPair makeVideo(const QJsonObject& json) if (infoJson.contains("thumbnail_url")) { c->thumbnail = ImageInfo(infoJson["thumbnail_url"].toString(), - infoJson["thumbnail_info"].toObject()); + infoJson["thumbnail_info"].toObject()); } - return { MessageEventType::Video, c }; + return { CType::Video, c }; }; ContentPair makeUnknown(const QJsonObject& json) { qCDebug(EVENTS) << "RoomMessageEvent: couldn't resolve msgtype, JSON follows:"; qCDebug(EVENTS) << json; - return { MessageEventType::Unknown, new Base }; + return { CType::Unknown, new Base() }; } -RoomMessageEvent* RoomMessageEvent::fromJson(const QJsonObject& obj) +RoomMessageEvent::RoomMessageEvent(const QJsonObject& obj) + : RoomEvent(Type::RoomMessage, obj), _msgtype(CType::Unknown) + , _content(nullptr) { - RoomMessageEvent* e = new RoomMessageEvent(); - e->parseJson(obj); - if( obj.contains("sender") ) + const QJsonObject content = contentJson(); + if ( content.contains("msgtype") && content.contains("body") ) { - e->d->userId = obj.value("sender").toString(); - } else { - qCDebug(EVENTS) << "RoomMessageEvent: user_id not found"; + _plainBody = content["body"].toString(); + + auto factory = lookup(content["msgtype"].toString(), + "m.text", make<CType::Text, TextContent>, + "m.emote", make<CType::Emote, TextContent>, + "m.notice", make<CType::Notice, TextContent>, + "m.image", make<CType::Image, ImageContent>, + "m.file", make<CType::File, FileContent>, + "m.location", make<CType::Location, LocationContent>, + "m.video", makeVideo, + "m.audio", make<CType::Audio, AudioContent>, + // Insert new message types before this line + makeUnknown + ); + std::tie(_msgtype, _content) = factory(content); } - if( obj.contains("content") ) + else { - const QJsonObject content = obj["content"].toObject(); - if ( content.contains("msgtype") && content.contains("body") ) - { - e->d->plainBody = content["body"].toString(); - - auto delegate = lookup(content["msgtype"].toString(), - "m.text", &make<MessageEventType::Text, TextContent>, - "m.emote", &make<MessageEventType::Emote, TextContent>, - "m.notice", &make<MessageEventType::Notice, TextContent>, - "m.image", &make<MessageEventType::Image, ImageContent>, - "m.file", &make<MessageEventType::File, FileContent>, - "m.location", &make<MessageEventType::Location, LocationContent>, - "m.video", &makeVideo, - "m.audio", &make<MessageEventType::Audio, AudioContent>, - // Insert new message types before this line - &makeUnknown - ); - std::tie(e->d->msgtype, e->d->content) = delegate(content); - } - else - { - qCWarning(EVENTS) << "RoomMessageEvent(" << e->id() << "): no body or msgtype"; - qCDebug(EVENTS) << obj; - } + qCWarning(EVENTS) << "No body or msgtype in room message event"; + qCWarning(EVENTS) << formatJson << obj; } - return e; } -using namespace MessageEventContent; +RoomMessageEvent::~RoomMessageEvent() +{ + if (_content) + delete _content; +} TextContent::TextContent(const QJsonObject& json) { |