Skip to content

Commit

Permalink
Merge pull request #4109 from /issues/6757
Browse files Browse the repository at this point in the history
Ads History should be displayed in reverse chronological order
  • Loading branch information
tmancey authored Dec 9, 2019
2 parents 8e48e61 + 8d96731 commit 33f6998
Show file tree
Hide file tree
Showing 50 changed files with 1,271 additions and 470 deletions.
21 changes: 18 additions & 3 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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 base::ListValue& ads_history);
void OnGetAdsHistory(const base::ListValue& 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 @@ -278,6 +278,12 @@ class RewardsDOMHandler : public WebUIMessageHandler,
DISALLOW_COPY_AND_ASSIGN(RewardsDOMHandler);
};

namespace {

const int kDaysOfAdsHistory = 7;

} // namespace

RewardsDOMHandler::RewardsDOMHandler() : weak_factory_(this) {}

RewardsDOMHandler::~RewardsDOMHandler() {
Expand Down Expand Up @@ -1125,8 +1131,17 @@ void RewardsDOMHandler::GetAdsHistory(const base::ListValue* args) {
return;
}

ads_service_->GetAdsHistory(base::Bind(&RewardsDOMHandler::OnGetAdsHistory,
weak_factory_.GetWeakPtr()));
const base::Time to_time = base::Time::Now();
const uint64_t to_timestamp = to_time.ToDoubleT();

const base::Time from_time = to_time -
base::TimeDelta::FromDays(kDaysOfAdsHistory - 1);
const base::Time from_time_local_midnight = from_time.LocalMidnight();
const uint64_t from_timestamp = from_time_local_midnight.ToDoubleT();

ads_service_->GetAdsHistory(from_timestamp, to_timestamp,
base::BindOnce(&RewardsDOMHandler::OnGetAdsHistory,
weak_factory_.GetWeakPtr()));
}

void RewardsDOMHandler::OnGetAdsHistory(const base::ListValue& ads_history) {
Expand Down
2 changes: 2 additions & 0 deletions components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class AdsService : public KeyedService {
const SessionID& tab_id) = 0;

virtual void GetAdsHistory(
const uint64_t from_timestamp,
const uint64_t to_timestamp,
OnGetAdsHistoryCallback callback) = 0;

virtual void ToggleAdThumbUp(
Expand Down
134 changes: 62 additions & 72 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "base/task_runner_util.h"
#include "base/time/time.h"
#include "base/i18n/time_formatting.h"
#include "bat/ads/ad_history_detail.h"
#include "bat/ads/ad_history.h"
#include "bat/ads/ads.h"
#include "bat/ads/ads_history.h"
#include "bat/ads/notification_info.h"
Expand Down Expand Up @@ -516,13 +516,16 @@ void AdsServiceImpl::OnTabClosed(
}

void AdsServiceImpl::GetAdsHistory(
const uint64_t from_timestamp,
const uint64_t to_timestamp,
OnGetAdsHistoryCallback callback) {
if (!connected()) {
return;
}

bat_ads_->GetAdsHistory(base::BindOnce(&AdsServiceImpl::OnGetAdsHistory,
AsWeakPtr(), std::move(callback)));
bat_ads_->GetAdsHistory(from_timestamp, to_timestamp,
base::BindOnce(&AdsServiceImpl::OnGetAdsHistory, AsWeakPtr(),
std::move(callback)));
}

void AdsServiceImpl::ToggleAdThumbUp(
Expand Down Expand Up @@ -1032,77 +1035,64 @@ void AdsServiceImpl::OnGetAdsForCategories(

void AdsServiceImpl::OnGetAdsHistory(
OnGetAdsHistoryCallback callback,
const base::flat_map<uint64_t, std::vector<std::string>>& json) {
// Reconstitute the map of AdsHistory items from JSON
std::map<uint64_t, std::vector<ads::AdsHistory>> ads_history_map;
for (const auto& entry : json) {
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_vector.push_back(ads_history);
}
const uint64_t timestamp_in_seconds = entry.first;
ads_history_map[timestamp_in_seconds] = ads_history_vector;
}
const std::string& json) {
ads::AdsHistory ads_history;
ads_history.FromJson(json);

// Build the list structure required by the WebUI
base::ListValue ads_history_list;
// Build the list structure required by the webUI
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);
base::ListValue list;
for (const auto& entry : ads_history.entries) {
base::DictionaryValue ad_content_dictionary;
ad_content_dictionary.SetKey("uuid", base::Value(entry.ad_content.uuid));
ad_content_dictionary.SetKey("creativeSetId",
base::Value(entry.ad_content.creative_set_id));
ad_content_dictionary.SetKey("brand", base::Value(entry.ad_content.brand));
ad_content_dictionary.SetKey("brandInfo",
base::Value(entry.ad_content.brand_info));
ad_content_dictionary.SetKey("brandLogo",
base::Value(entry.ad_content.brand_logo));
ad_content_dictionary.SetKey("brandDisplayUrl",
base::Value(entry.ad_content.brand_display_url));
ad_content_dictionary.SetKey("brandUrl",
base::Value(entry.ad_content.brand_url));
ad_content_dictionary.SetKey("likeAction",
base::Value(entry.ad_content.like_action));
ad_content_dictionary.SetKey(
"adAction", base::Value(std::string(entry.ad_content.ad_action)));
ad_content_dictionary.SetKey("savedAd",
base::Value(entry.ad_content.saved_ad));
ad_content_dictionary.SetKey("flaggedAd",
base::Value(entry.ad_content.flagged_ad));

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

base::DictionaryValue ad_history_dictionary;
ad_history_dictionary.SetKey("id", base::Value(entry.uuid));
ad_history_dictionary.SetPath("adContent",
std::move(ad_content_dictionary));
ad_history_dictionary.SetPath("categoryContent",
std::move(category_content_dictionary));

base::DictionaryValue dictionary;

dictionary.SetKey("id", base::Value(std::to_string(id++)));
auto time = base::Time::FromDoubleT(entry.timestamp_in_seconds);
auto js_time = time.ToJsTime();
dictionary.SetKey("timestampInMilliseconds", base::Value(js_time));

base::ListValue ad_history_list;
ad_history_list.GetList().emplace_back(std::move(ad_history_dictionary));
dictionary.SetPath("adDetailRows", std::move(ad_history_list));

list.GetList().emplace_back(std::move(dictionary));
}

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

void AdsServiceImpl::OnRemoveAllHistory(
Expand Down
4 changes: 3 additions & 1 deletion components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class AdsServiceImpl : public AdsService,
const SessionID& tab_id) override;

void GetAdsHistory(
const uint64_t from_timestamp,
const uint64_t to_timestamp,
OnGetAdsHistoryCallback callback) override;

void ToggleAdThumbUp(
Expand Down Expand Up @@ -212,7 +214,7 @@ class AdsServiceImpl : public AdsService,

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

void OnRemoveAllHistory(
const int32_t result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const onAdsData = (adsData: Rewards.AdsData) => action(types.ON_ADS_DATA,

export const getAdsHistory = () => action(types.GET_ADS_HISTORY)

export const onAdsHistory = (adsHistory: Rewards.AdsHistoryData[]) => action(types.ON_ADS_HISTORY, {
export const onAdsHistory = (adsHistory: Rewards.AdsHistory[]) => action(types.ON_ADS_HISTORY, {
adsHistory
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ window.cr.define('brave_rewards', function () {
getActions().onAdsData(adsData)
}

function adsHistory (adsHistory: Rewards.AdsHistoryData[]) {
function adsHistory (adsHistory: Rewards.AdsHistory[]) {
getActions().onAdsHistory(adsHistory)
}

Expand Down
Loading

0 comments on commit 33f6998

Please sign in to comment.