Skip to content

Commit

Permalink
Incorporated review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
emerick committed Jul 5, 2019
1 parent 7b30926 commit 4d5030c
Show file tree
Hide file tree
Showing 39 changed files with 388 additions and 132 deletions.
1 change: 0 additions & 1 deletion browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ source_set("ui") {
deps += [
"//brave/components/brave_rewards/resources:tip_generated_resources",
"//brave/components/brave_rewards/resources:ui_generated_resources",
"//brave/vendor/bat-native-ads",
]
}
}
62 changes: 3 additions & 59 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include "base/i18n/time_formatting.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "bat/ads/ad_history_detail.h"
#include "bat/ads/ads_history.h"
#include "brave/common/webui_url_constants.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/browser/ads_service_factory.h"
Expand Down Expand Up @@ -90,8 +88,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void CheckImported(const base::ListValue* args);
void GetAdsData(const base::ListValue* args);
void GetAdsHistory(const base::ListValue* args);
void OnGetAdsHistory(
const std::map<std::string, std::vector<ads::AdsHistory>>& ads_history);
void OnGetAdsHistory(const base::ListValue& ads_history);
void ToggleAdThumbUp(const base::ListValue* args);
void OnToggleAdThumbUp(const std::string& id, int action);
void ToggleAdThumbDown(const base::ListValue* args);
Expand Down Expand Up @@ -1050,66 +1047,13 @@ void RewardsDOMHandler::GetAdsHistory(const base::ListValue* args) {
weak_factory_.GetWeakPtr()));
}

void RewardsDOMHandler::OnGetAdsHistory(
const std::map<std::string, std::vector<ads::AdsHistory>>& ads_history) {
void RewardsDOMHandler::OnGetAdsHistory(const base::ListValue& ads_history) {
if (!web_ui()->CanCallJavascript()) {
return;
}

base::Value ads_history_list(base::Value::Type::LIST);
int id = 0;

for (const auto& entry : ads_history) {
base::Value ads_history_dict(base::Value::Type::DICTIONARY);
ads_history_dict.SetKey("id", base::Value(std::to_string(id++)));
ads_history_dict.SetKey("date", base::Value(entry.first));

base::Value ad_history_details(base::Value::Type::LIST);

for (const auto& ads_history_entry : entry.second) {
for (const auto& detail : ads_history_entry.details) {
base::Value ad_content(base::Value::Type::DICTIONARY);
ad_content.SetKey("uuid", base::Value(detail.ad_content.uuid));
ad_content.SetKey("creativeSetId",
base::Value(detail.ad_content.creative_set_id));
ad_content.SetKey("brand", base::Value(detail.ad_content.brand));
ad_content.SetKey("brandInfo",
base::Value(detail.ad_content.brand_info));
ad_content.SetKey("brandLogo",
base::Value(detail.ad_content.brand_logo));
ad_content.SetKey("brandDisplayUrl",
base::Value(detail.ad_content.brand_display_url));
ad_content.SetKey("brandUrl", base::Value(detail.ad_content.brand_url));
ad_content.SetKey("likeAction",
base::Value(detail.ad_content.like_action));
ad_content.SetKey(
"adAction", base::Value(std::string(detail.ad_content.ad_action)));
ad_content.SetKey("savedAd", base::Value(detail.ad_content.saved_ad));
ad_content.SetKey("flaggedAd",
base::Value(detail.ad_content.flagged_ad));

base::Value category_content(base::Value::Type::DICTIONARY);
category_content.SetKey("category",
base::Value(detail.category_content.category));
category_content.SetKey(
"optAction", base::Value(detail.category_content.opt_action));

base::Value ad_history_detail(base::Value::Type::DICTIONARY);
ad_history_detail.SetKey("id", base::Value(detail.uuid));
ad_history_detail.SetPath("adContent", std::move(ad_content));
ad_history_detail.SetPath("categoryContent",
std::move(category_content));

ad_history_details.GetList().emplace_back(std::move(ad_history_detail));
}
}

ads_history_dict.SetPath("adDetailRows", std::move(ad_history_details));
ads_history_list.GetList().emplace_back(std::move(ads_history_dict));
}

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.adsHistory",
ads_history_list);
ads_history);
}

void RewardsDOMHandler::ToggleAdThumbUp(const base::ListValue* args) {
Expand Down
8 changes: 6 additions & 2 deletions components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ namespace ads {
struct AdsHistory;
}

namespace base {
class ListValue;
}

namespace brave_ads {

using OnGetAdsHistoryCallback = base::OnceCallback<void(
const std::map<std::string, std::vector<ads::AdsHistory>>&)>;
using OnGetAdsHistoryCallback =
base::OnceCallback<void(const base::ListValue&)>;

using OnToggleAdThumbUpCallback =
base::OnceCallback<void(const std::string&, int)>;
Expand Down
79 changes: 72 additions & 7 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -860,21 +860,80 @@ void AdsServiceImpl::GetAdsHistory(OnGetAdsHistoryCallback callback) {

void AdsServiceImpl::OnGetAdsHistory(
OnGetAdsHistoryCallback callback,
const base::flat_map<std::string, std::vector<std::string>>&
const base::flat_map<uint64_t, std::vector<std::string>>&
json_ads_history) {
std::map<std::string, std::vector<ads::AdsHistory>> ads_history_map;
// Reconstitute the map of AdsHistory items from JSON
std::map<uint64_t, std::vector<ads::AdsHistory>> ads_history_map;
for (const auto& entry : json_ads_history) {
std::vector<ads::AdsHistory> ads_history_list;
std::vector<ads::AdsHistory> ads_history_vector;
for (const auto& ads_history_entry : entry.second) {
ads::AdsHistory ads_history;
ads_history.FromJson(ads_history_entry);
ads_history_list.push_back(ads_history);
ads_history_vector.push_back(ads_history);
}
const std::string date = entry.first;
ads_history_map[date] = ads_history_list;
const uint64_t timestamp_in_seconds = entry.first;
ads_history_map[timestamp_in_seconds] = ads_history_vector;
}

std::move(callback).Run(ads_history_map);
// Build the list structure required by the WebUI
base::ListValue ads_history_list;
int id = 0;

for (const auto& entry : ads_history_map) {
base::DictionaryValue ads_history_dict;
ads_history_dict.SetKey("id", base::Value(std::to_string(id++)));
double timestamp_in_milliseconds =
base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromSeconds(entry.first))
.ToJsTime();
ads_history_dict.SetKey("timestampInMilliseconds",
base::Value(timestamp_in_milliseconds));

base::ListValue ad_history_details;

for (const auto& ads_history_entry : entry.second) {
for (const auto& detail : ads_history_entry.details) {
base::DictionaryValue ad_content;
ad_content.SetKey("uuid", base::Value(detail.ad_content.uuid));
ad_content.SetKey("creativeSetId",
base::Value(detail.ad_content.creative_set_id));
ad_content.SetKey("brand", base::Value(detail.ad_content.brand));
ad_content.SetKey("brandInfo",
base::Value(detail.ad_content.brand_info));
ad_content.SetKey("brandLogo",
base::Value(detail.ad_content.brand_logo));
ad_content.SetKey("brandDisplayUrl",
base::Value(detail.ad_content.brand_display_url));
ad_content.SetKey("brandUrl", base::Value(detail.ad_content.brand_url));
ad_content.SetKey("likeAction",
base::Value(detail.ad_content.like_action));
ad_content.SetKey(
"adAction", base::Value(std::string(detail.ad_content.ad_action)));
ad_content.SetKey("savedAd", base::Value(detail.ad_content.saved_ad));
ad_content.SetKey("flaggedAd",
base::Value(detail.ad_content.flagged_ad));

base::DictionaryValue category_content;
category_content.SetKey("category",
base::Value(detail.category_content.category));
category_content.SetKey(
"optAction", base::Value(detail.category_content.opt_action));

base::DictionaryValue ad_history_detail;
ad_history_detail.SetKey("id", base::Value(detail.uuid));
ad_history_detail.SetPath("adContent", std::move(ad_content));
ad_history_detail.SetPath("categoryContent",
std::move(category_content));

ad_history_details.GetList().emplace_back(std::move(ad_history_detail));
}
}

ads_history_dict.SetPath("adDetailRows", std::move(ad_history_details));
ads_history_list.GetList().emplace_back(std::move(ads_history_dict));
}

std::move(callback).Run(ads_history_list);
}

void AdsServiceImpl::ToggleAdThumbUp(const std::string& id,
Expand Down Expand Up @@ -1093,6 +1152,12 @@ void AdsServiceImpl::ConfirmAd(std::unique_ptr<ads::NotificationInfo> info) {
rewards_service_->ConfirmAd(info->ToJson());
}

void AdsServiceImpl::ConfirmAction(const std::string& uuid,
const std::string& creative_set_id,
const ads::ConfirmationType& type) {
rewards_service_->ConfirmAction(uuid, creative_set_id, type);
}

void AdsServiceImpl::NotificationTimedOut(uint32_t timer_id,
const std::string& notification_id) {
timers_.erase(timer_id);
Expand Down
5 changes: 4 additions & 1 deletion components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ class AdsServiceImpl : public AdsService,
void ShowNotification(std::unique_ptr<ads::NotificationInfo> info) override;
void SetCatalogIssuers(std::unique_ptr<ads::IssuersInfo> info) override;
void ConfirmAd(std::unique_ptr<ads::NotificationInfo> info) override;
void ConfirmAction(const std::string& uuid,
const std::string& creative_set_id,
const ads::ConfirmationType& type) override;
uint32_t SetTimer(const uint64_t time_offset) override;
void KillTimer(uint32_t timer_id) override;
void URLRequest(
Expand Down Expand Up @@ -210,7 +213,7 @@ class AdsServiceImpl : public AdsService,

void OnGetAdsHistory(
OnGetAdsHistoryCallback callback,
const base::flat_map<std::string, std::vector<std::string>>&
const base::flat_map<uint64_t, std::vector<std::string>>&
json_ads_history);

void OnToggleAdThumbUp(OnToggleAdThumbUpCallback callback,
Expand Down
4 changes: 4 additions & 0 deletions components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ class MockRewardsService : public RewardsService {
void(const brave_rewards::GetRewardsMainEnabledCallback&));
MOCK_METHOD1(SetCatalogIssuers, void(const std::string&));
MOCK_METHOD1(ConfirmAd, void(const std::string&));
MOCK_METHOD3(ConfirmAction,
void(const std::string&,
const std::string&,
const std::string&));
MOCK_METHOD1(GetRewardsInternalsInfo,
void(brave_rewards::GetRewardsInternalsInfoCallback));
MOCK_METHOD1(GetAddressesForPaymentId,
Expand Down
3 changes: 3 additions & 0 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ class RewardsService : public KeyedService {
// as ledger
virtual void SetCatalogIssuers(const std::string& json) = 0;
virtual void ConfirmAd(const std::string& json) = 0;
virtual void ConfirmAction(const std::string& uuid,
const std::string& creative_set_id,
const std::string& type) = 0;
virtual void GetRewardsInternalsInfo(
GetRewardsInternalsInfoCallback callback) = 0;

Expand Down
10 changes: 10 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,16 @@ void RewardsServiceImpl::ConfirmAd(const std::string& json) {
bat_ledger_->ConfirmAd(json);
}

void RewardsServiceImpl::ConfirmAction(const std::string& uuid,
const std::string& creative_set_id,
const std::string& type) {
if (!Connected()) {
return;
}

bat_ledger_->ConfirmAction(uuid, creative_set_id, type);
}

void RewardsServiceImpl::SetConfirmationsIsReady(const bool is_ready) {
auto* ads_service = brave_ads::AdsServiceFactory::GetForProfile(profile_);
if (ads_service)
Expand Down
3 changes: 3 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ class RewardsServiceImpl : public RewardsService,
void UpdateAdsRewards() const override;
void SetCatalogIssuers(const std::string& json) override;
void ConfirmAd(const std::string& json) override;
void ConfirmAction(const std::string& uuid,
const std::string& creative_set_id,
const std::string& type) override;
void SetConfirmationsIsReady(const bool is_ready) override;
void GetTransactionHistory(
GetTransactionHistoryCallback callback) override;
Expand Down
11 changes: 8 additions & 3 deletions components/brave_rewards/resources/ui/components/adsBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,18 @@ class AdsBox extends React.Component<Props, State> {
return adHistoryData.map((item: Rewards.AdsHistoryData) => {
return {
id: item.id,
date: item.date,
date: new Date(item.timestampInMilliseconds).toLocaleDateString(),
adDetailRows: (
item.adDetailRows.map((itemDetail: Rewards.AdHistoryDetail) => {
let brandInfo = itemDetail.adContent.brandInfo
if (brandInfo.length > 50) {
brandInfo = brandInfo.substring(0, 50) + '...'
}
const adContent: Rewards.AdContent = {
uuid: itemDetail.adContent.uuid,
creativeSetId: itemDetail.adContent.creativeSetId,
brand: itemDetail.adContent.brand,
brandInfo: itemDetail.adContent.brandInfo,
brandInfo: brandInfo,
brandLogo: itemDetail.adContent.brandLogo,
brandDisplayUrl: itemDetail.adContent.brandDisplayUrl,
brandUrl: itemDetail.adContent.brandUrl,
Expand Down Expand Up @@ -250,10 +254,10 @@ class AdsBox extends React.Component<Props, State> {
}

render () {
const adsPerHour = 2
const savedOnly: boolean = false

let adsEnabled = false
let adsPerHour = 0
let adsUIEnabled = false
let adsIsSupported = false
let estimatedPendingRewards = '0'
Expand All @@ -270,6 +274,7 @@ class AdsBox extends React.Component<Props, State> {

if (adsData) {
adsEnabled = adsData.adsEnabled
adsPerHour = adsData.adsPerHour
adsUIEnabled = adsData.adsUIEnabled
adsIsSupported = adsData.adsIsSupported
estimatedPendingRewards = (adsData.adsEstimatedPendingRewards || 0).toFixed(2)
Expand Down
9 changes: 9 additions & 0 deletions components/services/bat_ads/bat_ads_client_mojo_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ void BatAdsClientMojoBridge::ConfirmAd(
bat_ads_client_->ConfirmAd(info->ToJson());
}

void BatAdsClientMojoBridge::ConfirmAction(const std::string& uuid,
const std::string& creative_set_id,
const ads::ConfirmationType& type) {
if (!connected())
return;

bat_ads_client_->ConfirmAction(uuid, creative_set_id, type);
}

uint32_t BatAdsClientMojoBridge::SetTimer(const uint64_t time_offset) {
if (!connected())
return 0;
Expand Down
3 changes: 3 additions & 0 deletions components/services/bat_ads/bat_ads_client_mojo_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class BatAdsClientMojoBridge : public ads::AdsClient {
void ShowNotification(std::unique_ptr<ads::NotificationInfo> info) override;
void SetCatalogIssuers(std::unique_ptr<ads::IssuersInfo> info) override;
void ConfirmAd(std::unique_ptr<ads::NotificationInfo> info) override;
void ConfirmAction(const std::string& uuid,
const std::string& creative_set_id,
const ads::ConfirmationType& type) override;
uint32_t SetTimer(const uint64_t time_offset) override;
void KillTimer(uint32_t timer_id) override;
void URLRequest(const std::string& url,
Expand Down
5 changes: 3 additions & 2 deletions components/services/bat_ads/bat_ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "bat/ads/ads.h"
#include "bat/ads/ads_history.h"
#include "bat/ads/category_content.h"
#include "bat/ads/confirmation_type.h"
#include "brave/components/services/bat_ads/bat_ads_client_mojo_bridge.h"
#include "mojo/public/cpp/bindings/map.h"

Expand All @@ -33,7 +34,7 @@ ads::CategoryContent::OptAction ToAdsOptAction(int action) {
return static_cast<ads::CategoryContent::OptAction>(action);
}

}
} // namespace

BatAdsImpl::BatAdsImpl(mojom::BatAdsClientAssociatedPtrInfo client_info)
: bat_ads_client_mojo_proxy_(
Expand Down Expand Up @@ -126,7 +127,7 @@ void BatAdsImpl::GenerateAdReportingNotificationResultEvent(
}

void BatAdsImpl::GetAdsHistory(GetAdsHistoryCallback callback) {
std::map<std::string, std::vector<std::string>> result;
std::map<uint64_t, std::vector<std::string>> result;

auto ads_history_map = ads_->GetAdsHistory();
for (const auto& entry : ads_history_map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@ void AdsClientMojoBridge::ConfirmAd(const std::string& notification_info) {
ads_client_->ConfirmAd(std::move(info));
}

void AdsClientMojoBridge::ConfirmAction(const std::string& uuid,
const std::string& creative_set_id,
const std::string& type) {
ads_client_->ConfirmAction(uuid, creative_set_id,
ads::ConfirmationType(type));
}

// static
void AdsClientMojoBridge::OnSaveBundleState(
CallbackHolder<SaveBundleStateCallback>* holder,
Expand Down
Loading

0 comments on commit 4d5030c

Please sign in to comment.