aboutsummaryrefslogtreecommitdiff
path: root/lib/events/accountdataevents.h
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-09-02 15:15:19 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-09-02 15:15:19 +0900
commit62ad12b69b3b085a32c9522194b7b141d2346361 (patch)
tree127b301688351104dfc5af3a009486e374e99ae3 /lib/events/accountdataevents.h
parent3d446f3ff6effb87da2e2a9df0e2c7ba9073e154 (diff)
downloadlibquotient-62ad12b69b3b085a32c9522194b7b141d2346361.tar.gz
libquotient-62ad12b69b3b085a32c9522194b7b141d2346361.zip
Switch tag order from strings to floats, as The Spec preaches
The Spec wasn't entirely consistent on this until recently but floats actually are used in the wild, rather than strings.
Diffstat (limited to 'lib/events/accountdataevents.h')
-rw-r--r--lib/events/accountdataevents.h38
1 files changed, 28 insertions, 10 deletions
diff --git a/lib/events/accountdataevents.h b/lib/events/accountdataevents.h
index 94fc510a..27f6c77c 100644
--- a/lib/events/accountdataevents.h
+++ b/lib/events/accountdataevents.h
@@ -31,22 +31,40 @@ namespace QMatrixClient
struct TagRecord
{
- TagRecord (QString order = {}) : order(std::move(order)) { }
- explicit TagRecord(const QJsonValue& jv)
- : order(jv.toObject().value("order"_ls).toString())
- { }
+ using order_type = Omittable<float>;
+
+ order_type order;
- QString order;
+ TagRecord (order_type order = none) : order(order) { }
+ explicit TagRecord(const QJsonValue& jv)
+ {
+ // Parse a float both from JSON double and JSON string because
+ // libqmatrixclient previously used to use strings to store order.
+ const auto orderJv = jv.toObject().value("order"_ls);
+ if (orderJv.isDouble())
+ order = fromJson<float>(orderJv);
+ else if (orderJv.isString())
+ {
+ bool ok;
+ order = orderJv.toString().toFloat(&ok);
+ if (!ok)
+ order = none;
+ }
+ }
- bool operator==(const TagRecord& other) const
- { return order == other.order; }
- bool operator!=(const TagRecord& other) const
- { return !operator==(other); }
+ bool operator<(const TagRecord& other) const
+ {
+ // Per The Spec, rooms with no order should be after those with order
+ return !order.omitted() &&
+ (other.order.omitted() || order.value() < other.order.value());
+ }
};
inline QJsonValue toJson(const TagRecord& rec)
{
- return QJsonObject {{ QStringLiteral("order"), rec.order }};
+ QJsonObject o;
+ addParam(o, QStringLiteral("order"), rec.order);
+ return o;
}
using TagsMap = QHash<QString, TagRecord>;