aboutsummaryrefslogtreecommitdiff
path: root/events/roommessageevent.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-05-28 13:00:44 +0900
committerGitHub <noreply@github.com>2017-05-28 13:00:44 +0900
commit36dbba9c543b819aec526f18c33f1d95b0ee61c7 (patch)
tree49caf6450be52072d8f1e4e40e2dfb8490d73732 /events/roommessageevent.cpp
parentced7a66686596e74a1f25b5d9634b9b562870943 (diff)
parenta5e14da86c3299ca8d36eb4a4eb58ce2a245dc4e (diff)
downloadlibquotient-36dbba9c543b819aec526f18c33f1d95b0ee61c7.tar.gz
libquotient-36dbba9c543b819aec526f18c33f1d95b0ee61c7.zip
Merge pull request #65 from QMatrixClient/kitsune-simplify-events
Events refactoring
Diffstat (limited to 'events/roommessageevent.cpp')
-rw-r--r--events/roommessageevent.cpp121
1 files changed, 33 insertions, 88 deletions
diff --git a/events/roommessageevent.cpp b/events/roommessageevent.cpp
index 677bb79f..19da8827 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.value("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)
{