From d51684b759f686b2c9895c5013dce88fead9661b Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 8 Dec 2018 22:42:25 +0900 Subject: MSC 688: MSC-compliant RoomSummary; update Room::calculateDisplayname() The members of the summary can be omitted in the payload; this change fixes calculation of the roomname from hero names passed in room summary. Also: RoomSummary can be dumped to QDebug now. --- lib/syncdata.cpp | 57 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 19 deletions(-) (limited to 'lib/syncdata.cpp') diff --git a/lib/syncdata.cpp b/lib/syncdata.cpp index a5f849b3..f55d4396 100644 --- a/lib/syncdata.cpp +++ b/lib/syncdata.cpp @@ -28,45 +28,64 @@ using namespace QMatrixClient; const QString SyncRoomData::UnreadCountKey = QStringLiteral("x-qmatrixclient.unread_count"); -template -inline EventsArrayT load(const QJsonObject& batches, StrT keyName) +bool RoomSummary::isEmpty() const { - return fromJson(batches[keyName].toObject().value("events"_ls)); + return joinedMemberCount.omitted() && invitedMemberCount.omitted() && + heroes.omitted(); +} + +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); +} + +QDebug QMatrixClient::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(',')); + dbg.nospace().noquote() << sl.join(QStringLiteral("; ")); + return dbg; } void JsonObjectConverter::dumpTo(QJsonObject& jo, const RoomSummary& rs) { - if (rs.joinedMemberCount != 0) - jo.insert(QStringLiteral("m.joined_member_count"), - rs.joinedMemberCount); - if (rs.invitedMemberCount != 0) - jo.insert(QStringLiteral("m.invited_member_count"), - rs.invitedMemberCount); - if (!rs.heroes.empty()) - jo.insert(QStringLiteral("m.heroes"), toJson(rs.heroes)); + addParam(jo, QStringLiteral("m.joined_member_count"), + rs.joinedMemberCount); + addParam(jo, QStringLiteral("m.invited_member_count"), + rs.invitedMemberCount); + addParam(jo, QStringLiteral("m.heroes"), rs.heroes); } void JsonObjectConverter::fillFrom(const QJsonObject& jo, RoomSummary& rs) { - rs.joinedMemberCount = fromJson(jo["m.joined_member_count"_ls]); - rs.joinedMemberCount = fromJson(jo["m.invited_member_count"_ls]); - rs.heroes = fromJson(jo["m.heroes"]); + fromJson(jo["m.joined_member_count"_ls], rs.joinedMemberCount); + fromJson(jo["m.invited_member_count"_ls], rs.invitedMemberCount); + fromJson(jo["m.heroes"], rs.heroes); } -bool RoomSummary::operator==(const RoomSummary& other) const +template +inline EventsArrayT load(const QJsonObject& batches, StrT keyName) { - return joinedMemberCount == other.joinedMemberCount && - invitedMemberCount == other.invitedMemberCount && - heroes == other.heroes; + return fromJson(batches[keyName].toObject().value("events"_ls)); } SyncRoomData::SyncRoomData(const QString& roomId_, JoinState joinState_, const QJsonObject& room_) : roomId(roomId_) , joinState(joinState_) - , summary(fromJson(room_["summary"].toObject())) + , summary(fromJson(room_["summary"])) , state(load(room_, joinState == JoinState::Invite ? "invite_state"_ls : "state"_ls)) { -- cgit v1.2.3