Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds contribution queue (uplift to 0.71.x) #3740

Merged
merged 3 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .storybook/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const locale: Record<string, string> = {
siteBannerConnectedText: 'This creator has not yet signed up to receive contributions from Brave users. Your browser will keep trying to contribute until they verify, or until 90 days have passed.',
sites: 'sites',
tellOthers: 'Tell others about your tip.',
supportedSites: 'Sites viewed',
viewedSites: 'Sites Viewed',
thankYou: 'Thank You!',
termsOfService: 'Terms of Service',
optOutTooltip: 'You will no longer receive\nads from this category',
Expand Down
18 changes: 9 additions & 9 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnReconcileComplete(brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
int32_t category,
const std::string& probi) override;
const std::string& probi,
const int32_t type) override;
void OnPendingContributionSaved(
brave_rewards::RewardsService* rewards_service,
int result) override;
Expand All @@ -214,7 +214,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnContributionSaved(
brave_rewards::RewardsService* rewards_service,
bool success,
int category) override;
int type) override;

void OnPendingContributionRemoved(
brave_rewards::RewardsService* rewards_service,
Expand Down Expand Up @@ -903,12 +903,12 @@ void RewardsDOMHandler::OnReconcileComplete(
brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
int32_t category,
const std::string& probi) {
const std::string& probi,
const int32_t type) {
if (web_ui()->CanCallJavascript()) {
base::DictionaryValue complete;
complete.SetKey("result", base::Value(static_cast<int>(result)));
complete.SetKey("category", base::Value(category));
complete.SetKey("type", base::Value(type));

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.reconcileComplete",
complete);
Expand Down Expand Up @@ -1361,14 +1361,14 @@ void RewardsDOMHandler::OnRecurringTipRemoved(
void RewardsDOMHandler::OnContributionSaved(
brave_rewards::RewardsService* rewards_service,
bool success,
int category) {
int type) {
if (!web_ui()->CanCallJavascript()) {
return;
}

base::DictionaryValue result;
result.SetBoolean("success", success);
result.SetInteger("category", category);
result.SetInteger("type", type);

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.onContributionSaved", result);
Expand Down Expand Up @@ -1412,7 +1412,7 @@ void RewardsDOMHandler::OnGetPendingContributions(
contribution->SetKey("amount", base::Value(item.amount));
contribution->SetKey("addedDate",
base::Value(std::to_string(item.added_date)));
contribution->SetKey("category", base::Value(item.category));
contribution->SetKey("type", base::Value(item.type));
contribution->SetKey("viewingId", base::Value(item.viewing_id));
contribution->SetKey("expirationDate",
base::Value(std::to_string(item.expiration_date)));
Expand Down
4 changes: 2 additions & 2 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "contributionVisitSome", IDS_BRAVE_REWARDS_LOCAL_CONTR_VISIT_SOME },
{ "contributionMinTime", IDS_BRAVE_REWARDS_LOCAL_CONTR_MIN_TIME },
{ "contributionMinVisits", IDS_BRAVE_REWARDS_LOCAL_CONTR_MIN_VISITS },
{ "contributionAllowed", IDS_BRAVE_REWARDS_LOCAL_CONTR_ALLOWED },
{ "contributionOther", IDS_BRAVE_REWARDS_LOCAL_CONTR_OTHER },
{ "contributionShowNonVerified", IDS_BRAVE_REWARDS_LOCAL_CONTR_SHOW_NON_VERIFIED }, // NOLINT
{ "contributionVideos", IDS_BRAVE_REWARDS_LOCAL_CONTR_ALLOW_VIDEOS },
{ "contributionVisit1", IDS_BRAVE_REWARDS_LOCAL_CONTR_VISIT_1 },
Expand Down Expand Up @@ -548,7 +548,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "serviceTextWelcome", IDS_BRAVE_UI_SERVICE_TEXT_WELCOME },
{ "serviceTextReady", IDS_BRAVE_UI_SERVICE_TEXT_READY },
{ "showAll", IDS_BRAVE_UI_SHOW_ALL },
{ "supportedSites", IDS_BRAVE_UI_SUPPORTED_SITES },
{ "viewedSites", IDS_BRAVE_UI_VIEWED_SITES },
{ "termsOfService", IDS_BRAVE_UI_TERMS_OF_SERVICE }
}
}, {
Expand Down
4 changes: 2 additions & 2 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@
"type": "integer",
"description": "result for this contribution"
},
"category": {
"type": {
"type": "integer",
"description": "category of this contribution"
"description": "type of this contribution"
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions components/brave_rewards/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ source_set("browser") {

if (brave_rewards_enabled) {
sources += [
"database/database_contribution_queue.cc",
"database/database_contribution_queue.h",
"database/database_contribution_queue_publishers.cc",
"database/database_contribution_queue_publishers.h",
"database/database_server_publisher_amounts.cc",
"database/database_server_publisher_amounts.h",
"database/database_server_publisher_banner.cc",
Expand Down
32 changes: 17 additions & 15 deletions components/brave_rewards/browser/contribution_info.cc
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/* 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/. */
/* 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_info.h"

namespace brave_rewards {

ContributionInfo::ContributionInfo() :
date(0) {
}
ContributionInfo::ContributionInfo() :
date(0) {
}

ContributionInfo::~ContributionInfo() { }
ContributionInfo::~ContributionInfo() = default;

ContributionInfo::ContributionInfo(const ContributionInfo &data) {
probi = data.probi;
month = data.month;
year = data.year;
category = data.category;
date = data.date;
publisher_key = data.publisher_key;
}
ContributionInfo::ContributionInfo(const ContributionInfo& data) {
probi = data.probi;
month = data.month;
year = data.year;
type = data.type;
date = data.date;
publisher_key = data.publisher_key;
}

} // namespace brave_rewards
36 changes: 19 additions & 17 deletions components/brave_rewards/browser/contribution_info.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
/* 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/. */
/* 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_BROWSER_PAYMENTS_CONTRIBUTION_INFO_
#define BRAVE_BROWSER_PAYMENTS_CONTRIBUTION_INFO_
#ifndef BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_CONTRIBUTION_INFO_H_
#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_CONTRIBUTION_INFO_H_

#include <string>

namespace brave_rewards {
struct ContributionInfo {
ContributionInfo();
~ContributionInfo();
ContributionInfo(const ContributionInfo& properties);

std::string probi;
int month;
int year;
int category;
uint32_t date = 0;
std::string publisher_key;
};
struct ContributionInfo {
ContributionInfo();
~ContributionInfo();
ContributionInfo(const ContributionInfo& properties);

std::string probi;
int month;
int year;
int type;
uint32_t date = 0;
std::string publisher_key;
};

} // namespace brave_rewards

#endif //BRAVE_BROWSER_PAYMENTS_CONTRIBUTION_INFO_
#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_CONTRIBUTION_INFO_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/* 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/database/database_contribution_queue.h"

#include <string>
#include <utility>

#include "base/bind.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "bat/ledger/publisher_info.h"
#include "sql/statement.h"
#include "sql/transaction.h"

namespace brave_rewards {

DatabaseContributionQueue::DatabaseContributionQueue(int current_db_version) :
DatabaseTable(current_db_version),
publishers_(std::make_unique<DatabaseContributionQueuePublishers>
(current_db_version)) {
}

DatabaseContributionQueue::~DatabaseContributionQueue() {
}

std::string DatabaseContributionQueue::GetIdColumnName() {
return base::StringPrintf("%s_id", table_name_);
}

bool DatabaseContributionQueue::Init(sql::Database* db) {
if (GetCurrentDBVersion() < minimum_version_) {
return true;
}

sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}

bool success = CreateTable(db);
if (!success) {
return false;
}

success = publishers_->Init(db);
if (!success) {
return false;
}

return transaction.Commit();
}

bool DatabaseContributionQueue::CreateTable(sql::Database* db) {
if (db->DoesTableExist(table_name_)) {
return true;
}

const std::string query = base::StringPrintf(
"CREATE TABLE %s ("
"%s INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"type INTEGER NOT NULL,"
"amount DOUBLE NOT NULL,"
"partial INTEGER NOT NULL DEFAULT 0,"
"created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL"
")",
table_name_,
GetIdColumnName().c_str());

return db->Execute(query.c_str());
}

bool DatabaseContributionQueue::CreateIndex(sql::Database* db) {
return true;
}

bool DatabaseContributionQueue::InsertOrUpdate(
sql::Database* db,
ledger::ContributionQueuePtr info) {
if (!info) {
return false;
}

sql::Transaction transaction(db);
if (!transaction.Begin()) {
return false;
}
const std::string query = base::StringPrintf(
"INSERT OR REPLACE INTO %s (%s, type, amount, partial) "
"VALUES (?, ?, ?, ?)",
table_name_,
GetIdColumnName().c_str());

sql::Statement statement(
db->GetCachedStatement(SQL_FROM_HERE, query.c_str()));

if (info->id != 0) {
statement.BindInt64(0, info->id);
} else {
statement.BindNull(0);
}

statement.BindInt(1, static_cast<int>(info->type));
statement.BindDouble(2, info->amount);
statement.BindBool(3, info->partial);

if (!statement.Run()) {
return false;
}

if (info->id == 0) {
info->id = db->GetLastInsertRowId();
}

if (!publishers_->InsertOrUpdate(db, info->Clone())) {
transaction.Rollback();
return false;
}

return transaction.Commit();
}

ledger::ContributionQueuePtr DatabaseContributionQueue::GetFirstRecord(
sql::Database* db) {
const std::string query = base::StringPrintf(
"SELECT %s, type, amount, partial FROM %s ORDER BY %s ASC LIMIT 1",
GetIdColumnName().c_str(),
table_name_,
GetIdColumnName().c_str());

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

if (!statment.Step()) {
return nullptr;
}

auto info = ledger::ContributionQueue::New();
info->id = statment.ColumnInt64(0);
info->type = static_cast<ledger::RewardsType>(statment.ColumnInt(1));
info->amount = statment.ColumnDouble(2);
info->partial = static_cast<bool>(statment.ColumnInt(3));
info->publishers = publishers_->GetRecords(db, info->id);

return info;
}

bool DatabaseContributionQueue::DeleteRecord(
sql::Database* db,
const uint64_t id) {
if (!db->Execute("PRAGMA foreign_keys=1;")) {
LOG(ERROR) << "Error: deleting record for contribution queue with id" << id;
return false;
}

const std::string query = base::StringPrintf(
"DELETE FROM %s WHERE %s = ?",
table_name_,
GetIdColumnName().c_str());

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

bool success = statement.Run();

if (!db->Execute("PRAGMA foreign_keys=0;")) {
return false;
}

return success;
}

} // namespace brave_rewards
Loading