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

Promotion expiration #4035

Merged
merged 1 commit into from
Dec 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

namespace brave_rewards {

namespace {

const char* table_name_ = "unblinded_tokens";
NejcZdovc marked this conversation as resolved.
Show resolved Hide resolved
const int minimum_version_ = 10;

} // namespace

DatabaseUnblindedToken::DatabaseUnblindedToken(
int current_db_version) :
DatabaseTable(current_db_version) {
Expand Down Expand Up @@ -157,4 +164,22 @@ bool DatabaseUnblindedToken::DeleteRecords(
return statement.Run();
}

// static
bool DatabaseUnblindedToken::DeleteRecordsForPromotion(
NejcZdovc marked this conversation as resolved.
Show resolved Hide resolved
sql::Database* db,
const std::string& promotion_id) {
if (promotion_id.empty()) {
return false;
}

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

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

return statement.Run();
}

} // namespace brave_rewards
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class DatabaseUnblindedToken: public DatabaseTable {
sql::Database* db,
const std::vector<std::string>& id_list);

private:
NejcZdovc marked this conversation as resolved.
Show resolved Hide resolved
const char* table_name_ = "unblinded_tokens";
const int minimum_version_ = 10;
static bool DeleteRecordsForPromotion(
sql::Database* db,
const std::string& promotion_id);
};

} // namespace brave_rewards
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,17 @@ bool PublisherInfoDatabase::DeleteUnblindedTokens(
return unblinded_token_->DeleteRecords(&GetDB(), id_list);
}

bool PublisherInfoDatabase::DeleteUnblindedTokensForPromotion(
const std::string& promotion_id) {
if (!IsInitialized()) {
return false;
}

return DatabaseUnblindedToken::DeleteRecordsForPromotion(
&GetDB(),
promotion_id);
}

// Other -------------------------------------------------------------------

bool PublisherInfoDatabase::IsInitialized() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class PublisherInfoDatabase {

bool DeleteUnblindedTokens(const std::vector<std::string>& id_list);

bool DeleteUnblindedTokensForPromotion(
const std::string& promotion_id);

void RecordP3AStats(bool auto_contributions_on);

// Returns the current version of the publisher info database
Expand Down
26 changes: 26 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4306,6 +4306,32 @@ void RewardsServiceImpl::DeleteUnblindedTokens(
callback));
}

ledger::Result DeleteUnblindedTokensForPromotionOnFileTaskRunner(
PublisherInfoDatabase* backend,
const std::string& promotion_id) {
if (!backend) {
return ledger::Result::LEDGER_ERROR;
}

const bool result = backend->DeleteUnblindedTokensForPromotion(promotion_id);

return result ? ledger::Result::LEDGER_OK : ledger::Result::LEDGER_ERROR;
}

void RewardsServiceImpl::DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) {
base::PostTaskAndReplyWithResult(
file_task_runner_.get(),
FROM_HERE,
base::BindOnce(&DeleteUnblindedTokensForPromotionOnFileTaskRunner,
publisher_info_backend_.get(),
promotion_id),
base::BindOnce(&RewardsServiceImpl::OnResult,
AsWeakPtr(),
callback));
}

ledger::ClientInfoPtr GetDesktopClientInfo() {
auto info = ledger::ClientInfo::New();
info->platform = ledger::Platform::DESKTOP;
Expand Down
4 changes: 4 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,10 @@ class RewardsServiceImpl : public RewardsService,
const std::vector<std::string>& id_list,
ledger::ResultCallback callback) override;

void DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) override;

ledger::ClientInfoPtr GetClientInfo() override;

void UnblindedTokensReady() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class PageWallet extends React.Component<Props, State> {
}

let claimedPromotions = promotions.filter((promotion: Rewards.Promotion) => {
return promotion.status === 4 // PromotionStatus::FINISHED
return promotion.status === 4 || // PromotionStatus::FINISHED
promotion.status === 5 // PromotionStatus::OVER
tmancey marked this conversation as resolved.
Show resolved Hide resolved
})

return claimedPromotions.map((promotion: Rewards.Promotion) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export const generatePromotions = (promotions?: RewardsExtension.Promotion[]) =>
}

let claimedPromotions = promotions.filter((promotion: Rewards.Promotion) => {
return promotion.status === 4 // PromotionStatus::FINISHED
return promotion.status === 4 || // PromotionStatus::FINISHED
promotion.status === 5 // PromotionStatus::OVER
NejcZdovc marked this conversation as resolved.
Show resolved Hide resolved
})

const typeUGP = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ class PageWallet extends React.Component<Props, State> {
}

let claimedPromotions = promotions.filter((promotion: Rewards.Promotion) => {
return promotion.status === 4 // PromotionStatus::FINISHED
return promotion.status === 4 || // PromotionStatus::FINISHED
promotion.status === 5 // PromotionStatus::OVER
NejcZdovc marked this conversation as resolved.
Show resolved Hide resolved
})

const typeUGP = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,14 @@ void BatLedgerClientMojoProxy::DeleteUnblindedTokens(
base::BindOnce(&OnResultCallback, std::move(callback)));
}

void BatLedgerClientMojoProxy::DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) {
bat_ledger_client_->DeleteUnblindedTokensForPromotion(
promotion_id,
base::BindOnce(&OnResultCallback, std::move(callback)));
}

ledger::ClientInfoPtr BatLedgerClientMojoProxy::GetClientInfo() {
auto info = ledger::ClientInfo::New();
bat_ledger_client_->GetClientInfo(&info);
Expand Down
4 changes: 4 additions & 0 deletions components/services/bat_ledger/bat_ledger_client_mojo_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient,
const std::vector<std::string>& id_list,
ledger::ResultCallback callback) override;

void DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) override;

ledger::ClientInfoPtr GetClientInfo() override;

void UnblindedTokensReady() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,30 @@ void LedgerClientMojoProxy::DeleteUnblindedTokens(
_1));
}

// static
void LedgerClientMojoProxy::OnDeleteUnblindedTokensForPromotion(
CallbackHolder<DeleteUnblindedTokensForPromotionCallback>* holder,
const ledger::Result result) {
DCHECK(holder);
if (holder->is_valid()) {
std::move(holder->get()).Run(result);
}
delete holder;
NejcZdovc marked this conversation as resolved.
Show resolved Hide resolved
}

void LedgerClientMojoProxy::DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
DeleteUnblindedTokensForPromotionCallback callback) {
auto* holder = new CallbackHolder<DeleteUnblindedTokensForPromotionCallback>(
AsWeakPtr(),
std::move(callback));
ledger_client_->DeleteUnblindedTokensForPromotion(
promotion_id,
std::bind(LedgerClientMojoProxy::OnDeleteUnblindedTokensForPromotion,
holder,
_1));
}

void LedgerClientMojoProxy::GetClientInfo(
GetClientInfoCallback callback) {
auto info = ledger_client_->GetClientInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient,
const std::vector<std::string>& id_list,
DeleteUnblindedTokensCallback callback) override;

void DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
DeleteUnblindedTokensForPromotionCallback callback) override;

void GetClientInfo(
GetClientInfoCallback callback) override;

Expand Down Expand Up @@ -460,6 +464,10 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient,
CallbackHolder<DeleteUnblindedTokensCallback>* holder,
const ledger::Result result);

static void OnDeleteUnblindedTokensForPromotion(
CallbackHolder<DeleteUnblindedTokensForPromotionCallback>* holder,
const ledger::Result result);

ledger::LedgerClient* ledger_client_;

DISALLOW_COPY_AND_ASSIGN(LedgerClientMojoProxy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ interface BatLedgerClient {
InsertOrUpdateUnblindedToken(ledger.mojom.UnblindedToken info) => (ledger.mojom.Result result);
GetAllUnblindedTokens() => (array<ledger.mojom.UnblindedToken> info);
DeleteUnblindedTokens(array<string> id_list) => (ledger.mojom.Result result);
DeleteUnblindedTokensForPromotion(string promotion_id) => (ledger.mojom.Result result);
[Sync]
GetClientInfo() => (ledger.mojom.ClientInfo info);
UnblindedTokensReady();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ class MockConfirmationsClient : public ConfirmationsClient {
const std::vector<std::string>& id_list,
ledger::ResultCallback callback));

MOCK_METHOD2(DeleteUnblindedTokensForPromotion, void(
const std::string& promotion_id,
ledger::ResultCallback callback));

MOCK_METHOD0(GetClientInfo, ledger::ClientInfoPtr());

MOCK_METHOD0(UnblindedTokensReady, void());
Expand Down
8 changes: 6 additions & 2 deletions vendor/bat-native-ledger/include/bat/ledger/ledger_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,12 @@ class LEDGER_EXPORT LedgerClient {
ledger::GetAllUnblindedTokensCallback callback) = 0;

virtual void DeleteUnblindedTokens(
const std::vector<std::string>& id_list,
ledger::ResultCallback callback) = 0;
const std::vector<std::string>& id_list,
Copy link
Contributor

Choose a reason for hiding this comment

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

spacing was fixed for this function

ledger::ResultCallback callback) = 0;

virtual void DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) = 0;

virtual ledger::ClientInfoPtr GetClientInfo() = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ class MockLedgerClient : public LedgerClient {
MOCK_METHOD0(GetClientInfo, ledger::ClientInfoPtr());

MOCK_METHOD0(UnblindedTokensReady, void());

MOCK_METHOD2(DeleteUnblindedTokensForPromotion,
void(const std::string& promotion_id, ledger::ResultCallback));
};

} // namespace ledger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,12 @@ void LedgerImpl::DeleteUnblindedTokens(
ledger_client_->DeleteUnblindedTokens(id_list, callback);
}

void LedgerImpl::DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback) {
ledger_client_->DeleteUnblindedTokensForPromotion(promotion_id, callback);
}

ledger::ClientInfoPtr LedgerImpl::GetClientInfo() {
return ledger_client_->GetClientInfo();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ class LedgerImpl : public ledger::Ledger,
const std::vector<std::string>& id_list,
ledger::ResultCallback callback);

void DeleteUnblindedTokensForPromotion(
const std::string& promotion_id,
ledger::ResultCallback callback);

ledger::ClientInfoPtr GetClientInfo();

void UnblindedTokensReady();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,9 @@ class MockLedgerImpl : public LedgerImpl {
MOCK_METHOD0(UnblindedTokensReady, void());

MOCK_METHOD1(GetAnonWalletStatus, void(ledger::ResultCallback));

MOCK_METHOD2(DeleteUnblindedTokensForPromotion,
void(const std::string& promotion_id, ledger::ResultCallback));
};

} // namespace bat_ledger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,32 @@ using challenge_bypass_ristretto::UnblindedToken;

namespace braveledger_promotion {

namespace {

void HandleExpiredPromotions(
bat_ledger::LedgerImpl* ledger,
ledger::PromotionMap* promotions) {
DCHECK(promotions);
if (!promotions) {
return;
}

const uint64_t current_time =
static_cast<uint64_t>(base::Time::Now().ToDoubleT());

for (auto& item : *promotions) {
if (item.second->expires_at > 0 &&
item.second->expires_at <= current_time) {
item.second->status = ledger::PromotionStatus::OVER;

ledger->DeleteUnblindedTokensForPromotion(item.second->id,
[](const ledger::Result _){});
}
}
}

} // namespace

Promotion::Promotion(bat_ledger::LedgerImpl* ledger) :
attestation_(std::make_unique<braveledger_attestation::AttestationImpl>
(ledger)),
Expand Down Expand Up @@ -128,6 +154,8 @@ void Promotion::OnGetAllPromotions(
ledger::PromotionMap promotions,
const std::string& response,
ledger::FetchPromotionCallback callback) {
HandleExpiredPromotions(ledger_, &promotions);

ledger::PromotionList list;
bool success = ParseFetchResponse(response, &list);

Expand Down Expand Up @@ -284,6 +312,8 @@ void Promotion::OnTimer(const uint32_t timer_id) {
}

void Promotion::Retry(ledger::PromotionMap promotions) {
HandleExpiredPromotions(ledger_, &promotions);

for (auto & promotion : promotions) {
if (promotion.second->status == ledger::PromotionStatus::CLAIMED) {
FetchSignedTokens(
Expand Down
5 changes: 5 additions & 0 deletions vendor/brave-ios/Ledger/BATBraveLedger.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1997,4 +1997,9 @@ - (void)unblindedTokensReady
}
}

- (void)deleteUnblindedTokensForPromotion:(const std::string&)promotion_id callback:(ledger::ResultCallback)callback
{
// TODO please implement
NejcZdovc marked this conversation as resolved.
Show resolved Hide resolved
}

@end
1 change: 1 addition & 0 deletions vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class NativeLedgerClient : public ledger::LedgerClient {
void InsertOrUpdateUnblindedToken(ledger::UnblindedTokenPtr info, ledger::ResultCallback callback) override;
void GetAllUnblindedTokens(ledger::GetAllUnblindedTokensCallback callback) override;
void DeleteUnblindedTokens(const std::vector<std::string>& id_list, ledger::ResultCallback callback) override;
void DeleteUnblindedTokensForPromotion(const std::string& promotion_id, ledger::ResultCallback callback) override;
ledger::ClientInfoPtr GetClientInfo() override;
void UnblindedTokensReady() override;
};
3 changes: 3 additions & 0 deletions vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,6 @@
void NativeLedgerClient::GetAllPromotions(ledger::GetAllPromotionsCallback callback) {
[bridge_ getAllPromotions:callback];
}
void NativeLedgerClient::DeleteUnblindedTokensForPromotion(const std::string& promotion_id, ledger::ResultCallback callback) {
[bridge_ deleteUnblindedTokensForPromotion:promotion_id callback:callback];
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@
- (ledger::ClientInfoPtr)getClientInfo;
- (void)unblindedTokensReady;
- (void)getAllPromotions:(ledger::GetAllPromotionsCallback)callback;
- (void)deleteUnblindedTokensForPromotion:(const std::string&)promotion_id callback:(ledger::ResultCallback)callback;

@end