Skip to content

Commit

Permalink
Expire Brave Ads catalog after last_updated + ping
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Jul 5, 2022
1 parent 00127e8 commit d52a18c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "bat/ads/internal/ads_client_helper.h"
#include "bat/ads/internal/base/unittest/unittest_base.h"
#include "bat/ads/internal/base/unittest/unittest_time_util.h"
#include "bat/ads/internal/catalog/catalog_util.h"
#include "bat/ads/internal/creatives/new_tab_page_ads/creative_new_tab_page_ad_info.h"
#include "bat/ads/internal/creatives/new_tab_page_ads/creative_new_tab_page_ad_unittest_util.h"
#include "bat/ads/internal/creatives/new_tab_page_ads/new_tab_page_ad_builder.h"
Expand Down Expand Up @@ -313,7 +314,7 @@ TEST_F(BatAdsNewTabPageAdEventHandlerIfAdsDisabledTest,

FireAdEvents(ad_event, ads_per_day - 1);

AdvanceClockBy(base::Days(1) - base::Seconds(1));
AdvanceClockBy(GetCatalogPing() - base::Seconds(1));

const std::string placement_id =
base::GUID::GenerateRandomV4().AsLowercaseString();
Expand All @@ -339,7 +340,7 @@ TEST_F(BatAdsNewTabPageAdEventHandlerIfAdsDisabledTest,

FireAdEvents(ad_event, ads_per_day);

AdvanceClockBy(base::Days(1) - base::Seconds(1));
AdvanceClockBy(GetCatalogPing() - base::Seconds(1));

const std::string placement_id =
base::GUID::GenerateRandomV4().AsLowercaseString();
Expand Down
4 changes: 2 additions & 2 deletions vendor/bat-native-ads/src/bat/ads/internal/catalog/catalog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ void Catalog::OnFetch(const mojom::UrlResponse& url_response) {
return;
}

SetCatalogLastUpdated(base::Time::Now());

if (!HasCatalogChanged(catalog.id)) {
BLOG(1, "Catalog id " << catalog.id << " is up to date");
FetchAfterDelay();
return;
}

SetCatalogLastUpdated(base::Time::Now());

SaveCatalog(catalog);
NotifyDidUpdateCatalog(catalog);
FetchAfterDelay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ namespace ads {

namespace {

constexpr base::TimeDelta kCatalogLifespan = base::Days(1);

void Delete() {
database::DeleteCampaigns();
database::DeleteCreativeNotificationAds();
Expand Down Expand Up @@ -122,7 +120,7 @@ bool HasCatalogChanged(const std::string& catalog_id) {
}

bool HasCatalogExpired() {
if (base::Time::Now() < GetCatalogLastUpdated() + kCatalogLifespan) {
if (base::Time::Now() < GetCatalogLastUpdated() + GetCatalogPing()) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ TEST_F(BatAdsCatalogUtilTest, CatalogHasNotChanged) {

TEST_F(BatAdsCatalogUtilTest, CatalogHasExpired) {
// Arrange
SetCatalogPing(base::Days(1));
SetCatalogLastUpdated(Now());

// Act
Expand All @@ -99,6 +100,7 @@ TEST_F(BatAdsCatalogUtilTest, CatalogHasExpired) {

TEST_F(BatAdsCatalogUtilTest, CatalogHasNotExpired) {
// Arrange
SetCatalogPing(base::Days(1));
SetCatalogLastUpdated(Now());

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ TEST_F(BatAdsCatalogPermissionRuleIntegrationTest, AllowAd) {
}

TEST_F(BatAdsCatalogPermissionRuleIntegrationTest,
AllowAdIfCatalogWasLastUpdated23HoursAnd59MinutesAgo) {
AllowAdIfCatalogWasLastUpdatedOnOrBeforeCatalogPing) {
// Arrange
AdvanceClockBy(base::Days(1) - base::Seconds(1));
AdvanceClockBy(GetCatalogPing() - base::Seconds(1));

// Act
CatalogPermissionRule permission_rule;
Expand All @@ -56,9 +56,9 @@ TEST_F(BatAdsCatalogPermissionRuleIntegrationTest,
}

TEST_F(BatAdsCatalogPermissionRuleIntegrationTest,
DoNotAllowAdIfCatalogWasLastUpdated1DayAgo) {
DoNotAllowAdIfCatalogWasNotUpdatedAfterCatalogPing) {
// Arrange
AdvanceClockBy(base::Days(1));
AdvanceClockBy(GetCatalogPing());

// Act
CatalogPermissionRule permission_rule;
Expand Down

0 comments on commit d52a18c

Please sign in to comment.