Skip to content

Commit

Permalink
Filling notification result with account info
Browse files Browse the repository at this point in the history
  • Loading branch information
lostystyg committed Sep 8, 2022
1 parent 1344fe5 commit 24e63f4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
78 changes: 75 additions & 3 deletions src/pocketdb/repositories/web/WebRpcRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ namespace PocketDb
}
}

UniValue Serialize() const
std::set<std::string> GetNotifiersAddresses() const
{
std::set<std::string> res;
for (const auto& notifierEntry: m_notifiers) {
res.insert(notifierEntry.first);
}

return res;
}

UniValue Serialize(const std::map<std::string, ShortAccount> accsData) const
{
std::map<std::string, std::pair<ShortTxType, int>> hashToIndexMap;
UniValue data (UniValue::VARR);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -5649,6 +5661,64 @@ namespace PocketDb
return reconstructor.GetResult();
}

std::map<std::string, ShortAccount> WebRpcRepository::GetShortAccountsForAddresses(const std::set<std::string>& 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<string>(addresses.size(), "?"), ",") + R"sql( )
and ac.Type = 100
and ac.Last = 1
)sql";

std::map<std::string, ShortAccount> 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<ShortTxType>& filters)
{
struct QueryParams {
Expand Down Expand Up @@ -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<ShortForm> WebRpcRepository::GetEventsForAddresses(const std::string& address, int64_t heightMax, int64_t heightMin, int64_t blockNumMax, const std::set<ShortTxType>& filters)
Expand Down
2 changes: 2 additions & 0 deletions src/pocketdb/repositories/web/WebRpcRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ namespace PocketDb
*/
UniValue GetNotifications(int64_t height, const std::set<ShortTxType>& filters);

std::map<std::string, ShortAccount> GetShortAccountsForAddresses(const std::set<std::string>& addresses);

/**
* Get all activities (posts, comments, etc) created by address
*
Expand Down

0 comments on commit 24e63f4

Please sign in to comment.