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 Mar 12, 2023
1 parent 97e803b commit 4ac9467
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "brave/components/brave_ads/core/internal/ads/ad_events/new_tab_page_ads/new_tab_page_ad_event_handler_observer.h"
#include "brave/components/brave_ads/core/internal/ads/serving/permission_rules/permission_rules_unittest_util.h"
#include "brave/components/brave_ads/core/internal/ads/serving/serving_features.h"
#include "brave/components/brave_ads/core/internal/catalog/catalog_util.h"
#include "brave/components/brave_ads/core/internal/common/unittest/unittest_base.h"
#include "brave/components/brave_ads/core/internal/common/unittest/unittest_time_util.h"
#include "brave/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ad_info.h"
Expand Down Expand Up @@ -333,7 +334,7 @@ TEST_F(BatAdsNewTabPageAdEventHandlerIfAdsDisabledTest,
creative_ad, AdType::kNewTabPageAd, ConfirmationType::kViewed, Now());
FireAdEvents(viewed_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 Down Expand Up @@ -367,7 +368,7 @@ TEST_F(BatAdsNewTabPageAdEventHandlerIfAdsDisabledTest,
creative_ad, AdType::kNewTabPageAd, ConfirmationType::kViewed, Now());
FireAdEvents(viewed_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
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,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 @@ -52,9 +52,9 @@ TEST_F(BatAdsCatalogPermissionRuleIntegrationTest,
}

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

// Act
CatalogPermissionRule permission_rule;
Expand Down
5 changes: 3 additions & 2 deletions components/brave_ads/core/internal/catalog/catalog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,17 @@ void Catalog::OnFetch(const mojom::UrlResponseInfo& 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
4 changes: 1 addition & 3 deletions components/brave_ads/core/internal/catalog/catalog_util.cc
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() {
return base::Time::Now() >= GetCatalogLastUpdated() + kCatalogLifespan;
return base::Time::Now() >= GetCatalogLastUpdated() + GetCatalogPing();
}

} // namespace ads
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ TEST_F(BatAdsCatalogUtilTest, CatalogHasNotChanged) {

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

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

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

// Act
Expand Down

0 comments on commit 4ac9467

Please sign in to comment.