From 9e58bbe349205ded067fdc92c221fa128a05820f Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 22 Mar 2020 21:05:21 +0100 Subject: Fix missing alias updates; make Room aliases accessors work (using MSC2432) Backport of #383 to 0.5.x branch. Fixes #301. --- lib/room.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++----------- lib/room.h | 1 + 2 files changed, 44 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/room.cpp b/lib/room.cpp index 9e7ff8d2..0942d730 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -377,7 +377,17 @@ QString Room::name() const QStringList Room::aliases() const { - return d->getCurrentState()->aliases(); + const auto* evt = d->getCurrentState(); + auto aliases = fromJson(evt->contentJson()["alt_aliases"]); + if (!evt->alias().isEmpty()) + aliases << evt->alias(); + return aliases; +} + +QStringList Room::altAliases() const +{ + const auto* evt = d->getCurrentState(); + return fromJson(evt->contentJson()["alt_aliases"]); } QString Room::canonicalAlias() const @@ -1331,7 +1341,7 @@ void Room::updateData(SyncRoomData&& data, bool fromCache) if (roomChanges&TopicChange) emit topicChanged(); - if (roomChanges&NameChange) + if (roomChanges&(NameChange|CanonicalAliasChange)) emit namesChanged(this); if (roomChanges&MembersChange) @@ -1608,12 +1618,18 @@ void Room::setName(const QString& newName) void Room::setCanonicalAlias(const QString& newAlias) { - d->requestSetState(RoomCanonicalAliasEvent(newAlias)); + connection()->callApi( + id(), RoomCanonicalAliasEvent::matrixTypeId(), + QJsonObject { { "alias", newAlias }, + { "alt_aliases", QMatrixClient::toJson(altAliases()) } }); } void Room::setAliases(const QStringList& aliases) { - d->requestSetState(RoomAliasesEvent(aliases)); + connection()->callApi( + id(), RoomCanonicalAliasEvent::matrixTypeId(), + QJsonObject { { "alias", canonicalAlias() }, + { "alt_aliases", QMatrixClient::toJson(aliases) } }); } void Room::setTopic(const QString& newTopic) @@ -2183,15 +2199,31 @@ Room::Changes Room::processStateEvent(const RoomEvent& e) , [] (const RoomNameEvent&) { return NameChange; } - , [this,oldStateEvent] (const RoomAliasesEvent& ae) { - const auto previousAliases = oldStateEvent - ? static_cast(oldStateEvent)->aliases() - : QStringList(); - connection()->updateRoomAliases(id(), previousAliases, ae.aliases()); - return OtherChange; + , [] (const RoomAliasesEvent&) { + // This event has been removed by MSC-2432 + return NoChange; } - , [this] (const RoomCanonicalAliasEvent& evt) { + , [this, oldStateEvent] (const RoomCanonicalAliasEvent& evt) { setObjectName(evt.alias().isEmpty() ? d->id : evt.alias()); + + auto prevAliases = oldStateEvent ? fromJson( + oldStateEvent->contentJson()["alt_aliases"]) + : QStringList(); + if (oldStateEvent) { + const auto prevCanonicalAlias = + static_cast(oldStateEvent) + ->alias(); + if (!prevCanonicalAlias.isEmpty()) + prevAliases.push_back(prevCanonicalAlias); + } + + auto newAliases = + fromJson(evt.contentJson()["alt_aliases"]); + if (!evt.alias().isEmpty()) + newAliases.push_back(evt.alias()); + + connection()->updateRoomAliases(id(), prevAliases, newAliases); + return CanonicalAliasChange; } , [] (const RoomTopicEvent&) { diff --git a/lib/room.h b/lib/room.h index 33d1f4ea..9fd850e2 100644 --- a/lib/room.h +++ b/lib/room.h @@ -154,6 +154,7 @@ namespace QMatrixClient QString successorId() const; QString name() const; QStringList aliases() const; + QStringList altAliases() const; QString canonicalAlias() const; QString displayName() const; QString topic() const; -- cgit v1.2.3