From 24e63f43b73f38a3b534682ca5c2b864863f7bf2 Mon Sep 17 00:00:00 2001 From: lostystyg Date: Thu, 8 Sep 2022 13:13:52 +0400 Subject: [PATCH] Filling notification result with account info --- .../repositories/web/WebRpcRepository.cpp | 78 ++++++++++++++++++- .../repositories/web/WebRpcRepository.h | 2 + 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/pocketdb/repositories/web/WebRpcRepository.cpp b/src/pocketdb/repositories/web/WebRpcRepository.cpp index 6b911a7ee..fcf0d05fa 100644 --- a/src/pocketdb/repositories/web/WebRpcRepository.cpp +++ b/src/pocketdb/repositories/web/WebRpcRepository.cpp @@ -30,7 +30,17 @@ namespace PocketDb } } - UniValue Serialize() const + std::set GetNotifiersAddresses() const + { + std::set res; + for (const auto& notifierEntry: m_notifiers) { + res.insert(notifierEntry.first); + } + + return res; + } + + UniValue Serialize(const std::map accsData) const { std::map> hashToIndexMap; UniValue data (UniValue::VARR); @@ -58,7 +68,9 @@ namespace PocketDb } UniValue notifier (UniValue::VOBJ); notifier.pushKV("e", e); - notifier.pushKV("i", "TODO"); + if (auto acc = accsData.find(notifiersEntry.first); acc != accsData.end()) { + notifier.pushKV("i", acc->second.Serialize()); + } notifiers.pushKV(notifiersEntry.first, notifier); } @@ -5649,6 +5661,64 @@ namespace PocketDb return reconstructor.GetResult(); } + std::map WebRpcRepository::GetShortAccountsForAddresses(const std::set& addresses) + { + auto sql = R"sql( + select + ac.String1, + p.String1, + p.String2, + p.String3, + ifnull(r.Value,0) + + from Transactions ac + + left join Payload p + on p.TxHash = ac.Hash + + left join Ratings r indexed by Ratings_Type_Id_Last_Height + on r.Type = 0 + and r.Id = ac.Id + and r.Last = 1 + + where ac.String1 in ( )sql" + join(vector(addresses.size(), "?"), ",") + R"sql( ) + and ac.Type = 100 + and ac.Last = 1 + )sql"; + + std::map res; + TryTransactionStep(__func__, [&]() + { + auto stmt = SetupSqlStatement(sql); + int i = 1; + + for (const auto& address: addresses) { + TryBindStatementText(stmt, i, address); + i++; + } + + while (sqlite3_step(*stmt) == SQLITE_ROW) + { + std::string address; + if (auto [ok, val] = TryGetColumnString(*stmt, 0); ok) + address = val; + else + continue; // TODO (losty): error + + ShortAccount acc; + if (auto [ok, val] = TryGetColumnString(*stmt, 1); ok) acc.SetLang(val); + if (auto [ok, val] = TryGetColumnString(*stmt, 2); ok) acc.SetName(val); + if (auto [ok, val] = TryGetColumnString(*stmt, 3); ok) acc.SetAvatar(val); + if (auto [ok, val] = TryGetColumnInt64(*stmt, 4); ok) acc.SetReputation(val); + res.insert({address, acc}); + } + + FinalizeSqlStatement(*stmt); + }); + + return res; + } + UniValue WebRpcRepository::GetNotifications(int64_t height, const std::set& filters) { struct QueryParams { @@ -6357,7 +6427,9 @@ namespace PocketDb } } - return reconstructor.GetResult().Serialize(); + auto notificationResult = reconstructor.GetResult(); + auto accsData = GetShortAccountsForAddresses(notificationResult.GetNotifiersAddresses()); + return notificationResult.Serialize(accsData); } std::vector WebRpcRepository::GetEventsForAddresses(const std::string& address, int64_t heightMax, int64_t heightMin, int64_t blockNumMax, const std::set& filters) diff --git a/src/pocketdb/repositories/web/WebRpcRepository.h b/src/pocketdb/repositories/web/WebRpcRepository.h index 629a201a8..f0b9c0c50 100644 --- a/src/pocketdb/repositories/web/WebRpcRepository.h +++ b/src/pocketdb/repositories/web/WebRpcRepository.h @@ -172,6 +172,8 @@ namespace PocketDb */ UniValue GetNotifications(int64_t height, const std::set& filters); + std::map GetShortAccountsForAddresses(const std::set& addresses); + /** * Get all activities (posts, comments, etc) created by address *