/****************************************************************************** * Copyright (C) 2015 Felix Rohrbach * * 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 */ #include "event.h" #include "roommessageevent.h" #include "roomnameevent.h" #include "roomaliasesevent.h" #include "roomcanonicalaliasevent.h" #include "roommemberevent.h" #include "roomtopicevent.h" #include "roomavatarevent.h" #include "typingevent.h" #include "receiptevent.h" #include "encryptedevent.h" #include "logging.h" #include using namespace QMatrixClient; Event::Event(Type type, const QJsonObject& rep) : _type(type), _originalJson(rep) { if (!rep.contains("content")) { qCWarning(EVENTS) << "Event without 'content' node"; qCWarning(EVENTS) << formatJson << rep; } } QByteArray Event::originalJson() const { return QJsonDocument(_originalJson).toJson(); } QJsonObject Event::originalJsonObject() const { return _originalJson; } QDateTime Event::toTimestamp(const QJsonValue& v) { Q_ASSERT(v.isDouble() || v.isNull() || v.isUndefined()); return QDateTime::fromMSecsSinceEpoch( static_cast(v.toDouble()), Qt::UTC); } QStringList Event::toStringList(const QJsonValue& v) { Q_ASSERT(v.isArray() || v.isNull() || v.isUndefined()); QStringList l; for( const QJsonValue& e : v.toArray() ) l.push_back(e.toString()); return l; } const QJsonObject Event::contentJson() const { return _originalJson["content"].toObject(); } template EventT* make(const QJsonObject& o) { return new EventT(o); } Event* Event::fromJson(const QJsonObject& obj) { // Check more specific event types first if (auto e = RoomEvent::fromJson(obj)) return e; return dispatch(obj).to(obj["type"].toString(), "m.typing", make, "m.receipt", make, /* Insert new event types (except room events) BEFORE this line */ nullptr ); } RoomEvent::RoomEvent(Type type, const QJsonObject& rep) : Event(type, rep), _id(rep["event_id"].toString()) , _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()) // { // qCWarning(EVENTS) << "Can't find event_id in a room event"; // qCWarning(EVENTS) << formatJson << rep; // } // if (!rep.contains("origin_server_ts")) // { // qCWarning(EVENTS) << "Can't find server timestamp in a room event"; // qCWarning(EVENTS) << formatJson << rep; // } // if (_senderId.isEmpty()) // { // 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) { return dispatch(obj).to(obj["type"].toString(), "m.room.message", make, "m.room.name", make, "m.room.aliases", make, "m.room.canonical_alias", make, "m.room.member", make, "m.room.topic", make, "m.room.avatar", make, "m.room.encryption", make, /* Insert new ROOM event types BEFORE this line */ nullptr ); }