diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-12-08 15:36:04 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-12-08 20:12:22 +0900 |
commit | 3392e66fd015e191b01f6e3fc6839edc3948e31f (patch) | |
tree | c839259aece7462d978f7aa9eeb712edd932cc98 /lib/csapi/rooms.cpp | |
parent | 95d4df58b39962f771885a6615efe1a682aab356 (diff) | |
download | libquotient-3392e66fd015e191b01f6e3fc6839edc3948e31f.tar.gz libquotient-3392e66fd015e191b01f6e3fc6839edc3948e31f.zip |
Refactor toJson/fillJson
Both now use through a common JsonConverter<> template class with its
base definition tuned for structs/QJsonObjects and specialisations for
non-object types. This new implementation doesn't work with virtual
fillJson functions yet (so EventContent classes still use toJson as a
member function) and does not cope quite well with non-constructible
objects (you have to specialise JsonConverter<> rather than, more
intuitively, JsonObjectConverter<>), but overall is more streamlined
compared to the previous implementation. It also fixes one important
issue that pushed for a rewrite: the previous implementation was not
working with structure hierarchies at all so (in particular) the Filter
part of CS API was totally disfunctional.
Diffstat (limited to 'lib/csapi/rooms.cpp')
-rw-r--r-- | lib/csapi/rooms.cpp | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/lib/csapi/rooms.cpp b/lib/csapi/rooms.cpp index cebb295a..0b08ccec 100644 --- a/lib/csapi/rooms.cpp +++ b/lib/csapi/rooms.cpp @@ -42,7 +42,7 @@ EventPtr&& GetOneRoomEventJob::data() BaseJob::Status GetOneRoomEventJob::parseJson(const QJsonDocument& data) { - d->data = fromJson<EventPtr>(data); + fromJson(data, d->data); return Success; } @@ -104,7 +104,7 @@ StateEvents&& GetRoomStateJob::data() BaseJob::Status GetRoomStateJob::parseJson(const QJsonDocument& data) { - d->data = fromJson<StateEvents>(data); + fromJson(data, d->data); return Success; } @@ -114,17 +114,28 @@ class GetMembersByRoomJob::Private EventsArray<RoomMemberEvent> chunk; }; -QUrl GetMembersByRoomJob::makeRequestUrl(QUrl baseUrl, const QString& roomId) +BaseJob::Query queryToGetMembersByRoom(const QString& at, const QString& membership, const QString& notMembership) +{ + BaseJob::Query _q; + addParam<IfNotEmpty>(_q, QStringLiteral("at"), at); + addParam<IfNotEmpty>(_q, QStringLiteral("membership"), membership); + addParam<IfNotEmpty>(_q, QStringLiteral("not_membership"), notMembership); + return _q; +} + +QUrl GetMembersByRoomJob::makeRequestUrl(QUrl baseUrl, const QString& roomId, const QString& at, const QString& membership, const QString& notMembership) { return BaseJob::makeRequestUrl(std::move(baseUrl), - basePath % "/rooms/" % roomId % "/members"); + basePath % "/rooms/" % roomId % "/members", + queryToGetMembersByRoom(at, membership, notMembership)); } static const auto GetMembersByRoomJobName = QStringLiteral("GetMembersByRoomJob"); -GetMembersByRoomJob::GetMembersByRoomJob(const QString& roomId) +GetMembersByRoomJob::GetMembersByRoomJob(const QString& roomId, const QString& at, const QString& membership, const QString& notMembership) : BaseJob(HttpVerb::Get, GetMembersByRoomJobName, - basePath % "/rooms/" % roomId % "/members") + basePath % "/rooms/" % roomId % "/members", + queryToGetMembersByRoom(at, membership, notMembership)) , d(new Private) { } @@ -139,7 +150,7 @@ EventsArray<RoomMemberEvent>&& GetMembersByRoomJob::chunk() BaseJob::Status GetMembersByRoomJob::parseJson(const QJsonDocument& data) { auto json = data.object(); - d->chunk = fromJson<EventsArray<RoomMemberEvent>>(json.value("chunk"_ls)); + fromJson(json.value("chunk"_ls), d->chunk); return Success; } @@ -147,17 +158,12 @@ namespace QMatrixClient { // Converters - template <> struct FromJsonObject<GetJoinedMembersByRoomJob::RoomMember> + template <> struct JsonObjectConverter<GetJoinedMembersByRoomJob::RoomMember> { - GetJoinedMembersByRoomJob::RoomMember operator()(const QJsonObject& jo) const + static void fillFrom(const QJsonObject& jo, GetJoinedMembersByRoomJob::RoomMember& result) { - GetJoinedMembersByRoomJob::RoomMember result; - result.displayName = - fromJson<QString>(jo.value("display_name"_ls)); - result.avatarUrl = - fromJson<QString>(jo.value("avatar_url"_ls)); - - return result; + fromJson(jo.value("display_name"_ls), result.displayName); + fromJson(jo.value("avatar_url"_ls), result.avatarUrl); } }; } // namespace QMatrixClient @@ -193,7 +199,7 @@ const QHash<QString, GetJoinedMembersByRoomJob::RoomMember>& GetJoinedMembersByR BaseJob::Status GetJoinedMembersByRoomJob::parseJson(const QJsonDocument& data) { auto json = data.object(); - d->joined = fromJson<QHash<QString, RoomMember>>(json.value("joined"_ls)); + fromJson(json.value("joined"_ls), d->joined); return Success; } |