Skip to content

Commit

Permalink
Merge pull request #46 from brave-intl/save_publishers
Browse files Browse the repository at this point in the history
Save Publishers calls
  • Loading branch information
SergeyZhukovsky authored Aug 14, 2018
2 parents f198216 + d34322c commit 291aacb
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 52 deletions.
26 changes: 13 additions & 13 deletions include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
namespace ledger {

LEDGER_EXPORT struct VisitData {
VisitData();
VisitData(const std::string& _tld,
const std::string& _domain,
const std::string& _path,
uint32_t tab_id);
VisitData(const VisitData& data);
const std::string tld;
const std::string domain;
const std::string path;
const uint32_t tab_id;
uint64_t duration;
std::string tld;
std::string domain;
std::string path;
uint32_t tab_id;
};

class LEDGER_EXPORT Ledger {
Expand All @@ -42,14 +42,14 @@ class LEDGER_EXPORT Ledger {
virtual void CreateWallet() = 0;
virtual void Reconcile() = 0;

virtual void OnLoad(const VisitData& visit_data) = 0;
virtual void OnUnload(uint32_t tab_id) = 0;
virtual void OnShow(uint32_t tab_id) = 0;
virtual void OnHide(uint32_t tab_id) = 0;
virtual void OnForeground(uint32_t tab_id) = 0;
virtual void OnBackground(uint32_t tab_id) = 0;
virtual void OnMediaStart(uint32_t tab_id) = 0;
virtual void OnMediaStop(uint32_t tab_id) = 0;
virtual void OnLoad(const VisitData& visit_data, const uint64_t& current_time) = 0;
virtual void OnUnload(uint32_t tab_id, const uint64_t& current_time) = 0;
virtual void OnShow(uint32_t tab_id, const uint64_t& current_time) = 0;
virtual void OnHide(uint32_t tab_id, const uint64_t& current_time) = 0;
virtual void OnForeground(uint32_t tab_id, const uint64_t& current_time) = 0;
virtual void OnBackground(uint32_t tab_id, const uint64_t& current_time) = 0;
virtual void OnMediaStart(uint32_t tab_id, const uint64_t& current_time) = 0;
virtual void OnMediaStop(uint32_t tab_id, const uint64_t& current_time) = 0;
virtual void OnXHRLoad(uint32_t tab_id, const std::string& url) = 0;

virtual void SetPublisherInfo(std::unique_ptr<PublisherInfo> publisher_info,
Expand Down
7 changes: 5 additions & 2 deletions src/bat/ledger/ledger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace ledger {

VisitData::VisitData():
tab_id(-1) {}

VisitData::VisitData(const std::string& _tld,
const std::string& _domain,
const std::string& _path,
Expand All @@ -24,11 +27,11 @@ VisitData::VisitData(const VisitData& data) :
tld(data.tld),
domain(data.domain),
path(data.path),
tab_id(data.tab_id),
duration(data.duration) {}
tab_id(data.tab_id) {}

const PublisherInfo invalid("");


PublisherInfo::PublisherInfo(const id_type& publisher_id) :
id(publisher_id),
duration(0u),
Expand Down
11 changes: 6 additions & 5 deletions src/bat_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,10 @@ void BatClient::reconcileCallback(bool result, const std::string& response) {
void BatClient::currentReconcile() {
std::ostringstream amount;
amount << state_->fee_amount_;
std::string path = (std::string)WALLET_PROPERTIES + state_->walletInfo_.paymentId_ + "?refresh=true&amount=" + amount.str() + "&altcurrency=" + state_->fee_currency_;
std::string path = (std::string)WALLET_PROPERTIES + state_->walletInfo_.paymentId_ + "?amount=" + amount.str() + "&altcurrency=" + state_->fee_currency_;

auto request_id = ledger_->LoadURL(buildURL(path, ""),
LOG(ERROR) << "!!!currentReconcile path == " << path;
auto request_id = ledger_->LoadURL(buildURL(path, PREFIX_V2),
std::vector<std::string>(), "", "",
ledger::URL_METHOD::GET, &handler_);
handler_.AddRequestHandler(std::move(request_id),
Expand All @@ -283,7 +284,7 @@ void BatClient::currentReconcile() {
}

void BatClient::currentReconcileCallback(bool result, const std::string& response) {
//LOG(ERROR) << "!!!currentReconcileCallback response == " << response;
LOG(ERROR) << "!!!currentReconcileCallback response == " << response;
if (!result) {
// TODO errors handling
return;
Expand Down Expand Up @@ -347,7 +348,7 @@ void BatClient::currentReconcileCallback(bool result, const std::string& respons
}

void BatClient::reconcilePayloadCallback(bool result, const std::string& response) {
//LOG(ERROR) << "!!!response reconcilePayloadCallback == " << response;
LOG(ERROR) << "!!!response reconcilePayloadCallback == " << response;
if (!result) {
// TODO errors handling
return;
Expand Down Expand Up @@ -900,6 +901,7 @@ void BatClient::recoverWallet(const std::string& passPhrase) {
braveledger_bat_helper::getPublicKeyFromSeed(secretKey, publicKey, newSecretKey);
std::string publicKeyHex = braveledger_bat_helper::uint8ToHex(publicKey);

//LOG(ERROR) << "!!!recover URL == " << buildURL((std::string)RECOVER_WALLET_PUBLIC_KEY + publicKeyHex, "");
auto request_id = ledger_->LoadURL(buildURL((std::string)RECOVER_WALLET_PUBLIC_KEY + publicKeyHex, ""),
std::vector<std::string>(), "", "",
ledger::URL_METHOD::GET, &handler_);
Expand All @@ -918,7 +920,6 @@ void BatClient::recoverWalletPublicKeyCallback(bool result, const std::string& r
ledger_->OnRecoverWallet(ledger::Result::ERROR, 0);
return;
}

std::string recoveryId;
braveledger_bat_helper::getJSONValue("paymentId", response, recoveryId);

Expand Down
12 changes: 12 additions & 0 deletions src/bat_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "static_values.h"
#include "tweetnacl.h"

//#include "crypto/hkdf.h"

namespace braveledger_bat_helper {

REQUEST_CREDENTIALS_ST::REQUEST_CREDENTIALS_ST() {}
Expand Down Expand Up @@ -1278,6 +1280,16 @@ namespace braveledger_bat_helper {

DCHECK(hkdfRes);
DCHECK(!seed.empty());

// We set the key_length to the length of the expected output and then take
// the result from the first key, which is the client write key.
//const std::string key((char*)&seed[0], seed.size());
//const std::string salt((char*)&braveledger_ledger::g_hkdfSalt[0], SALT_LENGTH);
//crypto::HKDF hkdf(key, salt, "", SEED_LENGTH, 0, 0);

//LOG(ERROR) << "hkdf.client_write_key() == " << hkdf.client_write_key().data();
//memcpy(&out.front(), hkdf.client_write_key().data(), hkdf.client_write_key().size());

return out;
}

Expand Down
11 changes: 6 additions & 5 deletions src/bat_publishers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ bool ignoreMinTime(const ledger::PublisherInfo::id_type publisher_id) {
return !getProviderName(publisher_id).empty();
}

void BatPublishers::saveVisit(const ledger::VisitData& visit_data) {
void BatPublishers::saveVisit(const ledger::VisitData& visit_data, const uint64_t& duration) {
const ledger::PublisherInfo::id_type publisher_id =
getPublisherID(visit_data);

if (!ignoreMinTime(publisher_id) &&
visit_data.duration < state_->min_pubslisher_duration_)
duration < state_->min_pubslisher_duration_)
return;

ledger_->GetPublisherInfo(publisher_id,
std::bind(&BatPublishers::saveVisitInternal, this,
publisher_id, visit_data, _1, _2));
publisher_id, visit_data, duration, _1, _2));
}

void onVisitSavedDummy(ledger::Result result,
Expand All @@ -92,6 +92,7 @@ void onVisitSavedDummy(ledger::Result result,
void BatPublishers::saveVisitInternal(
ledger::PublisherInfo::id_type publisher_id,
ledger::VisitData visit_data,
uint64_t duration,
ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> publisher_info) {
if (result != ledger::Result::OK) {
Expand All @@ -102,9 +103,9 @@ void BatPublishers::saveVisitInternal(
if (!publisher_info.get())
publisher_info.reset(new ledger::PublisherInfo(publisher_id));

publisher_info->duration += visit_data.duration;
publisher_info->duration += duration;
publisher_info->visits += 1;
publisher_info->score += concaveScore(visit_data.duration);
publisher_info->score += concaveScore(duration);

ledger_->SetPublisherInfo(std::move(publisher_info),
std::bind(&onVisitSavedDummy, _1, _2));
Expand Down
3 changes: 2 additions & 1 deletion src/bat_publishers.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler {

void loadState(const std::string& data);

void saveVisit(const ledger::VisitData& visit_data);
void saveVisit(const ledger::VisitData& visit_data, const uint64_t& duration);

void setPublisherMinVisitTime(const uint64_t& duration); // In milliseconds

Expand All @@ -61,6 +61,7 @@ class BatPublishers : public ledger::LedgerCallbackHandler {
void saveVisitInternal(
ledger::PublisherInfo::id_type publisher_id,
ledger::VisitData visit_data,
uint64_t duration,
ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> publisher_info);

Expand Down
75 changes: 57 additions & 18 deletions src/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ LedgerImpl::LedgerImpl(ledger::LedgerClient* client) :
bat_client_(new BatClient(this)),
bat_publishers_(new BatPublishers(this)),
bat_get_media_(new BatGetMedia(this)),
initialized_(false) {
initialized_(false),
last_tab_active_time_(0),
last_shown_tab_id_(-1) {
}

LedgerImpl::~LedgerImpl() {
Expand All @@ -42,40 +44,77 @@ void LedgerImpl::CreateWallet() {
bat_client_->registerPersona();
}

void LedgerImpl::OnLoad(const ledger::VisitData& visit_data) {

void LedgerImpl::OnLoad(const ledger::VisitData& visit_data, const uint64_t& current_time) {
if (visit_data.domain.empty()) {
// Skip the same domain name
return;
}
visit_data_iter iter = current_pages_.find(visit_data.tab_id);
if (iter != current_pages_.end() && iter->second.domain == visit_data.domain) {
DCHECK(iter == current_pages_.end());
return;
}
if (last_shown_tab_id_ == visit_data.tab_id) {
last_tab_active_time_ = current_time;
}
current_pages_[visit_data.tab_id] = visit_data;
//LOG(ERROR) << "!!!LedgerImpl::OnLoad tab_id == " << visit_data.tab_id << ", domain == " << visit_data.domain
// << ", tld == " << visit_data.tld << ", path == " << visit_data.path;
}

void LedgerImpl::OnUnload(uint32_t tab_id) {

void LedgerImpl::OnUnload(uint32_t tab_id, const uint64_t& current_time) {
//LOG(ERROR) << "!!!LedgerImpl::OnUnload tab_id == " << tab_id;
OnHide(tab_id, current_time);
visit_data_iter iter = current_pages_.find(tab_id);
if (iter != current_pages_.end()) {
current_pages_.erase(iter);
}
}

void LedgerImpl::OnShow(uint32_t tab_id) {

void LedgerImpl::OnShow(uint32_t tab_id, const uint64_t& current_time) {
//LOG(ERROR) << "!!!LedgerImpl::OnShow tab_id == " << tab_id;
last_tab_active_time_ = current_time;
last_shown_tab_id_ = tab_id;
}

void LedgerImpl::OnHide(uint32_t tab_id) {

void LedgerImpl::OnHide(uint32_t tab_id, const uint64_t& current_time) {
if (tab_id != last_shown_tab_id_) {
return;
}
visit_data_iter iter = current_pages_.find(tab_id);
if (iter == current_pages_.end() || 0 == last_tab_active_time_) {
return;
}
//LOG(ERROR) << "!!!LedgerImpl::OnHide tab_id == " << tab_id << ", time == " << (current_time - last_tab_active_time_);
DCHECK(last_tab_active_time_);
bat_publishers_->saveVisit(iter->second, current_time - last_tab_active_time_);
last_tab_active_time_ = 0;
}

void LedgerImpl::OnForeground(uint32_t tab_id) {

void LedgerImpl::OnForeground(uint32_t tab_id, const uint64_t& current_time) {
// TODO media resources could have been played in the background
if (last_shown_tab_id_ != tab_id) {
return;
}
OnShow(tab_id, current_time);
}

void LedgerImpl::OnBackground(uint32_t tab_id) {

void LedgerImpl::OnBackground(uint32_t tab_id, const uint64_t& current_time) {
// TODO media resources could stay and be active in the background
OnHide(tab_id, current_time);
}

void LedgerImpl::OnMediaStart(uint32_t tab_id) {

void LedgerImpl::OnMediaStart(uint32_t tab_id, const uint64_t& current_time) {
// TODO
}

void LedgerImpl::OnMediaStop(uint32_t tab_id) {

void LedgerImpl::OnMediaStop(uint32_t tab_id, const uint64_t& current_time) {
// TODO
}

void LedgerImpl::OnXHRLoad(uint32_t tab_id, const std::string& url) {

// TODO
//LOG(ERROR) << "!!!LedgerImpl::OnXHRLoad " << url;
}

void LedgerImpl::LoadLedgerState(ledger::LedgerCallbackHandler* handler) {
Expand Down
23 changes: 15 additions & 8 deletions src/ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace bat_ledger {
class LedgerImpl : public ledger::Ledger,
public ledger::LedgerCallbackHandler {
public:
typedef std::map<uint32_t, ledger::VisitData>::const_iterator visit_data_iter;

LedgerImpl(ledger::LedgerClient* client);
~LedgerImpl() override;

Expand Down Expand Up @@ -104,14 +106,14 @@ class LedgerImpl : public ledger::Ledger,
void RunTask(LedgerTaskRunnerImpl::Task task);

private:
void OnLoad(const ledger::VisitData& visit_data) override;
void OnUnload(uint32_t tab_id) override;
void OnShow(uint32_t tab_id) override;
void OnHide(uint32_t tab_id) override;
void OnForeground(uint32_t tab_id) override;
void OnBackground(uint32_t tab_id) override;
void OnMediaStart(uint32_t tab_id) override;
void OnMediaStop(uint32_t tab_id) override;
void OnLoad(const ledger::VisitData& visit_data, const uint64_t& current_time) override;
void OnUnload(uint32_t tab_id, const uint64_t& current_time) override;
void OnShow(uint32_t tab_id, const uint64_t& current_time) override;
void OnHide(uint32_t tab_id, const uint64_t& current_time) override;
void OnForeground(uint32_t tab_id, const uint64_t& current_time) override;
void OnBackground(uint32_t tab_id, const uint64_t& current_time) override;
void OnMediaStart(uint32_t tab_id, const uint64_t& current_time) override;
void OnMediaStop(uint32_t tab_id, const uint64_t& current_time) override;
void OnXHRLoad(uint32_t tab_id, const std::string& url) override;

void OnSetPublisherInfo(ledger::PublisherInfoCallback callback,
Expand Down Expand Up @@ -144,6 +146,11 @@ class LedgerImpl : public ledger::Ledger,
bool initialized_;

URLRequestHandler handler_;

//ledger::VisitData current_visit_data_;
std::map<uint32_t, ledger::VisitData> current_pages_;
uint64_t last_tab_active_time_;
uint32_t last_shown_tab_id_;
};
} // namespace bat_ledger

Expand Down

0 comments on commit 291aacb

Please sign in to comment.