2017-04-06 02:06:42 +03:00
|
|
|
/*
|
|
|
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonValue>
|
|
|
|
|
|
|
|
#include "Deserializable.h"
|
|
|
|
#include "Sync.h"
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
SyncResponse::deserialize(const QJsonDocument &data)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!data.isObject())
|
|
|
|
throw DeserializationException("Sync response is not a JSON object");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
QJsonObject object = data.object();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
if (!object.contains("next_batch"))
|
2017-09-10 12:59:21 +03:00
|
|
|
throw DeserializationException("Sync: missing next_batch parameter");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
if (object.contains("rooms")) {
|
|
|
|
if (!object.value("rooms").isObject()) {
|
|
|
|
throw DeserializationException("Sync: rooms is not a JSON object");
|
|
|
|
}
|
|
|
|
rooms_.deserialize(object.value("rooms"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object.contains("presence")) {
|
|
|
|
if (!object.value("presence").isObject()) {
|
|
|
|
throw DeserializationException("Sync: presence is not a JSON object");
|
|
|
|
}
|
|
|
|
// TODO: implement presence handling
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object.contains("account_data")) {
|
|
|
|
if (!object.value("account_data").isObject()) {
|
|
|
|
throw DeserializationException("Sync: account_data is not a JSON object");
|
|
|
|
}
|
|
|
|
// TODO: implement account_data handling
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object.contains("to_device")) {
|
|
|
|
if (!object.value("to_device").isObject()) {
|
|
|
|
throw DeserializationException("Sync: to_device is not a JSON object");
|
|
|
|
}
|
|
|
|
// TODO: implement to_device handling
|
|
|
|
}
|
|
|
|
|
|
|
|
// for device_lists updates (for e2e)
|
|
|
|
if (object.contains("device_lists")) {
|
|
|
|
if (!object.value("device_lists").isObject()) {
|
|
|
|
throw DeserializationException("Sync: device_lists is not a JSON object");
|
|
|
|
}
|
|
|
|
// TODO: implement device_lists handling
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
next_batch_ = object.value("next_batch").toString();
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
Rooms::deserialize(const QJsonValue &data)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!data.isObject())
|
|
|
|
throw DeserializationException("Rooms value is not a JSON object");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
QJsonObject object = data.toObject();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
if (object.contains("join")) {
|
|
|
|
if (!object.value("join").isObject())
|
|
|
|
throw DeserializationException("rooms/join must be a JSON object");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
auto join = object.value("join").toObject();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
for (auto it = join.constBegin(); it != join.constEnd(); it++) {
|
|
|
|
JoinedRoom tmp_room;
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
try {
|
|
|
|
tmp_room.deserialize(it.value());
|
|
|
|
join_.insert(it.key(), tmp_room);
|
|
|
|
} catch (DeserializationException &e) {
|
|
|
|
qWarning() << e.what();
|
|
|
|
qWarning() << "Skipping malformed object for room" << it.key();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
if (object.contains("invite")) {
|
|
|
|
if (!object.value("invite").isObject()) {
|
|
|
|
throw DeserializationException("rooms/invite must be a JSON object");
|
|
|
|
}
|
|
|
|
// TODO: Implement invite handling
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
if (object.contains("leave")) {
|
|
|
|
if (!object.value("leave").isObject()) {
|
|
|
|
throw DeserializationException("rooms/leave must be a JSON object");
|
2017-09-10 12:59:21 +03:00
|
|
|
}
|
2017-09-29 21:10:11 +03:00
|
|
|
// TODO: Implement leave handling
|
2017-09-10 12:59:21 +03:00
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
JoinedRoom::deserialize(const QJsonValue &data)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!data.isObject())
|
|
|
|
throw DeserializationException("JoinedRoom is not a JSON object");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
QJsonObject object = data.toObject();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
if (object.contains("state")) {
|
|
|
|
if (!object.value("state").isObject()) {
|
|
|
|
throw DeserializationException("join/state should be an object");
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject state = object.value("state").toObject();
|
|
|
|
|
|
|
|
if (state.contains("events")) {
|
|
|
|
if (!state.value("events").isArray()) {
|
|
|
|
throw DeserializationException(
|
|
|
|
"join/state/events should be an array");
|
|
|
|
}
|
|
|
|
|
|
|
|
state_.deserialize(state.value("events"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object.contains("timeline")) {
|
|
|
|
if (!object.value("timeline").isObject())
|
|
|
|
throw DeserializationException("join/timeline should be an object");
|
|
|
|
timeline_.deserialize(object.value("timeline"));
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
if (object.contains("ephemeral")) {
|
|
|
|
if (!object.value("ephemeral").isObject())
|
|
|
|
throw DeserializationException("join/ephemeral should be an object");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
QJsonObject ephemeral = object.value("ephemeral").toObject();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
if (ephemeral.contains("events")) {
|
|
|
|
if (!ephemeral.value("events").isArray())
|
|
|
|
qWarning() << "join/ephemeral/events should be an array";
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
// TODO: Implement ephemeral handling
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
if (object.contains("account_data")) {
|
|
|
|
if (!object.value("account_data").isObject())
|
|
|
|
throw DeserializationException("join/account_data is not a JSON object");
|
|
|
|
// TODO: Implement account_data handling
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
if (object.contains("unread_notifications")) {
|
|
|
|
if (!object.value("unread_notifications").isObject()) {
|
|
|
|
throw DeserializationException(
|
|
|
|
"join/unread_notifications is not a JSON object");
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-29 21:10:11 +03:00
|
|
|
QJsonObject unreadNotifications = object.value("unread_notifications").toObject();
|
|
|
|
|
|
|
|
if (unreadNotifications.contains("highlight_count")) {
|
|
|
|
// TODO: Implement unread_notifications handling
|
|
|
|
}
|
|
|
|
if (unreadNotifications.contains("notification_count")) {
|
|
|
|
// TODO: Implement unread_notifications handling
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
Event::deserialize(const QJsonValue &data)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!data.isObject())
|
|
|
|
throw DeserializationException("Event is not a JSON object");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
QJsonObject object = data.toObject();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!object.contains("content"))
|
|
|
|
throw DeserializationException("event/content is missing");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!object.contains("unsigned"))
|
|
|
|
throw DeserializationException("event/content is missing");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!object.contains("sender"))
|
|
|
|
throw DeserializationException("event/sender is missing");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!object.contains("event_id"))
|
|
|
|
throw DeserializationException("event/event_id is missing");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
// TODO: Make this optional
|
|
|
|
/* if (!object.contains("state_key")) */
|
|
|
|
/* throw DeserializationException("event/state_key is missing"); */
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!object.contains("type"))
|
|
|
|
throw DeserializationException("event/type is missing");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!object.contains("origin_server_ts"))
|
|
|
|
throw DeserializationException("event/origin_server_ts is missing");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
content_ = object.value("content").toObject();
|
|
|
|
unsigned_ = object.value("unsigned").toObject();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
sender_ = object.value("sender").toString();
|
|
|
|
state_key_ = object.value("state_key").toString();
|
|
|
|
type_ = object.value("type").toString();
|
|
|
|
event_id_ = object.value("event_id").toString();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
origin_server_ts_ = object.value("origin_server_ts").toDouble();
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
State::deserialize(const QJsonValue &data)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!data.isArray())
|
|
|
|
throw DeserializationException("State is not a JSON array");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
events_ = data.toArray();
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
Timeline::deserialize(const QJsonValue &data)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!data.isObject())
|
|
|
|
throw DeserializationException("Timeline is not a JSON object");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
auto object = data.toObject();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!object.contains("events"))
|
|
|
|
throw DeserializationException("timeline/events is missing");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!object.contains("prev_batch"))
|
|
|
|
throw DeserializationException("timeline/prev_batch is missing");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!object.contains("limited"))
|
|
|
|
throw DeserializationException("timeline/limited is missing");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
prev_batch_ = object.value("prev_batch").toString();
|
|
|
|
limited_ = object.value("limited").toBool();
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
if (!object.value("events").isArray())
|
|
|
|
throw DeserializationException("timeline/events is not a JSON array");
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:59:21 +03:00
|
|
|
events_ = object.value("events").toArray();
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|