From 5e7b4faba25e06092495c446320f1dadf06c980f Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Tue, 3 Sep 2019 09:43:38 +0100 Subject: [PATCH] Do not show the same ad more than once every hour Fixes https://github.com/brave/brave-browser/issues/5281 --- .../src/bat/ads/internal/ads_impl.cc | 44 ++++++++++++++++--- .../src/bat/ads/internal/ads_impl.h | 6 +++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc index e8cf929daa90..10bf36a1d3ed 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc @@ -959,34 +959,43 @@ std::vector AdsImpl::GetEligibleAds( for (const auto& ad : ads) { if (!AdRespectsTotalMaxFrequencyCapping(ad)) { BLOG(WARNING) << "creativeSetId " << ad.creative_set_id - << " has exceeded the totalMax"; + << " has exceeded the frequency capping for totalMax"; + + continue; + } + + if (!AdRespectsPerHourFrequencyCapping(ad)) { + BLOG(WARNING) << "adUUID " << ad.uuid + << " has exceeded the frequency capping for perHour"; continue; } if (!AdRespectsPerDayFrequencyCapping(ad)) { BLOG(WARNING) << "creativeSetId " << ad.creative_set_id - << " has exceeded the perDay"; + << " has exceeded the frequency capping for perDay"; continue; } if (!AdRespectsDailyCapFrequencyCapping(ad)) { - BLOG(WARNING) << "creativeSetId " << ad.creative_set_id - << " has exceeded the dailyCap"; + BLOG(WARNING) << "campaignId " << ad.campaign_id + << " has exceeded the frequency capping for dailyCap"; continue; } if (client_->IsFilteredAd(ad.creative_set_id)) { BLOG(WARNING) << "creativeSetId " << ad.creative_set_id - << " appears in filtered ads list"; + << " appears in the filtered ads list"; + continue; } if (client_->IsFlaggedAd(ad.creative_set_id)) { BLOG(WARNING) << "creativeSetId " << ad.creative_set_id - << " appears in flagged ads list"; + << " appears in the flagged ads list"; + continue; } @@ -1006,6 +1015,15 @@ bool AdsImpl::AdRespectsTotalMaxFrequencyCapping( return true; } +bool AdsImpl::AdRespectsPerHourFrequencyCapping( + const AdInfo& ad) { + auto ads_shown = GetAdsShownForId(ad.uuid); + auto hour_window = base::Time::kSecondsPerHour; + + return HistoryRespectsRollingTimeConstraint( + ads_shown, hour_window, 1); +} + bool AdsImpl::AdRespectsPerDayFrequencyCapping( const AdInfo& ad) { auto creative_set = GetCreativeSetForId(ad.creative_set_id); @@ -1024,6 +1042,20 @@ bool AdsImpl::AdRespectsDailyCapFrequencyCapping( campaign, day_window, ad.daily_cap); } +std::deque AdsImpl::GetAdsShownForId( + const std::string& id) { + std::deque ads_shown = {}; + + auto ads_shown_history = client_->GetAdsShownHistory(); + for (const auto& ad_shown : ads_shown_history) { + if (ad_shown.ad_content.uuid == id) { + ads_shown.push_back(ad_shown.timestamp_in_seconds); + } + } + + return ads_shown; +} + std::deque AdsImpl::GetCreativeSetForId( const std::string& id) { std::deque creative_set = {}; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h index f0d4cfc09c94..a14ea3c02da8 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h @@ -206,14 +206,20 @@ class AdsImpl : public Ads { bool AdRespectsTotalMaxFrequencyCapping( const AdInfo& ad); + bool AdRespectsPerHourFrequencyCapping( + const AdInfo& ad); bool AdRespectsPerDayFrequencyCapping( const AdInfo& ad); bool AdRespectsDailyCapFrequencyCapping( const AdInfo& ad); + + std::deque GetAdsShownForId( + const std::string& id); std::deque GetCreativeSetForId( const std::string& id); std::deque GetCampaignForId( const std::string& id); + bool IsAdValid( const AdInfo& ad_info); NotificationInfo last_shown_notification_info_;