diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-03-04 15:21:25 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-03-04 15:21:25 +0900 |
commit | 5f346a377c6c2e1989c61ba62cc08e1629ab15f4 (patch) | |
tree | f6d70873fc30a5d7b09d8164e3385fb484bc0c9a /room.cpp | |
parent | 2222bb226f43e9419c4a231c9f95da8466de4c0a (diff) | |
download | libquotient-5f346a377c6c2e1989c61ba62cc08e1629ab15f4.tar.gz libquotient-5f346a377c6c2e1989c61ba62cc08e1629ab15f4.zip |
Room::toJson(): Use QJsonObject/Array modern constructors; check execution time
Now that we're on Qt 5.6 we can afford brace-initialization of those objects.
Diffstat (limited to 'room.cpp')
-rw-r--r-- | room.cpp | 115 |
1 files changed, 49 insertions, 66 deletions
@@ -1572,87 +1572,67 @@ void Room::Private::updateDisplayname() emit q->displaynameChanged(q); } -template <typename T> void appendStateEvent(QJsonArray& events, const QString& type, - const QString& name, const T& content) + const QJsonObject& content, const QString& stateKey = {}) { - if (content.isEmpty()) - return; - - QJsonObject contentObj; - contentObj.insert(name, content); - - QJsonObject eventObj; - eventObj.insert("type", type); - eventObj.insert("content", contentObj); - eventObj.insert("state_key", {}); // Mandatory for state events - - events.append(eventObj); + if (!content.isEmpty() || !stateKey.isEmpty()) + events.append(QJsonObject + { { QStringLiteral("type"), type } + , { QStringLiteral("content"), content } + , { QStringLiteral("state_key"), stateKey } + }); } +#define ADD_STATE_EVENT(events, type, name, content) \ + appendStateEvent((events), QStringLiteral(type), \ + {{ QStringLiteral(name), content }}); + QJsonObject Room::Private::toJson() const { + QElapsedTimer et; et.start(); QJsonObject result; { QJsonArray stateEvents; - appendStateEvent(stateEvents, "m.room.name", "name", name); - appendStateEvent(stateEvents, "m.room.topic", "topic", topic); - appendStateEvent(stateEvents, "m.room.avatar", "url", - avatar.url().toString()); - appendStateEvent(stateEvents, "m.room.aliases", "aliases", - QJsonArray::fromStringList(aliases)); - appendStateEvent(stateEvents, "m.room.canonical_alias", "alias", - canonicalAlias); + ADD_STATE_EVENT(stateEvents, "m.room.name", "name", name); + ADD_STATE_EVENT(stateEvents, "m.room.topic", "topic", topic); + ADD_STATE_EVENT(stateEvents, "m.room.avatar", "url", + avatar.url().toString()); + ADD_STATE_EVENT(stateEvents, "m.room.aliases", "aliases", + QJsonArray::fromStringList(aliases)); + ADD_STATE_EVENT(stateEvents, "m.room.canonical_alias", "alias", + canonicalAlias); for (const auto *m : membersMap) - { - QJsonObject content; - content.insert("membership", QStringLiteral("join")); - content.insert("displayname", m->name(q)); - content.insert("avatar_url", m->avatarUrl(q).toString()); - - QJsonObject memberEvent; - memberEvent.insert("type", QStringLiteral("m.room.member")); - memberEvent.insert("state_key", m->id()); - memberEvent.insert("content", content); - stateEvents.append(memberEvent); - } - - QJsonObject roomStateObj; - roomStateObj.insert("events", stateEvents); - - result.insert( - joinState == JoinState::Invite ? "invite_state" : "state", - roomStateObj); + appendStateEvent(stateEvents, QStringLiteral("m.room.member"), + { { QStringLiteral("membership"), QStringLiteral("join") } + , { QStringLiteral("displayname"), m->name(q) } + , { QStringLiteral("avatar_url"), m->avatarUrl(q).toString() } + }, m->id()); + + const auto stateObjName = joinState == JoinState::Invite ? + QStringLiteral("invite_state") : QStringLiteral("state"); + result.insert(stateObjName, + QJsonObject {{ QStringLiteral("events"), stateEvents }}); } if (!q->readMarkerEventId().isEmpty()) { - QJsonArray ephemeralEvents; - { - // Don't dump the timestamp because it's useless in the cache. - QJsonObject user; - user.insert(connection->userId(), {}); - - QJsonObject receipt; - receipt.insert("m.read", user); - - QJsonObject lastReadEvent; - lastReadEvent.insert(q->readMarkerEventId(), receipt); - lastReadEvent.insert("x-qmatrixclient.unread_messages", - unreadMessages); - - QJsonObject receiptsObj; - receiptsObj.insert("type", QStringLiteral("m.receipt")); - receiptsObj.insert("content", lastReadEvent); - ephemeralEvents.append(receiptsObj); - } - - QJsonObject ephemeralObj; - ephemeralObj.insert("events", ephemeralEvents); - - result.insert("ephemeral", ephemeralObj); + result.insert(QStringLiteral("ephemeral"), + QJsonObject {{ QStringLiteral("events"), + QJsonArray { QJsonObject( + { { QStringLiteral("type"), QStringLiteral("m.receipt") } + , { QStringLiteral("content"), QJsonObject( + { { q->readMarkerEventId(), + QJsonObject {{ QStringLiteral("m.read"), + QJsonObject {{ connection->userId(), {} }} }} + } + , { QStringLiteral("x-qmatrixclient.unread_messages"), + unreadMessages } + }) } + } + ) } + }}); } QJsonArray accountDataEvents; @@ -1668,7 +1648,7 @@ QJsonObject Room::Private::toJson() const }); } QJsonObject accountDataEventsObj; - result.insert("account_data", QJsonObject { {"events", accountDataEvents} }); + result.insert("account_data", QJsonObject {{ "events", accountDataEvents }}); QJsonObject unreadNotificationsObj; if (highlightCount > 0) @@ -1678,6 +1658,9 @@ QJsonObject Room::Private::toJson() const if (!unreadNotificationsObj.isEmpty()) result.insert("unread_notifications", unreadNotificationsObj); + if (et.elapsed() > 50) + qCDebug(PROFILER) << "Room::toJson() for" << displayname << "took" << et; + return result; } |