diff options
-rw-r--r-- | events/event.cpp | 31 | ||||
-rw-r--r-- | events/event.h | 18 | ||||
-rw-r--r-- | events/redactionevent.cpp | 1 | ||||
-rw-r--r-- | events/redactionevent.h | 43 | ||||
-rw-r--r-- | events/roommessageevent.cpp | 6 | ||||
-rw-r--r-- | libqmatrixclient.pri | 2 |
6 files changed, 91 insertions, 10 deletions
diff --git a/events/event.cpp b/events/event.cpp index 44b742c1..d779f293 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -24,6 +24,7 @@ #include "roomavatarevent.h" #include "typingevent.h" #include "receiptevent.h" +#include "redactionevent.h" #include "logging.h" #include <QtCore/QJsonDocument> @@ -33,7 +34,8 @@ using namespace QMatrixClient; Event::Event(Type type, const QJsonObject& rep) : _type(type), _originalJson(rep) { - if (!rep.contains("content")) + if (!rep.contains("content") && + !rep.value("unsigned").toObject().contains("redacted_because")) { qCWarning(EVENTS) << "Event without 'content' node"; qCWarning(EVENTS) << formatJson << rep; @@ -80,13 +82,12 @@ Event* Event::fromJson(const QJsonObject& obj) TypingEvent, ReceiptEvent>(obj, obj["type"].toString()); } +RoomEvent::RoomEvent(Event::Type type) : Event(type) { } + RoomEvent::RoomEvent(Type type, const QJsonObject& rep) : Event(type, rep), _id(rep["event_id"].toString()) - , _serverTimestamp( - QMatrixClient::fromJson<QDateTime>(rep["origin_server_ts"])) , _roomId(rep["room_id"].toString()) , _senderId(rep["sender"].toString()) - , _txnId(rep["unsigned"].toObject().value("transactionId").toString()) { // if (_id.isEmpty()) // { @@ -103,10 +104,29 @@ RoomEvent::RoomEvent(Type type, const QJsonObject& rep) // qCWarning(EVENTS) << "Can't find sender in a room event"; // qCWarning(EVENTS) << formatJson << rep; // } + auto unsignedData = rep["unsigned"].toObject(); + auto redaction = unsignedData.value("redacted_because"); + if (redaction.isObject()) + { + _redactedBecause.reset(new RedactionEvent(redaction.toObject())); + return; + } + + _serverTimestamp = + QMatrixClient::fromJson<QDateTime>(rep["origin_server_ts"]); + _txnId = unsignedData.value("transactionId").toString(); if (!_txnId.isEmpty()) qCDebug(EVENTS) << "Event transactionId:" << _txnId; } +RoomEvent::~RoomEvent() +{ /* Let QScopedPointer<RedactionEvent> do its job */ } + +QString RoomEvent::redactionReason() const +{ + return isRedacted() ? _redactedBecause->reason() : QString{}; +} + void RoomEvent::addId(const QString& id) { Q_ASSERT(_id.isEmpty()); Q_ASSERT(!id.isEmpty()); @@ -118,5 +138,6 @@ RoomEvent* RoomEvent::fromJson(const QJsonObject& obj) return makeIfMatches<RoomEvent, RoomMessageEvent, RoomNameEvent, RoomAliasesEvent, RoomCanonicalAliasEvent, RoomMemberEvent, RoomTopicEvent, - RoomAvatarEvent, EncryptionEvent>(obj, obj["type"].toString()); + RoomAvatarEvent, EncryptionEvent, RedactionEvent> + (obj, obj["type"].toString()); } diff --git a/events/event.h b/events/event.h index 8681f4db..62d9c23b 100644 --- a/events/event.h +++ b/events/event.h @@ -104,6 +104,8 @@ namespace QMatrixClient }; using Events = EventsBatch<Event>; + class RedactionEvent; + /** This class corresponds to m.room.* events */ class RoomEvent : public Event { @@ -112,15 +114,26 @@ namespace QMatrixClient Q_PROPERTY(QDateTime timestamp READ timestamp CONSTANT) Q_PROPERTY(QString roomId READ roomId CONSTANT) Q_PROPERTY(QString senderId READ senderId CONSTANT) - Q_PROPERTY(QString transactionId READ transactionId CONSTANT) + Q_PROPERTY(QString redactionReason READ redactionReason) + Q_PROPERTY(bool isRedacted READ isRedacted) + Q_PROPERTY(QString transactionId READ transactionId) public: - explicit RoomEvent(Type type) : Event(type) { } + // RedactionEvent is an incomplete type here so we cannot inline + // constructors and destructors + explicit RoomEvent(Type type); RoomEvent(Type type, const QJsonObject& rep); + ~RoomEvent(); const QString& id() const { return _id; } const QDateTime& timestamp() const { return _serverTimestamp; } const QString& roomId() const { return _roomId; } const QString& senderId() const { return _senderId; } + bool isRedacted() const { return redactedBecause(); } + RedactionEvent* redactedBecause() const + { + return _redactedBecause.data(); + } + QString redactionReason() const; const QString& transactionId() const { return _txnId; } /** @@ -152,6 +165,7 @@ namespace QMatrixClient QDateTime _serverTimestamp; QString _roomId; QString _senderId; + QScopedPointer<RedactionEvent> _redactedBecause; QString _txnId; }; using RoomEvents = EventsBatch<RoomEvent>; diff --git a/events/redactionevent.cpp b/events/redactionevent.cpp new file mode 100644 index 00000000..bf467718 --- /dev/null +++ b/events/redactionevent.cpp @@ -0,0 +1 @@ +#include "redactionevent.h" diff --git a/events/redactionevent.h b/events/redactionevent.h new file mode 100644 index 00000000..fa6902ab --- /dev/null +++ b/events/redactionevent.h @@ -0,0 +1,43 @@ +/****************************************************************************** + * Copyright (C) 2017 Kitsune Ral <kitsune-ral@users.sf.net> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +#include "event.h" + +namespace QMatrixClient +{ + class RedactionEvent : public RoomEvent + { + public: + static constexpr const char* const TypeId = "m.room.redaction"; + + RedactionEvent(const QJsonObject& obj) + : RoomEvent(Type::Redaction, obj) + , _redactedEvent(obj.value("redacts").toString()) + , _reason(contentJson().value("reason").toString()) + { } + + const QString& redactedEvent() const { return _redactedEvent; } + const QString& reason() const { return _reason; } + + private: + QString _redactedEvent; + QString _reason; + }; +} // namespace QMatrixClient diff --git a/events/roommessageevent.cpp b/events/roommessageevent.cpp index f06474e9..bc41abf6 100644 --- a/events/roommessageevent.cpp +++ b/events/roommessageevent.cpp @@ -58,7 +58,6 @@ QString msgTypeToJson(MsgType enumType) if (it != msgTypes.end()) return it->jsonType; - qCCritical(EVENTS) << "Unknown msgtype:" << enumType; return {}; } @@ -69,8 +68,7 @@ MsgType jsonToMsgType(const QString& jsonType) if (it != msgTypes.end()) return it->enumType; - qCCritical(EVENTS) << "Unknown msgtype:" << jsonType; - return {}; + return MsgType::Unknown; } RoomMessageEvent::RoomMessageEvent(const QString& plainBody, @@ -81,6 +79,8 @@ RoomMessageEvent::RoomMessageEvent(const QString& plainBody, RoomMessageEvent::RoomMessageEvent(const QJsonObject& obj) : RoomEvent(Type::RoomMessage, obj), _content(nullptr) { + if (isRedacted()) + return; const QJsonObject content = contentJson(); if ( content.contains("msgtype") && content.contains("body") ) { diff --git a/libqmatrixclient.pri b/libqmatrixclient.pri index 86648860..49442197 100644 --- a/libqmatrixclient.pri +++ b/libqmatrixclient.pri @@ -18,6 +18,7 @@ HEADERS += \ $$PWD/events/roomavatarevent.h \ $$PWD/events/typingevent.h \ $$PWD/events/receiptevent.h \ + $$PWD/events/redactionevent.h \ $$PWD/jobs/basejob.h \ $$PWD/jobs/checkauthmethods.h \ $$PWD/jobs/passwordlogin.h \ @@ -44,6 +45,7 @@ SOURCES += \ $$PWD/events/roommemberevent.cpp \ $$PWD/events/typingevent.cpp \ $$PWD/events/receiptevent.cpp \ + $$PWD/events/redactionevent.cpp \ $$PWD/jobs/basejob.cpp \ $$PWD/jobs/checkauthmethods.cpp \ $$PWD/jobs/passwordlogin.cpp \ |