Skip to content

Commit

Permalink
Adds API for monthly statement
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Dec 19, 2019
1 parent d0c3fac commit 967b494
Show file tree
Hide file tree
Showing 45 changed files with 1,058 additions and 9 deletions.
94 changes: 94 additions & 0 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ class RewardsDOMHandler : public WebUIMessageHandler,
const int32_t result,
const brave_rewards::BalanceReport& report);

void GetMonthlyReport(const base::ListValue* args);

void OnGetMonthlyReport(
const uint32_t month,
const uint32_t year,
const brave_rewards::MonthlyReport& report);

// RewardsServiceObserver implementation
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
int32_t result) override;
Expand Down Expand Up @@ -441,6 +448,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getBalanceReport",
base::BindRepeating(&RewardsDOMHandler::GetBalanceReport,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getMonthlyReport",
base::BindRepeating(&RewardsDOMHandler::GetMonthlyReport,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand Down Expand Up @@ -1718,6 +1728,90 @@ void RewardsDOMHandler::GetBalanceReport(const base::ListValue* args) {
year));
}

void RewardsDOMHandler::OnGetMonthlyReport(
const uint32_t month,
const uint32_t year,
const brave_rewards::MonthlyReport& report) {
if (!web_ui()->CanCallJavascript()) {
return;
}

base::Value data(base::Value::Type::DICTIONARY);
data.SetIntKey("month", month);
data.SetIntKey("year", year);

base::Value balance_report(base::Value::Type::DICTIONARY);
balance_report.SetDoubleKey("grant", report.balance.grants);
balance_report.SetDoubleKey("ads", report.balance.earning_from_ads);
balance_report.SetDoubleKey("contribute", report.balance.auto_contribute);
balance_report.SetDoubleKey("donation", report.balance.recurring_donation);
balance_report.SetDoubleKey("tips", report.balance.one_time_donation);

base::Value transactions(base::Value::Type::LIST);
for (const auto& item : report.transactions) {
base::Value transaction_report(base::Value::Type::DICTIONARY);
transaction_report.SetDoubleKey("amount", item.amount);
transaction_report.SetIntKey("type", item.type);
transaction_report.SetIntKey("created_at", item.created_at);

transactions.Append(std::move(transaction_report));
}

base::Value contributions(base::Value::Type::LIST);
for (const auto& item : report.contributions) {
base::Value publishers(base::Value::Type::LIST);
for (const auto& item : item.publishers) {
base::Value publisher(base::Value::Type::DICTIONARY);
publisher.SetStringKey("id", item.id);
publisher.SetDoubleKey("percentage", item.percentage);
publisher.SetStringKey("publisherKey", item.id);
publisher.SetIntKey("status", item.status);
publisher.SetStringKey("name", item.name);
publisher.SetStringKey("provider", item.provider);
publisher.SetStringKey("url", item.url);
publisher.SetStringKey("favIcon", item.favicon_url);
publishers.Append(std::move(publisher));
}

base::Value contribution_report(base::Value::Type::DICTIONARY);
contribution_report.SetDoubleKey("amount", item.amount);
contribution_report.SetIntKey("type", item.type);
contribution_report.SetIntKey("created_at", item.created_at);
contribution_report.SetKey("publishers", std::move(publishers));
contributions.Append(std::move(contribution_report));
}

base::Value report_base(base::Value::Type::DICTIONARY);
report_base.SetKey("balance", std::move(balance_report));
report_base.SetKey("transactions", std::move(transactions));
report_base.SetKey("contributions", std::move(contributions));

data.SetKey("report", std::move(report_base));

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.monthlyReport",
data);
}

void RewardsDOMHandler::GetMonthlyReport(const base::ListValue* args) {
CHECK_EQ(2U, args->GetSize());
if (!rewards_service_) {
return;
}

const uint32_t month = args->GetList()[0].GetInt();
const uint32_t year = args->GetList()[1].GetInt();

rewards_service_->GetMonthlyReport(
month,
year,
base::BindOnce(&RewardsDOMHandler::OnGetMonthlyReport,
weak_factory_.GetWeakPtr(),
month,
year));
}


} // namespace

BraveRewardsPageUI::BraveRewardsPageUI(content::WebUI* web_ui,
Expand Down
5 changes: 5 additions & 0 deletions components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ class MockRewardsService : public RewardsService {
const uint32_t month,
const uint32_t year,
brave_rewards::GetBalanceReportCallback callback));

MOCK_METHOD3(GetMonthlyReport, void(
const uint32_t month,
const uint32_t year,
brave_rewards::GetMonthlyReportCallback callback));
};

class AdsServiceTest : public testing::Test {
Expand Down
6 changes: 6 additions & 0 deletions components/brave_rewards/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ source_set("browser") {
"rewards_protocol_handler.h",
"rewards_protocol_handler.cc",
"static_values.h",
"monthly_report.cc",
"monthly_report.h",
"transaction_report_info.cc",
"transaction_report_info.h",
"contribution_report_info.cc",
"contribution_report_info.h",
]

deps = [
Expand Down
17 changes: 17 additions & 0 deletions components/brave_rewards/browser/contribution_report_info.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_rewards/browser/contribution_report_info.h"

namespace brave_rewards {

ContributionReportInfo::ContributionReportInfo() = default;

ContributionReportInfo::~ContributionReportInfo() = default;

ContributionReportInfo::ContributionReportInfo(
const ContributionReportInfo &properties) = default;

} // namespace brave_rewards
28 changes: 28 additions & 0 deletions components/brave_rewards/browser/contribution_report_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_CONTRIBUTION_REPORT_INFO_H_
#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_CONTRIBUTION_REPORT_INFO_H_

#include <vector>

#include "brave/components/brave_rewards/browser/content_site.h"

namespace brave_rewards {

struct ContributionReportInfo {
ContributionReportInfo();
~ContributionReportInfo();
ContributionReportInfo(const ContributionReportInfo& properties);

double amount;
uint32_t type;
std::vector<ContentSite> publishers;
uint64_t created_at;
};

} // namespace brave_rewards

#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_CONTRIBUTION_REPORT_INFO_H_
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ namespace {

return std::stod(amount);
}

ledger::ReportType ConvertRewardsTypeToReportType(const int type) {
switch (type) {
case 2: {
return ledger::ReportType::AUTO_CONTRIBUTION;
}
case 8: {
return ledger::ReportType::TIP;
}
case 16: {
return ledger::ReportType::TIP_RECURRING;
}
default: {
NOTREACHED();
return ledger::ReportType::TIP;
}
}
}

} // namespace

namespace brave_rewards {
Expand Down Expand Up @@ -399,4 +418,42 @@ bool DatabaseContributionInfo::GetOneTimeTips(
return true;
}

bool DatabaseContributionInfo::GetContributionReport(
sql::Database* db,
ledger::ContributionReportInfoList* list,
const ledger::ActivityMonth month,
const uint32_t year) {
if (!list) {
return false;
}

const std::string query =
"SELECT ci.contribution_id, ci.amount, ci.type, ci.created_at "
"FROM contribution_info as ci "
"WHERE strftime('%m', datetime(ci.created_at, 'unixepoch')) = ? AND "
"strftime('%Y', datetime(ci.created_at, 'unixepoch')) = ?";

sql::Statement statement(db->GetUniqueStatement(query.c_str()));

const std::string formatted_month = base::StringPrintf("%02d", month);

statement.BindString(0, formatted_month);
statement.BindString(1, std::to_string(year));

while (statement.Step()) {
auto report = ledger::ContributionReportInfo::New();
report->amount = statement.ColumnDouble(1);
report->type = ConvertRewardsTypeToReportType(statement.ColumnInt64(2));
report->created_at = statement.ColumnInt64(3);
publishers_->GetPublisherInfoList(
db,
statement.ColumnString(0),
&report->publishers);

list->push_back(std::move(report));
}

return true;
}

} // namespace brave_rewards
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class DatabaseContributionInfo: public DatabaseTable {
const ledger::ActivityMonth month,
const int year);

bool GetContributionReport(
sql::Database* db,
ledger::ContributionReportInfoList* list,
const ledger::ActivityMonth month,
const uint32_t year);

private:
const char* table_name_ = "contribution_info";
const int minimum_version_ = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,42 @@ bool DatabaseContributionInfoPublishers::GetRecords(
return true;
}

bool DatabaseContributionInfoPublishers::GetPublisherInfoList(
sql::Database* db,
const std::string& contribution_id,
ledger::PublisherInfoList* list) {
if (contribution_id.empty() || !list) {
return false;
}

const std::string query = base::StringPrintf(
"SELECT cip.publisher_key, cip.total_amount, pi.name, pi.url, pi.favIcon, "
"spi.status, pi.provider FROM %s as cip "
"INNER JOIN publisher_info AS pi ON cip.publisher_key = pi.publisher_id "
"LEFT JOIN server_publisher_info AS spi "
"ON spi.publisher_key = cip.publisher_key "
"WHERE cip.contribution_id = ?",
table_name_);

sql::Statement statement(db->GetUniqueStatement(query.c_str()));
statement.BindString(0, contribution_id);

while (statement.Step()) {
auto publisher = ledger::PublisherInfo::New();

publisher->id = statement.ColumnString(0);
publisher->weight = statement.ColumnDouble(1);
publisher->name = statement.ColumnString(2);
publisher->url = statement.ColumnString(3);
publisher->favicon_url = statement.ColumnString(4);
publisher->status =
static_cast<ledger::mojom::PublisherStatus>(statement.ColumnInt64(5));
publisher->provider = statement.ColumnString(6);

list->push_back(std::move(publisher));
}

return true;
}

} // namespace brave_rewards
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class DatabaseContributionInfoPublishers: public DatabaseTable {
const std::string& contribution_id,
ledger::ContributionPublisherList* list);

bool GetPublisherInfoList(
sql::Database* db,
const std::string& contribution_id,
ledger::PublisherInfoList* list);

private:
const char* table_name_ = "contribution_info_publishers";
const int minimum_version_ = 11;
Expand Down
Loading

0 comments on commit 967b494

Please sign in to comment.