diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-07-28 18:00:59 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-07-28 18:00:59 +0900 |
commit | 53521f42d15d2553028bae09f1cd12206bc32955 (patch) | |
tree | dd8ef113bdeb1906684b3ad11ada409f8cf9cc19 | |
parent | 84aa055bba602635599def37915b96ccf3f63484 (diff) | |
download | libquotient-53521f42d15d2553028bae09f1cd12206bc32955.tar.gz libquotient-53521f42d15d2553028bae09f1cd12206bc32955.zip |
Room: validate tags before sending them to the server
See https://github.com/matrix-org/matrix-doc/pull/1457 for the background.
-rw-r--r-- | lib/room.cpp | 30 | ||||
-rw-r--r-- | lib/room.h | 2 |
2 files changed, 28 insertions, 4 deletions
diff --git a/lib/room.cpp b/lib/room.cpp index 4b349f44..af6ef8c4 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -689,12 +689,27 @@ TagRecord Room::tag(const QString& name) const return d->tags.value(name); } +std::pair<bool, QString> validatedTag(QString name) +{ + if (name.contains('.')) + return { false, name }; + + qWarning(MAIN) << "The tag" << name + << "doesn't follow the CS API conventions, check your client code"; + name.prepend("u."); + qWarning(MAIN) << "Using " << name << "instead"; + + return { true, name }; +} + void Room::addTag(const QString& name, const TagRecord& record) { - if (d->tags.contains(name)) + const auto& checkRes = validatedTag(name); + if (d->tags.contains(name) || + (checkRes.first && d->tags.contains(checkRes.second))) return; - d->tags.insert(name, record); + d->tags.insert(checkRes.second, record); d->broadcastTagUpdates(); } @@ -712,10 +727,19 @@ void Room::removeTag(const QString& name) d->broadcastTagUpdates(); } -void Room::setTags(const TagsMap& newTags) +void Room::setTags(TagsMap newTags) { if (newTags == d->tags) return; + + const auto& tagNames = newTags.keys(); + for (const auto& t: tagNames) + { + const auto& checkRes = validatedTag(t); + if (checkRes.first) + newTags.insert(checkRes.second, newTags.take(t)); + } + d->tags = newTags; d->broadcastTagUpdates(); } @@ -315,7 +315,7 @@ namespace QMatrixClient * (because tags are saved in account data rather than in shared * room state). */ - void setTags(const TagsMap& newTags); + void setTags(TagsMap newTags); /** Check whether the list of tags has m.favourite */ bool isFavourite() const; |