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

Fix Brave Ads save and dislike filters #11679

Merged
merged 1 commit into from
Dec 29, 2021
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 @@ -272,8 +272,8 @@ class AdsBox extends React.Component<Props, State> {
this.props.actions.toggleAdOptOut(category, action)
}

onMenuSave = (creativeInstanceId: string, creativeSetId: string, saved: boolean) => {
this.props.actions.toggleSavedAd(creativeInstanceId, creativeSetId, saved)
onMenuSave = (adContent: Rewards.AdContent) => {
this.props.actions.toggleSavedAd(adContent)
}

onMenuFlag = (adContent: Rewards.AdContent) => {
Expand Down Expand Up @@ -339,9 +339,7 @@ class AdsBox extends React.Component<Props, State> {
onThumbDownPress: () =>
this.onThumbDownPress(adHistory.adContent),
onMenuSave: () =>
this.onMenuSave(adHistory.adContent.creativeInstanceId,
adHistory.adContent.creativeSetId,
adHistory.adContent.savedAd),
this.onMenuSave(adHistory.adContent),
onMenuFlag: () =>
this.onMenuFlag(adHistory.adContent)
}
Expand Down
14 changes: 9 additions & 5 deletions ios/browser/api/ads/brave_ads.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,15 @@ OBJC_EXPORT
NSDate* _Nullable nextPaymentDate))completion
NS_SWIFT_NAME(detailsForCurrentCycle(_:));

/// Toggle that the user liked the given ad and more like it should be shown
- (void)toggleThumbsUpForAd:(NSString*)creativeInstanceId;

/// Toggle that the user disliked the given ad and it shouldn't be shown again
- (void)toggleThumbsDownForAd:(NSString*)creativeInstanceId;
/// Toggle that the user liked the given ad and advertiser and more like it
/// should be shown
- (void)toggleThumbsUpForAd:(NSString*)creativeInstanceId
advertiserId:(NSString*)advertiserId;

/// Toggle that the user disliked the given ad and advertiser and it shouldn't
/// be shown again
- (void)toggleThumbsDownForAd:(NSString*)creativeInstanceId
advertiserId:(NSString*)advertiserId;

#pragma mark -

Expand Down
10 changes: 8 additions & 2 deletions ios/browser/api/ads/brave_ads.mm
Original file line number Diff line number Diff line change
Expand Up @@ -681,21 +681,27 @@ - (void)detailsForCurrentCycle:(void (^)(NSInteger adsReceived,
});
}

- (void)toggleThumbsUpForAd:(NSString*)creativeInstanceId {
- (void)toggleThumbsUpForAd:(NSString*)creativeInstanceId
advertiserId:(NSString*)advertiserId {
if (![self isAdsServiceRunning]) {
return;
}
ads::AdContentInfo info;
info.creative_instance_id = base::SysNSStringToUTF8(creativeInstanceId);
info.advertiser_id = base::SysNSStringToUTF8(advertiserId);
info.type = ads::AdType::kAdNotification;
ads->ToggleAdThumbUp(info.ToJson());
}

- (void)toggleThumbsDownForAd:(NSString*)creativeInstanceId {
- (void)toggleThumbsDownForAd:(NSString*)creativeInstanceId
advertiserId:(NSString*)advertiserId {
if (![self isAdsServiceRunning]) {
return;
}
ads::AdContentInfo info;
info.creative_instance_id = base::SysNSStringToUTF8(creativeInstanceId);
info.advertiser_id = base::SysNSStringToUTF8(advertiserId);
info.type = ads::AdType::kAdNotification;
ads->ToggleAdThumbDown(info.ToJson());
}

Expand Down
27 changes: 14 additions & 13 deletions vendor/bat-native-ads/src/bat/ads/ad_content_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ bool AdContentInfo::FromValue(const base::Value& value) {
return false;
}

const absl::optional<int> ad_type = dictionary->FindIntKey("adType");
if (ad_type && ad_type.has_value()) {
type = AdType(static_cast<AdType::Value>(ad_type.value()));
const absl::optional<int> type_optional = dictionary->FindIntKey("adType");
if (type_optional) {
type = AdType(static_cast<AdType::Value>(type_optional.value()));
}

const std::string* uuid_value = dictionary->FindStringKey("uuid");
Expand Down Expand Up @@ -134,11 +134,11 @@ bool AdContentInfo::FromValue(const base::Value& value) {
brand_url = *brand_url_value;
}

const absl::optional<int> like_action_type_value =
const absl::optional<int> like_action_type_optional =
dictionary->FindIntKey("likeAction");
if (like_action_type_value && like_action_type_value.has_value()) {
if (like_action_type_optional) {
like_action_type =
static_cast<AdContentLikeActionType>(like_action_type_value.value());
static_cast<AdContentLikeActionType>(like_action_type_optional.value());
}

const std::string* confirmation_type_value =
Expand All @@ -147,15 +147,16 @@ bool AdContentInfo::FromValue(const base::Value& value) {
confirmation_type = ConfirmationType(*confirmation_type_value);
}

const absl::optional<int> is_saved_value = dictionary->FindIntKey("savedAd");
if (is_saved_value && is_saved_value.has_value()) {
is_saved = static_cast<bool>(is_saved_value.value());
const absl::optional<bool> is_saved_optional =
dictionary->FindBoolKey("savedAd");
if (is_saved_optional) {
is_saved = is_saved_optional.value();
}

const absl::optional<int> is_flagged_value =
dictionary->FindIntKey("flaggedAd");
if (is_flagged_value && is_flagged_value.has_value()) {
is_flagged = static_cast<bool>(is_flagged_value.value());
const absl::optional<bool> is_flagged_optional =
dictionary->FindBoolKey("flaggedAd");
if (is_flagged_optional) {
is_flagged = is_flagged_optional.value();
}

return true;
Expand Down
100 changes: 34 additions & 66 deletions vendor/bat-native-ads/src/bat/ads/internal/client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,33 @@ FilteredAdvertiserList::iterator FindFilteredAdvertiser(
}

FilteredCategoryList::iterator FindFilteredCategory(
const std::string& name,
const std::string& category,
FilteredCategoryList* filtered_categories) {
DCHECK(filtered_categories);

return std::find_if(filtered_categories->begin(), filtered_categories->end(),
[&name](const FilteredCategoryInfo& category) {
return category.name == name;
});
return std::find_if(
filtered_categories->begin(), filtered_categories->end(),
[&category](const FilteredCategoryInfo& filtered_category) {
return filtered_category.name == category;
});
}

CategoryContentOptActionType ToggleOptInActionType(
const CategoryContentOptActionType action_type) {
if (action_type == CategoryContentOptActionType::kOptIn) {
return CategoryContentOptActionType::kNone;
} else {
return CategoryContentOptActionType::kOptIn;
}

return CategoryContentOptActionType::kOptIn;
}

CategoryContentOptActionType ToggleOptOutActionType(
const CategoryContentOptActionType action_type) {
if (action_type == CategoryContentOptActionType::kOptOut) {
return CategoryContentOptActionType::kNone;
} else {
return CategoryContentOptActionType::kOptOut;
}

return CategoryContentOptActionType::kOptOut;
}

} // namespace
Expand Down Expand Up @@ -192,8 +193,7 @@ AdContentLikeActionType Client::ToggleAdThumbUp(
ad_content.ToggleThumbUpActionType();

for (auto& item : client_->ads_shown_history) {
if (item.ad_content.creative_instance_id ==
ad_content.creative_instance_id) {
if (item.ad_content.advertiser_id == ad_content.advertiser_id) {
item.ad_content.like_action_type = like_action_type;
}
}
Expand Down Expand Up @@ -228,8 +228,7 @@ AdContentLikeActionType Client::ToggleAdThumbDown(
}

for (auto& item : client_->ads_shown_history) {
if (item.ad_content.creative_instance_id ==
ad_content.creative_instance_id) {
if (item.ad_content.advertiser_id == ad_content.advertiser_id) {
item.ad_content.like_action_type = like_action_type;
}
}
Expand Down Expand Up @@ -285,6 +284,7 @@ CategoryContentOptActionType Client::ToggleAdOptOut(

const auto iter = FindFilteredCategory(
category, &client_->ad_preferences.filtered_categories);

if (toggled_opt_action_type == CategoryContentOptActionType::kNone) {
if (iter != client_->ad_preferences.filtered_categories.end()) {
client_->ad_preferences.filtered_categories.erase(iter);
Expand Down Expand Up @@ -323,22 +323,18 @@ bool Client::ToggleSavedAd(const AdContentInfo& ad_content) {
DCHECK(is_initialized_);

const bool is_saved = !ad_content.is_saved;

const auto iter = std::find_if(client_->ad_preferences.saved_ads.cbegin(),
client_->ad_preferences.saved_ads.cend(),
[&ad_content](const SavedAdInfo& saved_ad) {
return saved_ad.creative_instance_id ==
ad_content.creative_instance_id;
});

if (is_saved) {
if (iter == client_->ad_preferences.saved_ads.end()) {
SavedAdInfo saved_ad;
saved_ad.creative_instance_id = ad_content.creative_instance_id;
saved_ad.creative_set_id = ad_content.creative_set_id;
client_->ad_preferences.saved_ads.push_back(saved_ad);
}
SavedAdInfo saved_ad;
saved_ad.creative_instance_id = ad_content.creative_instance_id;
client_->ad_preferences.saved_ads.push_back(saved_ad);
} else {
const auto iter = std::find_if(client_->ad_preferences.saved_ads.cbegin(),
client_->ad_preferences.saved_ads.cend(),
[&ad_content](const SavedAdInfo& saved_ad) {
return saved_ad.creative_instance_id ==
ad_content.creative_instance_id;
});

if (iter != client_->ad_preferences.saved_ads.end()) {
client_->ad_preferences.saved_ads.erase(iter);
}
Expand All @@ -356,46 +352,29 @@ bool Client::ToggleSavedAd(const AdContentInfo& ad_content) {
return is_saved;
}

bool Client::GetSavedAdForCreativeInstanceId(
const std::string& creative_instance_id) {
for (const auto& element : client_->ads_shown_history) {
if (element.ad_content.creative_instance_id == creative_instance_id) {
return element.ad_content.is_saved;
}
}

return false;
}

bool Client::ToggleFlaggedAd(const AdContentInfo& ad_content) {
DCHECK(is_initialized_);

const bool is_flagged = !ad_content.is_flagged;

const auto iter =
std::find_if(client_->ad_preferences.flagged_ads.cbegin(),
client_->ad_preferences.flagged_ads.cend(),
[&ad_content](const FlaggedAdInfo& flagged_ad) {
return flagged_ad.creative_instance_id ==
ad_content.creative_instance_id;
});

if (is_flagged) {
if (iter == client_->ad_preferences.flagged_ads.end()) {
FlaggedAdInfo flagged_ad;
flagged_ad.creative_instance_id = ad_content.creative_instance_id;
flagged_ad.creative_set_id = ad_content.creative_set_id;
client_->ad_preferences.flagged_ads.push_back(flagged_ad);
}
FlaggedAdInfo flagged_ad;
flagged_ad.creative_set_id = ad_content.creative_set_id;
client_->ad_preferences.flagged_ads.push_back(flagged_ad);
aseren marked this conversation as resolved.
Show resolved Hide resolved
} else {
const auto iter = std::find_if(
client_->ad_preferences.flagged_ads.cbegin(),
client_->ad_preferences.flagged_ads.cend(),
[&ad_content](const FlaggedAdInfo& flagged_ad) {
return flagged_ad.creative_set_id == ad_content.creative_set_id;
});

if (iter != client_->ad_preferences.flagged_ads.end()) {
client_->ad_preferences.flagged_ads.erase(iter);
}
}

for (auto& item : client_->ads_shown_history) {
if (item.ad_content.creative_instance_id ==
ad_content.creative_instance_id) {
if (item.ad_content.creative_set_id == ad_content.creative_set_id) {
item.ad_content.is_flagged = is_flagged;
}
}
Expand All @@ -405,17 +384,6 @@ bool Client::ToggleFlaggedAd(const AdContentInfo& ad_content) {
return is_flagged;
}

bool Client::GetFlaggedAdForCreativeInstanceId(
const std::string& creative_instance_id) {
for (const auto& element : client_->ads_shown_history) {
if (element.ad_content.creative_instance_id == creative_instance_id) {
return element.ad_content.is_flagged;
}
}

return false;
}

void Client::UpdateSeenAd(const AdInfo& ad) {
DCHECK(is_initialized_);

Expand Down
3 changes: 0 additions & 3 deletions vendor/bat-native-ads/src/bat/ads/internal/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ class Client final {
const std::string& segment);

bool ToggleSavedAd(const AdContentInfo& ad_content);
bool GetSavedAdForCreativeInstanceId(const std::string& creative_instance_id);

bool ToggleFlaggedAd(const AdContentInfo& ad_content);
bool GetFlaggedAdForCreativeInstanceId(
const std::string& creative_instance_id);

void UpdateSeenAd(const AdInfo& ad);
const std::map<std::string, bool>& GetSeenAdsForType(const AdType& type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,42 @@ bool AdPreferencesInfo::FromJson(const std::string& json) {
}

for (const auto& advertiser : document["filtered_advertisers"].GetArray()) {
if (!advertiser["id"].IsString()) {
return false;
if (!advertiser.HasMember("id") || advertiser["id"].IsString()) {
continue;
}

FilteredAdvertiserInfo filtered_advertiser;
filtered_advertiser.id = advertiser["id"].GetString();
filtered_advertisers.push_back(filtered_advertiser);
}

for (const auto& ad : document["filtered_categories"].GetArray()) {
if (!ad["name"].IsString()) {
return false;
for (const auto& category : document["filtered_categories"].GetArray()) {
if (!category.HasMember("name") || category["name"].IsString()) {
continue;
}

FilteredCategoryInfo filtered_category;
filtered_category.name = ad["name"].GetString();
filtered_category.name = category["name"].GetString();
filtered_categories.push_back(filtered_category);
}

for (const auto& ad : document["saved_ads"].GetArray()) {
if (!ad["uuid"].IsString() || !ad["creative_set_id"].IsString()) {
return false;
if (!ad.HasMember("creative_instance_id") ||
!ad["creative_instance_id"].IsString()) {
continue;
}

SavedAdInfo saved_ad;
saved_ad.creative_instance_id = ad["uuid"].GetString();
saved_ad.creative_set_id = ad["creative_set_id"].GetString();
saved_ad.creative_instance_id = ad["creative_instance_id"].GetString();
saved_ads.push_back(saved_ad);
}

for (const auto& ad : document["flagged_ads"].GetArray()) {
if (!ad["uuid"].IsString() || !ad["creative_set_id"].IsString()) {
return false;
if (!ad.HasMember("creative_set_id") || !ad["creative_set_id"].IsString()) {
continue;
}

FlaggedAdInfo flagged_ad;
flagged_ad.creative_instance_id = ad["uuid"].GetString();
flagged_ad.creative_set_id = ad["creative_set_id"].GetString();
flagged_ads.push_back(flagged_ad);
}
Expand Down Expand Up @@ -108,12 +107,9 @@ void SaveToJson(JsonWriter* writer, const AdPreferencesInfo& info) {
for (const auto& ad : info.saved_ads) {
writer->StartObject();

writer->String("uuid");
writer->String("creative_instance_id");
writer->String(ad.creative_instance_id.c_str());

writer->String("creative_set_id");
writer->String(ad.creative_set_id.c_str());

writer->EndObject();
}
writer->EndArray();
Expand All @@ -123,9 +119,6 @@ void SaveToJson(JsonWriter* writer, const AdPreferencesInfo& info) {
for (const auto& ad : info.flagged_ads) {
writer->StartObject();

writer->String("uuid");
writer->String(ad.creative_instance_id.c_str());

writer->String("creative_set_id");
writer->String(ad.creative_set_id.c_str());

Expand Down
Loading