Skip to content

Commit

Permalink
Added GetContentActions
Browse files Browse the repository at this point in the history
  • Loading branch information
only1question committed Apr 12, 2022
1 parent 11e83ec commit 0082bf7
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 1 deletion.
150 changes: 150 additions & 0 deletions src/pocketdb/repositories/web/WebRpcRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3705,4 +3705,154 @@ namespace PocketDb
return result;
}

UniValue WebRpcRepository::GetContentActions(const string& postTxHash)
{
auto func = __func__;
UniValue resultScores(UniValue::VARR);
UniValue resultBoosts(UniValue::VARR);
UniValue resultDonations(UniValue::VARR);
UniValue result(UniValue::VOBJ);

string sql = R"sql(
--scores
select
s.String2 as ContentTxHash,
s.String1 as AddressHash,
p.String2 as AccountName,
p.String3 as AccountAvatar,
r.Value as AccountReputation,
s.Int1 as ScoreValue,
0 as sumBoost,
0 as sumDonation
from Transactions s indexed by Transactions_Type_Last_String2_Height
cross join Transactions u indexed by Transactions_Type_Last_String1_Height_Id
on u.String1 = s.String1 and u.Type in (100) and u.Last = 1 and u.Height > 0
left join Ratings r indexed by Ratings_Type_Id_Last_Value
on r.Id = u.Id and r.Type = 0 and r.Last = 1
cross join Payload p on p.TxHash = u.Hash
where s.Type in (300)
and s.Last in (0,1)
and s.Height > 0
and s.String2 = ?
--boosts
union
select
tb.String2 as ContentTxHash,
tb.String1 as AddressHash,
p.String2 as AccountName,
p.String3 as AccountAvatar,
r.Value as AccountReputation,
0 as ScoreValue,
tb.sumBoost as sumBoost,
0 as sumDonation
from
(
select
b.String1,
b.String2,
sum(b.Int1) as sumBoost
from Transactions b indexed by Transactions_Type_Last_String2_Height
where b.Type in (208)
and b.Last in (0,1)
and b.Height > 0
and b.String2 = ?
group by
b.String1,
b.String2
)tb
cross join Transactions u indexed by Transactions_Type_Last_String1_Height_Id
on u.String1 = tb.String1 and u.Type in (100) and u.Last = 1 and u.Height > 0
left join Ratings r indexed by Ratings_Type_Id_Last_Value
on r.Id = u.Id and r.Type = 0 and r.Last = 1
cross join Payload p on p.TxHash = u.Hash
--donations
union
select
td.String3 as ContentTxHash,
td.String1 as AddressHash,
p.String2 as AccountName,
p.String3 as AccountAvatar,
r.Value as AccountReputation,
0 as ScoreValue,
0 as sumBoost,
td.sumDonation as sumDonation
from
(
select
comment.String1,
comment.String3,
sum(o.Value) as sumDonation
from Transactions comment indexed by Transactions_Type_Last_String3_Height
join Transactions content indexed by Transactions_Type_Last_String2_Height
on content.String2 = comment.String3
and content.Type in (200,201,202)
and content.Last = 1
and content.Height is not null
join TxOutputs o indexed by TxOutputs_TxHash_AddressHash_Value
on o.TxHash = comment.Hash
and o.AddressHash = content.String1
and o.AddressHash != comment.String1
and o.Value > 0
where comment.Type in (204, 205, 206)
and comment.Height is not null
and comment.Last = 1
and comment.String3 = ?
group by
comment.String1,
comment.String3
)td
cross join Transactions u indexed by Transactions_Type_Last_String1_Height_Id
on u.String1 = td.String1 and u.Type in (100) and u.Last = 1 and u.Height > 0
left join Ratings r indexed by Ratings_Type_Id_Last_Value
on r.Id = u.Id and r.Type = 0 and r.Last = 1
cross join Payload p on p.TxHash = u.Hash
)sql";

TryTransactionStep(func, [&]()
{
auto stmt = SetupSqlStatement(sql);

int i = 1;
TryBindStatementText(stmt, i++, postTxHash);
TryBindStatementText(stmt, i++, postTxHash);
TryBindStatementText(stmt, i++, postTxHash);

// ---------------------------------------------

while (sqlite3_step(*stmt) == SQLITE_ROW)
{
UniValue record(UniValue::VOBJ);
if (auto[ok, value] = TryGetColumnString(*stmt, 0); ok) record.pushKV("posttxid", value);
if (auto[ok, value] = TryGetColumnString(*stmt, 1); ok) record.pushKV("address", value);
if (auto[ok, value] = TryGetColumnString(*stmt, 2); ok) record.pushKV("name", value);
if (auto[ok, value] = TryGetColumnString(*stmt, 3); ok) record.pushKV("avatar", value);
if (auto[ok, value] = TryGetColumnString(*stmt, 4); ok) record.pushKV("reputation", value);
if (auto[ok, value] = TryGetColumnInt(*stmt, 5); ok && value > 0) {
record.pushKV("value", value);
resultScores.push_back(record);
}
if (auto[ok, value] = TryGetColumnInt(*stmt, 6); ok && value > 0) {
record.pushKV("value", value);
resultBoosts.push_back(record);
}
if (auto[ok, value] = TryGetColumnInt(*stmt, 7); ok && value > 0) {
record.pushKV("value", value);
resultDonations.push_back(record);
}
}

FinalizeSqlStatement(*stmt);
});

result.pushKV("scores",resultScores);
result.pushKV("boosts",resultBoosts);
result.pushKV("donations",resultDonations);

return result;
}

}
2 changes: 2 additions & 0 deletions src/pocketdb/repositories/web/WebRpcRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ namespace PocketDb

vector<int64_t> GetRandomContentIds(const string& lang, int count, int height);

UniValue GetContentActions(const string& postTxHash);

// TODO (o1q): Remove this two methods when the client gui switches to new methods
UniValue GetProfileFeedOld(const string& addressFrom, const string& addressTo, int64_t topContentId, int count, const string& lang, const vector<string>& tags, const vector<int>& contentTypes);
UniValue GetSubscribesFeedOld(const string& addressFrom, int64_t topContentId, int count, const string& lang, const vector<string>& tags, const vector<int>& contentTypes);
Expand Down
13 changes: 13 additions & 0 deletions src/pocketdb/web/PocketContentRpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,4 +647,17 @@ namespace PocketWeb::PocketWebRpc

return result;
}

UniValue GetContentActions(const JSONRPCRequest& request)
{
if (request.fHelp)
throw std::runtime_error(
"getcontentactions\n"
"\nGet profiles that performed actions(score/boos/donate) on content.\n");

RPCTypeCheck(request.params, {UniValue::VSTR});

auto contentHash = request.params[0].get_str();
return request.DbConnection()->WebRpcRepoInst->GetContentActions(contentHash);
}
}
3 changes: 2 additions & 1 deletion src/pocketdb/web/PocketContentRpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace PocketWeb::PocketWebRpc
UniValue FeedSelector(const JSONRPCRequest& request);
UniValue GetContentsStatistic(const JSONRPCRequest& request);
UniValue GetRandomContents(const JSONRPCRequest& request);

UniValue GetContentActions(const JSONRPCRequest& request);

}

#endif //SRC_POCKETDEBUG_H
1 change: 1 addition & 0 deletions src/pocketdb/web/PocketRpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ static const CRPCCommand commands[] =
{"contents", "getcontentsstatistic", &GetContentsStatistic, {"addresses", "contentTypes", "height", "depth"}},
{"contents", "getcontents", &GetContents, {"address"}},
{"contents", "getrandomcontents", &GetRandomContents, {}},
{"contents", "getcontentactions", &GetContentActions, {"contentHash"}},

// Tags
// {"artifacts", "searchtags", &gettemplate, {"search_string", "count"}},
Expand Down

1 comment on commit 0082bf7

@only1question
Copy link
Collaborator Author

@only1question only1question commented on 0082bf7 Apr 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a method that returns active actions on the content (scores, boost, donate)
#271

Please sign in to comment.