diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-05-26 17:56:11 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2017-06-22 17:09:03 +0900 |
commit | 1c89c319ae8b61111e893969a40d740a9d76c945 (patch) | |
tree | 2a8722e996f271a2ab1480694e52c56270888e89 /events | |
parent | 7150b8e864f0a2b9ba602bb56384f7f521f4098d (diff) | |
download | libquotient-1c89c319ae8b61111e893969a40d740a9d76c945.tar.gz libquotient-1c89c319ae8b61111e893969a40d740a9d76c945.zip |
Enable creation and usage of Event and RoomEvent objects locally, including QML
This includes RoomEvent gaining transactionId property and addId() method so that it could gain ids when being/having been sent.
Diffstat (limited to 'events')
-rw-r--r-- | events/event.cpp | 13 | ||||
-rw-r--r-- | events/event.h | 50 |
2 files changed, 55 insertions, 8 deletions
diff --git a/events/event.cpp b/events/event.cpp index bd7e1b03..653396bd 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -95,6 +95,7 @@ RoomEvent::RoomEvent(Type type, const QJsonObject& rep) , _serverTimestamp(toTimestamp(rep["origin_server_ts"])) , _roomId(rep["room_id"].toString()) , _senderId(rep["sender"].toString()) + , _txnId(rep["unsigned"].toObject().value("transactionId").toString()) { if (_id.isEmpty()) { @@ -103,14 +104,22 @@ RoomEvent::RoomEvent(Type type, const QJsonObject& rep) } if (!rep.contains("origin_server_ts")) { - qCWarning(EVENTS) << "Event: can't find server timestamp in a room event"; + qCWarning(EVENTS) << "Can't find server timestamp in a room event"; qCWarning(EVENTS) << formatJson << rep; } if (_senderId.isEmpty()) { - qCWarning(EVENTS) << "user_id not found in a room event"; + qCWarning(EVENTS) << "Can't find sender in a room event"; qCWarning(EVENTS) << formatJson << rep; } + if (!_txnId.isEmpty()) + qCDebug(EVENTS) << "Event transactionId:" << _txnId; +} + +void RoomEvent::addId(const QString& id) +{ + Q_ASSERT(_id.isEmpty()); Q_ASSERT(!id.isEmpty()); + _id = id; } RoomEvent* RoomEvent::fromJson(const QJsonObject& obj) diff --git a/events/event.h b/events/event.h index fd2f6feb..8760aa28 100644 --- a/events/event.h +++ b/events/event.h @@ -37,14 +37,16 @@ namespace QMatrixClient RoomMember, RoomTopic, Typing, Receipt, Unknown }; - explicit Event(Type type, const QJsonObject& rep); + explicit Event(Type type) : _type(type) { } + Event(Type type, const QJsonObject& rep); Event(const Event&) = delete; Type type() const { return _type; } QByteArray originalJson() const; - // Every event also has a "content" object but since its structure is - // different for different types, we're implementing it per-event type + // According to the CS API spec, every event also has + // a "content" object; but since its structure is different for + // different types, we're implementing it per-event type // (and in most cases it will be a combination of other fields // instead of "content" field). @@ -61,6 +63,8 @@ namespace QMatrixClient QJsonObject _originalJson; REGISTER_ENUM(Type) + Q_PROPERTY(Type type READ type CONSTANT) + Q_PROPERTY(QJsonObject contentJson READ contentJson CONSTANT) }; using EventType = Event::Type; template <typename EventT> @@ -91,13 +95,42 @@ namespace QMatrixClient class RoomEvent : public Event { + Q_GADGET + Q_PROPERTY(QString id READ id) + 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) public: + explicit RoomEvent(Type type) : Event(type) { } RoomEvent(Type type, const QJsonObject& rep); - const QString& id() const { return _id; } + const QString& id() const { return _id; } const QDateTime& timestamp() const { return _serverTimestamp; } - const QString& roomId() const { return _roomId; } - const QString& senderId() const { return _senderId; } + const QString& roomId() const { return _roomId; } + const QString& senderId() const { return _senderId; } + const QString& transactionId() const { return _txnId; } + + /** + * Sets the transaction id for locally created events. This should be + * done before the event is exposed to any code using the respective + * Q_PROPERTY. + * + * \param txnId - transaction id, normally obtained from + * Connection::generateTxnId() + */ + void setTransactionId(const QString& txnId) { _txnId = txnId; } + + /** + * Sets event id for locally created events + * + * When a new event is created locally, it has no server id yet. + * This function allows to add the id once the confirmation from + * the server is received. There should be no id set previously + * in the event. It's the responsibility of the code calling addId() + * to notify clients that use Q_PROPERTY(id) about its change + */ + void addId(const QString& id); // "Static override" of the one in Event static RoomEvent* fromJson(const QJsonObject& obj); @@ -107,6 +140,11 @@ namespace QMatrixClient QDateTime _serverTimestamp; QString _roomId; QString _senderId; + QString _txnId; }; using RoomEvents = EventsBatch<RoomEvent>; } // namespace QMatrixClient +Q_DECLARE_OPAQUE_POINTER(QMatrixClient::Event*) +Q_DECLARE_METATYPE(QMatrixClient::Event*) +Q_DECLARE_OPAQUE_POINTER(QMatrixClient::RoomEvent*) +Q_DECLARE_METATYPE(QMatrixClient::RoomEvent*) |