aboutsummaryrefslogtreecommitdiff
path: root/lib/syncdata.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/syncdata.cpp')
-rw-r--r--lib/syncdata.cpp144
1 files changed, 86 insertions, 58 deletions
diff --git a/lib/syncdata.cpp b/lib/syncdata.cpp
index a3809469..ec7203af 100644
--- a/lib/syncdata.cpp
+++ b/lib/syncdata.cpp
@@ -1,33 +1,15 @@
-/******************************************************************************
- * Copyright (C) 2018 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
- */
+// SPDX-FileCopyrightText: 2018 Kitsune Ral <kitsune-ral@users.sf.net>
+// SPDX-License-Identifier: LGPL-2.1-or-later
#include "syncdata.h"
-#include "events/eventloader.h"
+#include "logging.h"
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
using namespace Quotient;
-const QString SyncRoomData::UnreadCountKey =
- QStringLiteral("x-quotient.unread_count");
-
bool RoomSummary::isEmpty() const
{
return !joinedMemberCount && !invitedMemberCount && !heroes;
@@ -36,9 +18,10 @@ bool RoomSummary::isEmpty() const
bool RoomSummary::merge(const RoomSummary& other)
{
// Using bitwise OR to prevent computation shortcut.
- return joinedMemberCount.merge(other.joinedMemberCount)
- | invitedMemberCount.merge(other.invitedMemberCount)
- | heroes.merge(other.heroes);
+ return static_cast<bool>(
+ static_cast<int>(joinedMemberCount.merge(other.joinedMemberCount))
+ | static_cast<int>(invitedMemberCount.merge(other.invitedMemberCount))
+ | static_cast<int>(heroes.merge(other.heroes)));
}
QDebug Quotient::operator<<(QDebug dbg, const RoomSummary& rs)
@@ -79,23 +62,23 @@ inline EventsArrayT load(const QJsonObject& batches, StrT keyName)
return fromJson<EventsArrayT>(batches[keyName].toObject().value("events"_ls));
}
-SyncRoomData::SyncRoomData(const QString& roomId_, JoinState joinState_,
- const QJsonObject& room_)
- : roomId(roomId_)
- , joinState(joinState_)
- , summary(fromJson<RoomSummary>(room_["summary"_ls]))
- , state(load<StateEvents>(room_, joinState == JoinState::Invite
+SyncRoomData::SyncRoomData(QString roomId_, JoinState joinState,
+ const QJsonObject& roomJson)
+ : roomId(std::move(roomId_))
+ , joinState(joinState)
+ , summary(fromJson<RoomSummary>(roomJson["summary"_ls]))
+ , state(load<StateEvents>(roomJson, joinState == JoinState::Invite
? "invite_state"_ls
: "state"_ls))
{
switch (joinState) {
case JoinState::Join:
- ephemeral = load<Events>(room_, "ephemeral"_ls);
+ ephemeral = load<Events>(roomJson, "ephemeral"_ls);
[[fallthrough]];
case JoinState::Leave: {
- accountData = load<Events>(room_, "account_data"_ls);
- timeline = load<RoomEvents>(room_, "timeline"_ls);
- const auto timelineJson = room_.value("timeline"_ls).toObject();
+ accountData = load<Events>(roomJson, "account_data"_ls);
+ timeline = load<RoomEvents>(roomJson, "timeline"_ls);
+ const auto timelineJson = roomJson.value("timeline"_ls).toObject();
timelineLimited = timelineJson.value("limited"_ls).toBool();
timelinePrevBatch = timelineJson.value("prev_batch"_ls).toString();
@@ -104,17 +87,52 @@ SyncRoomData::SyncRoomData(const QString& roomId_, JoinState joinState_,
default: /* nothing on top of state */;
}
- const auto unreadJson = room_.value("unread_notifications"_ls).toObject();
- unreadCount = unreadJson.value(UnreadCountKey).toInt(-2);
- highlightCount = unreadJson.value("highlight_count"_ls).toInt(-1);
- notificationCount = unreadJson.value("notification_count"_ls).toInt(-1);
+ const auto unreadJson = roomJson.value(UnreadNotificationsKey).toObject();
+
+ fromJson(unreadJson.value(PartiallyReadCountKey), partiallyReadCount);
+ if (!partiallyReadCount.has_value())
+ fromJson(unreadJson.value("x-quotient.unread_count"_ls),
+ partiallyReadCount);
+
+ fromJson(roomJson.value(NewUnreadCountKey), unreadCount);
+ if (!unreadCount.has_value())
+ fromJson(unreadJson.value("notification_count"_ls), unreadCount);
+ fromJson(unreadJson.value(HighlightCountKey), highlightCount);
+}
+
+QDebug Quotient::operator<<(QDebug dbg, const DevicesList& devicesList)
+{
+ QDebugStateSaver _(dbg);
+ QStringList sl;
+ if (!devicesList.changed.isEmpty())
+ sl << QStringLiteral("changed: %1").arg(devicesList.changed.join(", "));
+ if (!devicesList.left.isEmpty())
+ sl << QStringLiteral("left %1").arg(devicesList.left.join(", "));
+ dbg.nospace().noquote() << sl.join(QStringLiteral("; "));
+ return dbg;
+}
+
+void JsonObjectConverter<DevicesList>::dumpTo(QJsonObject& jo,
+ const DevicesList& rs)
+{
+ addParam<IfNotEmpty>(jo, QStringLiteral("changed"),
+ rs.changed);
+ addParam<IfNotEmpty>(jo, QStringLiteral("left"),
+ rs.left);
+}
+
+void JsonObjectConverter<DevicesList>::fillFrom(const QJsonObject& jo,
+ DevicesList& rs)
+{
+ fromJson(jo["changed"_ls], rs.changed);
+ fromJson(jo["left"_ls], rs.left);
}
SyncData::SyncData(const QString& cacheFileName)
{
QFileInfo cacheFileInfo { cacheFileName };
auto json = loadJson(cacheFileName);
- auto requiredVersion = std::get<0>(cacheVersion());
+ auto requiredVersion = MajorCacheVersion;
auto actualVersion =
json.value("cache_version"_ls).toObject().value("major"_ls).toInt();
if (actualVersion == requiredVersion)
@@ -125,7 +143,7 @@ SyncData::SyncData(const QString& cacheFileName)
<< "is required; discarding the cache";
}
-SyncDataList&& SyncData::takeRoomData() { return move(roomData); }
+SyncDataList SyncData::takeRoomData() { return move(roomData); }
QString SyncData::fileNameForRoom(QString roomId)
{
@@ -133,11 +151,18 @@ QString SyncData::fileNameForRoom(QString roomId)
return roomId + ".json";
}
-Events&& SyncData::takePresenceData() { return std::move(presenceData); }
+Events SyncData::takePresenceData() { return std::move(presenceData); }
-Events&& SyncData::takeAccountData() { return std::move(accountData); }
+Events SyncData::takeAccountData() { return std::move(accountData); }
-Events&& SyncData::takeToDeviceEvents() { return std::move(toDeviceEvents); }
+Events SyncData::takeToDeviceEvents() { return std::move(toDeviceEvents); }
+
+std::pair<int, int> SyncData::cacheVersion()
+{
+ return { MajorCacheVersion, 2 };
+}
+
+DevicesList SyncData::takeDevicesList() { return std::move(devicesList); }
QJsonObject SyncData::loadJson(const QString& fileName)
{
@@ -155,12 +180,7 @@ QJsonObject SyncData::loadJson(const QString& fileName)
const auto json = data.startsWith('{')
? QJsonDocument::fromJson(data).object()
-#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
- : QCborValue::fromCbor(data).toJsonValue().toObject()
-#else
- : QJsonDocument::fromBinaryData(data).object()
-#endif
- ;
+ : QCborValue::fromCbor(data).toJsonValue().toObject();
if (json.isEmpty()) {
qCWarning(MAIN) << "State cache in" << fileName
<< "is broken or empty, discarding";
@@ -181,24 +201,32 @@ void SyncData::parseJson(const QJsonObject& json, const QString& baseDir)
fromJson(json.value("device_one_time_keys_count"_ls),
deviceOneTimeKeysCount_);
+ if(json.contains("device_lists")) {
+ fromJson(json.value("device_lists"), devicesList);
+ }
+
auto rooms = json.value("rooms"_ls).toObject();
auto totalRooms = 0;
auto totalEvents = 0;
for (size_t i = 0; i < JoinStateStrings.size(); ++i) {
- // This assumes that JoinState values go over powers of 2: 1,2,4,...
+ // This assumes that MemberState values go over powers of 2: 1,2,4,...
const auto joinState = JoinState(1U << i);
const auto rs = rooms.value(JoinStateStrings[i]).toObject();
// We have a Qt container on the right and an STL one on the left
roomData.reserve(roomData.size() + static_cast<size_t>(rs.size()));
for (auto roomIt = rs.begin(); roomIt != rs.end(); ++roomIt) {
- auto roomJson =
- roomIt->isObject()
- ? roomIt->toObject()
- : loadJson(baseDir + fileNameForRoom(roomIt.key()));
- if (roomJson.isEmpty()) {
- unresolvedRoomIds.push_back(roomIt.key());
- continue;
- }
+ QJsonObject roomJson;
+ if (!baseDir.isEmpty()) {
+ // Loading data from the local cache, with room objects saved in
+ // individual files rather than inline
+ roomJson = loadJson(baseDir + fileNameForRoom(roomIt.key()));
+ if (roomJson.isEmpty()) {
+ unresolvedRoomIds.push_back(roomIt.key());
+ continue;
+ }
+ } else // When loading from /sync response, everything is inline
+ roomJson = roomIt->toObject();
+
roomData.emplace_back(roomIt.key(), joinState, roomJson);
const auto& r = roomData.back();
totalEvents += r.state.size() + r.ephemeral.size()