Skip to content

Commit

Permalink
Merge pull request #302 from lostystyg/feature/events
Browse files Browse the repository at this point in the history
feature: events, push notifications and activities
  • Loading branch information
lostystyg authored Sep 9, 2022
2 parents 88af784 + b6b9f45 commit 7b7338c
Show file tree
Hide file tree
Showing 28 changed files with 3,062 additions and 668 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,8 @@ add_library(${POCKETDB}
pocketdb/models/shortform/ShortAccount.h
pocketdb/models/shortform/ShortAccount.cpp
pocketdb/models/shortform/ShortTxType.h
pocketdb/models/shortform/ShortTxOutput.h
pocketdb/models/shortform/ShortTxOutput.cpp
)
target_link_libraries(${POCKETDB} PRIVATE ${POCKETCOIN_COMMON} ${POCKETCOIN_UTIL} ${POCKETCOIN_CRYPTO} univalue leveldb)

Expand Down
6 changes: 4 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ POCKETDB_H = \
pocketdb/models/shortform/ShortForm.h \
pocketdb/models/shortform/ShortAccount.h \
pocketdb/models/shortform/ShortTxData.h \
pocketdb/models/shortform/ShortTxType.h
pocketdb/models/shortform/ShortTxType.h \
pocketdb/models/shortform/ShortTxOutput.h

# PocketDb CPP
POCKETDB_CPP = \
Expand Down Expand Up @@ -310,7 +311,8 @@ POCKETDB_CPP = \
pocketdb/models/dto/moderation/Flag.cpp \
pocketdb/models/shortform/ShortForm.cpp \
pocketdb/models/shortform/ShortAccount.cpp \
pocketdb/models/shortform/ShortTxData.cpp
pocketdb/models/shortform/ShortTxData.cpp \
pocketdb/models/shortform/ShortTxOutput.cpp

# CORE
POCKETCOIN_CORE_H = \
Expand Down
1 change: 1 addition & 0 deletions src/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ void HTTPSocket::BindAddress(std::string ipAddr, int port)
{
CNetAddr addr;
if (ipAddr.empty() || (LookupHost(ipAddr, addr, false) && addr.IsBindAny())) {
// TODO (brangr, lostystyg): only for private ports
LogPrintf("WARNING: the RPC server is not safe to expose to untrusted networks such as the public internet\n");
}
m_boundSockets.push_back(bind_handle);
Expand Down
8 changes: 4 additions & 4 deletions src/pocketdb/helpers/PocketnetHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace PocketHelpers
{
using namespace std;

static map<string, vector<string>> PocketnetDevelopers{
{"main", {
static map<NetworkId, vector<string>> PocketnetDevelopers{
{NetworkId::NetworkMain, {
"P9EkPPJPPRYxmK541WJkmH8yBM4GuWDn2m",
"PUGBtfmivvcg1afnEt9vqVi3yZ7v6S9BcC",
"PDtuJDVXaq82HH7fafgwBmcoxbqqWdJ9u9",
Expand All @@ -28,7 +28,7 @@ namespace PocketHelpers
"PMyjUzHtzsmbejB87ATbrcQNatiGsT4NzG",
"PHdW4pwWbFdoofVhSEfPSHgradmrvZdbE5"
}},
{"test", {
{NetworkId::NetworkTest, {
"TG69Jioc81PiwMAJtRanfZqUmRY4TUG7nt",
"TLnfXcFNxxrpEUUzrzZvbW7b9gWFtAcc8x",
"TYMo5HRFpc7tqzccaVifx7s2x2ZDqMikCR",
Expand All @@ -37,7 +37,7 @@ namespace PocketHelpers

static inline bool IsDeveloper(const string& address)
{
auto net = Params().NetworkIDString();
auto net = Params().NetworkID();
return find(PocketnetDevelopers[net].begin(), PocketnetDevelopers[net].end(), address) != PocketnetDevelopers[net].end();
}

Expand Down
56 changes: 55 additions & 1 deletion src/pocketdb/helpers/ShortFormHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static const std::map<PocketDb::ShortTxType, std::string>& GetTypesMap() {
{ PocketDb::ShortTxType::PrivateContent, "privatecontent" },
{ PocketDb::ShortTxType::Boost, "boost" },
{ PocketDb::ShortTxType::Repost, "repost" },
{ PocketDb::ShortTxType::Blocking, "blocking" }
};
return typesMap;
}
Expand All @@ -41,7 +42,60 @@ bool PocketHelpers::ShortTxFilterValidator::Notifications::IsFilterAllowed(Pocke
PocketDb::ShortTxType::Money,
PocketDb::ShortTxType::Answer,
PocketDb::ShortTxType::PrivateContent,
PocketDb::ShortTxType::Boost
PocketDb::ShortTxType::Boost,
PocketDb::ShortTxType::Referal,
PocketDb::ShortTxType::Comment,
PocketDb::ShortTxType::Subscriber,
PocketDb::ShortTxType::CommentScore,
PocketDb::ShortTxType::ContentScore,
PocketDb::ShortTxType::Repost
};

return allowed.find(type) != allowed.end();
}

bool PocketHelpers::ShortTxFilterValidator::NotificationsSummary::IsFilterAllowed(PocketDb::ShortTxType type)
{
static const std::set<PocketDb::ShortTxType> allowed = {
PocketDb::ShortTxType::Referal,
PocketDb::ShortTxType::Comment,
PocketDb::ShortTxType::Subscriber,
PocketDb::ShortTxType::CommentScore,
PocketDb::ShortTxType::ContentScore,
PocketDb::ShortTxType::Repost,
};

return allowed.find(type) != allowed.end();
}

bool PocketHelpers::ShortTxFilterValidator::Activities::IsFilterAllowed(PocketDb::ShortTxType type)
{
static const std::set<PocketDb::ShortTxType> allowed = {
PocketDb::ShortTxType::Answer,
PocketDb::ShortTxType::Comment,
PocketDb::ShortTxType::Subscriber,
PocketDb::ShortTxType::CommentScore,
PocketDb::ShortTxType::ContentScore,
PocketDb::ShortTxType::Boost,
PocketDb::ShortTxType::Blocking,
};

return allowed.find(type) != allowed.end();
}

bool PocketHelpers::ShortTxFilterValidator::Events::IsFilterAllowed(PocketDb::ShortTxType type)
{
static const std::set<PocketDb::ShortTxType> allowed = {
PocketDb::ShortTxType::Money,
PocketDb::ShortTxType::Referal,
PocketDb::ShortTxType::Answer,
PocketDb::ShortTxType::Comment,
PocketDb::ShortTxType::Subscriber,
PocketDb::ShortTxType::CommentScore,
PocketDb::ShortTxType::ContentScore,
PocketDb::ShortTxType::PrivateContent,
PocketDb::ShortTxType::Boost,
PocketDb::ShortTxType::Repost,
};

return allowed.find(type) != allowed.end();
Expand Down
18 changes: 18 additions & 0 deletions src/pocketdb/helpers/ShortFormHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ namespace PocketHelpers
public:
static bool IsFilterAllowed(PocketDb::ShortTxType type);
};

class NotificationsSummary
{
public:
static bool IsFilterAllowed(PocketDb::ShortTxType type);
};

class Activities
{
public:
static bool IsFilterAllowed(PocketDb::ShortTxType type);
};

class Events
{
public:
static bool IsFilterAllowed(PocketDb::ShortTxType type);
};
};

// STMT here is used to avoid including here any of sqlite3 headers, however
Expand Down
3 changes: 3 additions & 0 deletions src/pocketdb/migrations/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,13 @@ namespace PocketDb
create index if not exists Transactions_Id_Last on Transactions (Id, Last);
create index if not exists Transactions_Hash_Height on Transactions (Hash, Height);
create index if not exists Transactions_Height_Type on Transactions (Height, Type);
create index if not exists Transactions_Hash_Type_Height on Transactions (Hash, Type, Height);
create index if not exists TxOutputs_AddressHash_TxHeight_TxHash on TxOutputs (AddressHash, TxHeight, TxHash);
create index if not exists Transactions_Type_Last_String1_Height_Id on Transactions (Type, Last, String1, Height, Id);
create index if not exists Transactions_Type_Last_String2_Height on Transactions (Type, Last, String2, Height);
create index if not exists Transactions_Type_Last_String3_Height on Transactions (Type, Last, String3, Height);
create index if not exists Transactions_Type_Last_String4_Height on Transactions (Type, Last, String4, Height);
create index if not exists Transactions_Type_Last_String5_Height on Transactions (Type, Last, String5, Height);
create index if not exists Transactions_Type_Last_String1_String2_Height on Transactions (Type, Last, String1, String2, Height);
create index if not exists Transactions_Type_Last_Height_String5_String1 on Transactions (Type, Last, Height, String5, String1);
create index if not exists Transactions_Type_Last_Height_Id on Transactions (Type, Last, Height, Id);
Expand Down
11 changes: 11 additions & 0 deletions src/pocketdb/models/shortform/ShortAccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ UniValue PocketDb::ShortAccount::Serialize() const
data.pushKV("avatar", m_avatar);
data.pushKV("badge", m_badge);
data.pushKV("reputation", m_reputation);
if (m_lang) data.pushKV("lang", *m_lang);

return data;
}
Expand Down Expand Up @@ -64,3 +65,13 @@ void PocketDb::ShortAccount::SetReputation(const int64_t& reputation)
{
m_reputation = reputation;
}

const std::optional<std::string>& PocketDb::ShortAccount::GetLang() const
{
return m_lang;
}

void PocketDb::ShortAccount::SetLang(const std::optional<std::string>& lang)
{
m_lang = lang;
}
5 changes: 4 additions & 1 deletion src/pocketdb/models/shortform/ShortAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <univalue.h>

#include <string>

#include <optional>

namespace PocketDb
{
Expand All @@ -27,10 +27,13 @@ namespace PocketDb
void SetBadge(const std::string& name);
const int64_t& GetReputation() const;
void SetReputation(const int64_t& reputation);
const std::optional<std::string>& GetLang() const;
void SetLang(const std::optional<std::string>& lang);
private:
std::string m_avatar;
std::string m_name;
std::string m_badge;
std::optional<std::string> m_lang;
int64_t m_reputation {0};
};
}
Expand Down
1 change: 0 additions & 1 deletion src/pocketdb/models/shortform/ShortForm.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace PocketDb
class ShortForm
{
public:
// TODO tomorrow: serialize me correctly!!!
ShortForm(PocketDb::ShortTxType type, ShortTxData txData, std::optional<ShortTxData> relatedContent);

UniValue Serialize() const;
Expand Down
52 changes: 49 additions & 3 deletions src/pocketdb/models/shortform/ShortTxData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
PocketDb::ShortTxData::ShortTxData(std::string hash, PocketTx::TxType txType, std::optional<std::string> address, std::optional<int64_t> height,
std::optional<int64_t> blockNum, std::optional<ShortAccount> account, std::optional<int> val,
std::optional<std::string> description, std::optional<std::string> commentParentId,
std::optional<std::string> commentAnswerId
std::optional<std::string> commentAnswerId, std::optional<std::string> rootTxHash,
std::optional<std::vector<std::pair<std::string, std::optional<ShortAccount>>>> multipleAddresses
)
: m_hash(std::move(hash)),
m_txType(txType),
Expand All @@ -21,7 +22,9 @@ PocketDb::ShortTxData::ShortTxData(std::string hash, PocketTx::TxType txType, st
m_val(std::move(val)),
m_description(std::move(description)),
m_commentParentId(std::move(commentParentId)),
m_commentAnswerId(std::move(commentAnswerId))
m_commentAnswerId(std::move(commentAnswerId)),
m_rootTxHash(std::move(rootTxHash)),
m_multipleAddresses(std::move(multipleAddresses))
{}

PocketDb::ShortTxData::ShortTxData(std::string hash, PocketTx::TxType txType)
Expand All @@ -43,6 +46,33 @@ UniValue PocketDb::ShortTxData::Serialize() const
if (m_description) data.pushKV("description", m_description.value());
if (m_commentParentId) data.pushKV("commentParentId", m_commentParentId.value());
if (m_commentAnswerId) data.pushKV("commentAnswerId", m_commentAnswerId.value());
if (m_rootTxHash) data.pushKV("rootTxHash", *m_rootTxHash);
if (m_inputs) {
UniValue inputs (UniValue::VARR);
std::vector<UniValue> tmp;
for (const auto& input: *m_inputs) {
tmp.emplace_back(std::move(input.Serialize()));
}
inputs.push_backV(tmp);
data.pushKV("inputs", inputs);
}
if (m_outputs) {
UniValue outputs (UniValue::VARR);
std::vector<UniValue> tmp;
for (const auto& output: *m_outputs) {
tmp.emplace_back(std::move(output.Serialize()));
}
outputs.push_backV(tmp);
data.pushKV("outputs", outputs);
}
if (m_multipleAddresses) {
UniValue multipleAddresses(UniValue::VOBJ);
multipleAddresses.reserveKVSize(m_multipleAddresses->size());
for (const auto& addressData: *m_multipleAddresses) {
multipleAddresses.pushKV(addressData.first, std::move(addressData.second->Serialize()), false);
}
data.pushKV("multipleAddresses", multipleAddresses);
}

return data;
}
Expand Down Expand Up @@ -81,4 +111,20 @@ const std::optional<std::string>& PocketDb::ShortTxData::GetCommentParentId() co

void PocketDb::ShortTxData::SetCommentAnswerId(const std::optional<std::string>& commentAnswerId) { m_commentAnswerId = commentAnswerId; }

const std::optional<std::string>& PocketDb::ShortTxData::GetCommentAnswerId() const { return m_commentAnswerId; }
const std::optional<std::string>& PocketDb::ShortTxData::GetCommentAnswerId() const { return m_commentAnswerId; }

void PocketDb::ShortTxData::SetRootTxHash(const std::optional<std::string>& rootTxHash) { m_rootTxHash = rootTxHash; }

const std::optional<std::string>& PocketDb::ShortTxData::GetRootTxHash() const { return m_rootTxHash; }

void PocketDb::ShortTxData::SetMultipleAddresses(const std::optional<std::vector<std::pair<std::string, std::optional<ShortAccount>>>>& multipleAddresses) { m_multipleAddresses = multipleAddresses; }

const std::optional<std::vector<std::pair<std::string, std::optional<PocketDb::ShortAccount>>>>& PocketDb::ShortTxData::GetMultipleAddresses() { return m_multipleAddresses; }

void PocketDb::ShortTxData::SetOutputs(const std::optional<std::vector<ShortTxOutput>>& outputs) { m_outputs = outputs; }

const std::optional<std::vector<PocketDb::ShortTxOutput>>& PocketDb::ShortTxData::GetOutputs() const { return m_outputs; }

void PocketDb::ShortTxData::SetInputs(const std::optional<std::vector<ShortTxOutput>>& inputs) { m_inputs = inputs; }

const std::optional<std::vector<PocketDb::ShortTxOutput>>& PocketDb::ShortTxData::GetInputs() const { return m_inputs; }
16 changes: 15 additions & 1 deletion src/pocketdb/models/shortform/ShortTxData.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "pocketdb/models/base/PocketTypes.h"
#include "pocketdb/models/shortform/ShortAccount.h"
#include "pocketdb/models/shortform/ShortTxOutput.h"

#include <univalue.h>

Expand All @@ -23,7 +24,8 @@ namespace PocketDb
std::optional<int64_t> height, std::optional<int64_t> blockNum,
std::optional<ShortAccount> account, std::optional<int> val,
std::optional<std::string> description,
std::optional<std::string> commentParentId, std::optional<std::string> commentAnswerId);
std::optional<std::string> commentParentId, std::optional<std::string> commentAnswerId,
std::optional<std::string> rootTxHash, std::optional<std::vector<std::pair<std::string, std::optional<ShortAccount>>>> multipleAddresses);

ShortTxData(std::string hash, PocketTx::TxType txType);

Expand All @@ -47,6 +49,14 @@ namespace PocketDb
const std::optional<std::string>& GetCommentParentId() const;
void SetCommentAnswerId(const std::optional<std::string>& commentAnswerId);
const std::optional<std::string>& GetCommentAnswerId() const;
void SetRootTxHash(const std::optional<std::string>& rootTxHash);
const std::optional<std::string>& GetRootTxHash() const;
void SetMultipleAddresses(const std::optional<std::vector<std::pair<std::string, std::optional<ShortAccount>>>>& multipleAddresses);
const std::optional<std::vector<std::pair<std::string, std::optional<ShortAccount>>>>& GetMultipleAddresses();
void SetOutputs(const std::optional<std::vector<ShortTxOutput>>& outputs);
const std::optional<std::vector<ShortTxOutput>>& GetOutputs() const;
void SetInputs(const std::optional<std::vector<ShortTxOutput>>& inputs);
const std::optional<std::vector<ShortTxOutput>>& GetInputs() const;

private:
std::string m_hash;
Expand All @@ -55,8 +65,12 @@ namespace PocketDb
std::optional<int64_t> m_height; // This field is optional if we are requesting a lot of txs for one height and want to not duplicate meaningless data
std::optional<int64_t> m_blockNum; // TODO (losty): probably some filters for these fields
std::optional<ShortAccount> m_account; // Account data associated with address
std::optional<std::vector<std::pair<std::string, std::optional<ShortAccount>>>> m_multipleAddresses;
std::optional<int> m_val;
std::optional<std::vector<ShortTxOutput>> m_inputs;
std::optional<std::vector<ShortTxOutput>> m_outputs;
std::optional<std::string> m_description; // Short description of content, e.x. first lines of post's text
std::optional<std::string> m_rootTxHash;

// Special-case fields for comments
std::optional<std::string> m_commentParentId;
Expand Down
45 changes: 45 additions & 0 deletions src/pocketdb/models/shortform/ShortTxOutput.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2018-2022 The Pocketnet developers
// Distributed under the Apache 2.0 software license, see the accompanying
// https://www.apache.org/licenses/LICENSE-2.0

#include "pocketdb/models/shortform/ShortTxOutput.h"


UniValue PocketDb::ShortTxOutput::Serialize() const
{
UniValue data (UniValue::VOBJ);

if (m_txHash) data.pushKV("txhash", *m_txHash);
if (m_spentTxHash) data.pushKV("spenttxhash", *m_spentTxHash);
if (m_addressHash) data.pushKV("addresshash", *m_addressHash);
if (m_number) data.pushKV("number", *m_number);
if (m_value) data.pushKV("value", *m_value);
if (m_scriptPubKey) data.pushKV("scriptpubkey", *m_scriptPubKey);

return data;
}

void PocketDb::ShortTxOutput::SetTxHash(const std::optional<std::string>& txHash) { m_txHash = txHash; }

const std::optional<std::string>& PocketDb::ShortTxOutput::GetTxHash() const { return m_txHash; }

void PocketDb::ShortTxOutput::SetSpentTxHash(const std::optional<std::string>& spentTxHash) { m_spentTxHash = spentTxHash; }

const std::optional<std::string>& PocketDb::ShortTxOutput::GetSpentTxHash() const { return m_spentTxHash; }

void PocketDb::ShortTxOutput::SetNumber(const std::optional<int>& number) { m_number = number; }

const std::optional<int>& PocketDb::ShortTxOutput::GetNumber() const { return m_number; }

void PocketDb::ShortTxOutput::SetAddressHash(const std::optional<std::string>& addressHash) { m_addressHash = addressHash; }

const std::optional<std::string>& PocketDb::ShortTxOutput::GetAddressHash() const { return m_addressHash; }

void PocketDb::ShortTxOutput::SetScriptPubKey(const std::optional<std::string>& scriptPubKey) { m_scriptPubKey = scriptPubKey; }

const std::optional<std::string>& PocketDb::ShortTxOutput::GetcriptPubKey() const { return m_scriptPubKey; }

void PocketDb::ShortTxOutput::SetValue(const std::optional<int64_t>& value) { m_value = value; }

const std::optional<int64_t>& PocketDb::ShortTxOutput::GetValue() const { return m_value; }

Loading

0 comments on commit 7b7338c

Please sign in to comment.