diff options
author | Andres Salomon <dilinger@queued.net> | 2021-01-18 04:00:14 -0500 |
---|---|---|
committer | Andres Salomon <dilinger@queued.net> | 2021-01-18 04:00:14 -0500 |
commit | 09eb39236666e81d5da014acea011dcd74d0999b (patch) | |
tree | 52876d96be71be1a39d5d935c1295a51995e8949 /lib/syncdata.cpp | |
parent | f1788ee27f33e9339334e0d79bde9a27d9ce2e44 (diff) | |
parent | a4e78956f105875625b572d8b98459ffa86fafe5 (diff) | |
download | libquotient-09eb39236666e81d5da014acea011dcd74d0999b.tar.gz libquotient-09eb39236666e81d5da014acea011dcd74d0999b.zip |
Update upstream source from tag 'upstream/0.6.4'
Update to upstream version '0.6.4'
with Debian dir aa8705fd74743e79c043bc9e3e425d5064404cfe
Diffstat (limited to 'lib/syncdata.cpp')
-rw-r--r-- | lib/syncdata.cpp | 140 |
1 files changed, 65 insertions, 75 deletions
diff --git a/lib/syncdata.cpp b/lib/syncdata.cpp index 21517884..e6472e18 100644 --- a/lib/syncdata.cpp +++ b/lib/syncdata.cpp @@ -13,7 +13,7 @@ * * 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 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "syncdata.h" @@ -23,36 +23,34 @@ #include <QtCore/QFile> #include <QtCore/QFileInfo> -using namespace QMatrixClient; +using namespace Quotient; const QString SyncRoomData::UnreadCountKey = - QStringLiteral("x-qmatrixclient.unread_count"); + QStringLiteral("x-quotient.unread_count"); bool RoomSummary::isEmpty() const { - return joinedMemberCount.omitted() && invitedMemberCount.omitted() && - heroes.omitted(); + return !joinedMemberCount && !invitedMemberCount && !heroes; } 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 joinedMemberCount.merge(other.joinedMemberCount) + | invitedMemberCount.merge(other.invitedMemberCount) + | heroes.merge(other.heroes); } -QDebug QMatrixClient::operator<<(QDebug dbg, const RoomSummary& rs) +QDebug Quotient::operator<<(QDebug dbg, const RoomSummary& rs) { QDebugStateSaver _(dbg); QStringList sl; - if (!rs.joinedMemberCount.omitted()) - sl << QStringLiteral("joined: %1").arg(rs.joinedMemberCount.value()); - if (!rs.invitedMemberCount.omitted()) - sl << QStringLiteral("invited: %1").arg(rs.invitedMemberCount.value()); - if (!rs.heroes.omitted()) - sl << QStringLiteral("heroes: [%1]").arg(rs.heroes.value().join(',')); + if (rs.joinedMemberCount) + sl << QStringLiteral("joined: %1").arg(*rs.joinedMemberCount); + if (rs.invitedMemberCount) + sl << QStringLiteral("invited: %1").arg(*rs.invitedMemberCount); + if (rs.heroes) + sl << QStringLiteral("heroes: [%1]").arg(rs.heroes->join(',')); dbg.nospace().noquote() << sl.join(QStringLiteral("; ")); return dbg; } @@ -87,23 +85,23 @@ SyncRoomData::SyncRoomData(const QString& roomId_, JoinState joinState_, , joinState(joinState_) , summary(fromJson<RoomSummary>(room_["summary"_ls])) , state(load<StateEvents>(room_, joinState == JoinState::Invite - ? "invite_state"_ls : "state"_ls)) + ? "invite_state"_ls + : "state"_ls)) { switch (joinState) { - case JoinState::Join: - ephemeral = load<Events>(room_, "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(); - timelineLimited = timelineJson.value("limited"_ls).toBool(); - timelinePrevBatch = timelineJson.value("prev_batch"_ls).toString(); - - break; - } - default: /* nothing on top of state */; + case JoinState::Join: + ephemeral = load<Events>(room_, "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(); + timelineLimited = timelineJson.value("limited"_ls).toBool(); + timelinePrevBatch = timelineJson.value("prev_batch"_ls).toString(); + + break; + } + default: /* nothing on top of state */; } const auto unreadJson = room_.value("unread_notifications"_ls).toObject(); @@ -121,20 +119,17 @@ SyncData::SyncData(const QString& cacheFileName) QFileInfo cacheFileInfo { cacheFileName }; auto json = loadJson(cacheFileName); auto requiredVersion = std::get<0>(cacheVersion()); - auto actualVersion = json.value("cache_version"_ls).toObject() - .value("major"_ls).toInt(); + auto actualVersion = + json.value("cache_version"_ls).toObject().value("major"_ls).toInt(); if (actualVersion == requiredVersion) parseJson(json, cacheFileInfo.absolutePath() + '/'); else - qCWarning(MAIN) - << "Major version of the cache file is" << actualVersion << "but" - << requiredVersion << "is required; discarding the cache"; + qCWarning(MAIN) << "Major version of the cache file is" << actualVersion + << "but" << requiredVersion + << "is required; discarding the cache"; } -SyncDataList&& SyncData::takeRoomData() -{ - return move(roomData); -} +SyncDataList&& SyncData::takeRoomData() { return move(roomData); } QString SyncData::fileNameForRoom(QString roomId) { @@ -142,42 +137,35 @@ 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); } QJsonObject SyncData::loadJson(const QString& fileName) { QFile roomFile { fileName }; - if (!roomFile.exists()) - { + if (!roomFile.exists()) { qCWarning(MAIN) << "No state cache file" << fileName; return {}; } - if(!roomFile.open(QIODevice::ReadOnly)) - { + if (!roomFile.open(QIODevice::ReadOnly)) { qCWarning(MAIN) << "Failed to open state cache file" << roomFile.fileName(); return {}; } auto data = roomFile.readAll(); - const auto json = - (data.startsWith('{') ? QJsonDocument::fromJson(data) - : QJsonDocument::fromBinaryData(data)).object(); - if (json.isEmpty()) - { + 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 + ; + if (json.isEmpty()) { qCWarning(MAIN) << "State cache in" << fileName << "is broken or empty, discarding"; } @@ -186,36 +174,38 @@ QJsonObject SyncData::loadJson(const QString& fileName) void SyncData::parseJson(const QJsonObject& json, const QString& baseDir) { - QElapsedTimer et; et.start(); + QElapsedTimer et; + et.start(); nextBatch_ = json.value("next_batch"_ls).toString(); presenceData = load<Events>(json, "presence"_ls); accountData = load<Events>(json, "account_data"_ls); toDeviceEvents = load<Events>(json, "to_device"_ls); + fromJson(json.value("device_one_time_keys_count"_ls), + deviceOneTimeKeysCount_); + auto rooms = json.value("rooms"_ls).toObject(); JoinStates::Int ii = 1; // ii is used to make a JoinState value auto totalRooms = 0; auto totalEvents = 0; - for (size_t i = 0; i < JoinStateStrings.size(); ++i, ii <<= 1) - { + for (size_t i = 0; i < JoinStateStrings.size(); ++i, ii <<= 1) { 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(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()) - { + 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; } roomData.emplace_back(roomIt.key(), JoinState(ii), roomJson); const auto& r = roomData.back(); - totalEvents += r.state.size() + r.ephemeral.size() + - r.accountData.size() + r.timeline.size(); + totalEvents += r.state.size() + r.ephemeral.size() + + r.accountData.size() + r.timeline.size(); } totalRooms += rs.size(); } @@ -223,6 +213,6 @@ void SyncData::parseJson(const QJsonObject& json, const QString& baseDir) qCWarning(MAIN) << "Unresolved rooms:" << unresolvedRoomIds.join(','); if (totalRooms > 9 || et.nsecsElapsed() >= profilerMinNsecs()) qCDebug(PROFILER) << "*** SyncData::parseJson(): batch with" - << totalRooms << "room(s)," - << totalEvents << "event(s) in" << et; + << totalRooms << "room(s)," << totalEvents + << "event(s) in" << et; } |