diff options
-rw-r--r-- | events/event.cpp | 14 | ||||
-rw-r--r-- | events/event.h | 4 | ||||
-rw-r--r-- | jobs/roommessagesjob.cpp | 9 |
3 files changed, 18 insertions, 9 deletions
diff --git a/events/event.cpp b/events/event.cpp index c3e94ce1..9a48108c 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -18,7 +18,7 @@ #include "event.h" -#include <QtCore/QJsonObject> +#include <QtCore/QJsonArray> #include <QtCore/QJsonDocument> #include <QtCore/QDateTime> #include <QtCore/QDebug> @@ -134,7 +134,8 @@ bool Event::parseJson(const QJsonObject& obj) } if( obj.contains("origin_server_ts") ) { - d->timestamp = QDateTime::fromMSecsSinceEpoch( (quint64) obj.value("origin_server_ts").toDouble(), Qt::UTC ); + d->timestamp = QDateTime::fromMSecsSinceEpoch( + static_cast<qint64>(obj.value("origin_server_ts").toDouble()), Qt::UTC ); } else { correct = false; qDebug() << "Event: can't find ts"; @@ -146,3 +147,12 @@ bool Event::parseJson(const QJsonObject& obj) } return correct; } + +QList<Event*> QMatrixClient::eventListFromJson(const QJsonArray& json) +{ + QList<Event*> l; + l.reserve(json.size()); + for (auto event: json) + l.push_back(Event::fromJson(event.toObject())); + return l; +} diff --git a/events/event.h b/events/event.h index 6a8d0e89..a009aa99 100644 --- a/events/event.h +++ b/events/event.h @@ -25,6 +25,8 @@ #include <QtCore/QDateTime> #include <QtCore/QJsonObject> +class QJsonArray; + namespace QMatrixClient { enum class EventType @@ -56,6 +58,8 @@ namespace QMatrixClient Private* d; }; + QList<Event*> eventListFromJson(const QJsonArray& contents); + /** * Finds a place in the timeline where a new event/message could be inserted. * @return an iterator to an item with the earliest timestamp after diff --git a/jobs/roommessagesjob.cpp b/jobs/roommessagesjob.cpp index fc3f5f1e..5a97cc23 100644 --- a/jobs/roommessagesjob.cpp +++ b/jobs/roommessagesjob.cpp @@ -84,12 +84,7 @@ QUrlQuery RoomMessagesJob::query() void RoomMessagesJob::parseJson(const QJsonDocument& data) { QJsonObject obj = data.object(); - QJsonArray chunk = obj.value("chunk").toArray(); - for( const QJsonValue& val: chunk ) - { - Event* event = Event::fromJson(val.toObject()); - d->events.append(event); - } + d->events = eventListFromJson(obj.value("chunk").toArray()); d->end = obj.value("end").toString(); emitResult(); -}
\ No newline at end of file +} |