Skip to content

Commit

Permalink
Merge pull request #2195 from brave/twitter-tips
Browse files Browse the repository at this point in the history
Add support for Twitter tips
  • Loading branch information
NejcZdovc committed May 23, 2019
2 parents 74f3cfa + 09729d5 commit d79fd0d
Show file tree
Hide file tree
Showing 61 changed files with 1,816 additions and 199 deletions.
24 changes: 11 additions & 13 deletions browser/brave_rewards/tip_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/values.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "brave/common/webui_url_constants.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
Expand All @@ -37,7 +38,7 @@ constexpr int kDialogMaxHeight = 700;
class TipDialogDelegate : public ui::WebDialogDelegate {
public:
explicit TipDialogDelegate(WebContents* initiator,
std::string publisher_key);
std::unique_ptr<base::DictionaryValue> params);
~TipDialogDelegate() override;

ui::ModalType GetDialogModalType() const override;
Expand All @@ -53,15 +54,14 @@ class TipDialogDelegate : public ui::WebDialogDelegate {

private:
WebContents* initiator_;
const std::string publisher_key_;
std::unique_ptr<base::DictionaryValue> params_;

DISALLOW_COPY_AND_ASSIGN(TipDialogDelegate);
};

TipDialogDelegate::TipDialogDelegate(WebContents* initiator,
std::string publisher_key)
: initiator_(initiator),
publisher_key_(publisher_key) {
std::unique_ptr<base::DictionaryValue> params)
: initiator_(initiator), params_(std::move(params)) {
}

TipDialogDelegate::~TipDialogDelegate() {
Expand Down Expand Up @@ -111,11 +111,9 @@ void TipDialogDelegate::GetDialogSize(gfx::Size* size) const {
}

std::string TipDialogDelegate::GetDialogArgs() const {
std::string data;
base::DictionaryValue dialog_args;
dialog_args.SetString("publisherKey", publisher_key_);
base::JSONWriter::Write(dialog_args, &data);
return data;
std::string json;
base::JSONWriter::Write(*params_, &json);
return json;
}

void TipDialogDelegate::OnDialogClosed(
Expand All @@ -136,7 +134,7 @@ bool TipDialogDelegate::ShouldShowDialogTitle() const {
namespace brave_rewards {

void OpenTipDialog(WebContents* initiator,
const std::string& publisher_key) {
std::unique_ptr<base::DictionaryValue> params) {
content::WebContents* outermost_web_contents =
guest_view::GuestViewBase::GetTopLevelWebContents(initiator);
gfx::Size host_size = outermost_web_contents->GetContainerBounds().size();
Expand All @@ -147,7 +145,7 @@ void OpenTipDialog(WebContents* initiator,
// resize)
ShowConstrainedWebDialogWithAutoResize(
initiator->GetBrowserContext(),
std::make_unique<TipDialogDelegate>(initiator, publisher_key),
std::make_unique<TipDialogDelegate>(initiator, std::move(params)),
initiator, min_size, max_size);
}

Expand Down
7 changes: 6 additions & 1 deletion browser/brave_rewards/tip_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
#ifndef BRAVE_BROWSER_BRAVE_REWARDS_TIP_DIALOG_H_
#define BRAVE_BROWSER_BRAVE_REWARDS_TIP_DIALOG_H_

#include <memory>
#include <string>

namespace base {
class DictionaryValue;
}

namespace content {
class WebContents;
}

namespace brave_rewards {

void OpenTipDialog(content::WebContents* initiator,
const std::string& publisher_key);
std::unique_ptr<base::DictionaryValue> params);

}

Expand Down
128 changes: 121 additions & 7 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "brave/browser/extensions/api/brave_rewards_api.h"

#include <map>
#include <memory>
#include <string>
#include <utility>
Expand All @@ -13,19 +14,19 @@
#include "base/strings/string_number_conversions.h"
#include "brave/browser/brave_rewards/tip_dialog.h"
#include "brave/common/extensions/api/brave_rewards.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/browser/ads_service_factory.h"
#include "content/public/browser/web_contents.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/web_contents.h"

using brave_rewards::RewardsService;
using brave_rewards::RewardsServiceFactory;
using brave_ads::AdsService;
using brave_ads::AdsServiceFactory;
using brave_rewards::RewardsService;
using brave_rewards::RewardsServiceFactory;

namespace extensions {
namespace api {
Expand Down Expand Up @@ -62,19 +63,104 @@ ExtensionFunction::ResponseAction BraveRewardsTipSiteFunction::Run() {
if (!ExtensionTabUtil::GetTabById(
params->tab_id,
profile,
include_incognito_information(),
false,
nullptr,
nullptr,
&contents,
nullptr)) {
return RespondNow(Error(tabs_constants::kTabNotFoundError,
base::NumberToString(params->tab_id)));
}
::brave_rewards::OpenTipDialog(contents, params->publisher_key);

auto params_dict = std::make_unique<base::DictionaryValue>();
params_dict->SetString("publisherKey", params->publisher_key);
::brave_rewards::OpenTipDialog(contents, std::move(params_dict));

return RespondNow(NoArguments());
}

BraveRewardsTipTwitterUserFunction::BraveRewardsTipTwitterUserFunction()
: weak_factory_(this) {
}

BraveRewardsTipTwitterUserFunction::~BraveRewardsTipTwitterUserFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsTipTwitterUserFunction::Run() {
std::unique_ptr<brave_rewards::TipTwitterUser::Params> params(
brave_rewards::TipTwitterUser::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());

// Sanity check: don't allow tips in private / tor contexts,
// although the command should not have been enabled in the first place.
Profile* profile = Profile::FromBrowserContext(browser_context());
if (profile->IsOffTheRecord()) {
return RespondNow(
Error("Cannot tip Twitter user in a private context"));
}

auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);
if (rewards_service) {
AddRef();
std::map<std::string, std::string> args;
args["user_id"] = params->tweet_meta_data.user_id;
args["name"] = params->tweet_meta_data.name;
args["screen_name"] = params->tweet_meta_data.screen_name;
rewards_service->SaveTwitterPublisherInfo(
args,
base::Bind(&BraveRewardsTipTwitterUserFunction::
OnTwitterPublisherInfoSaved,
weak_factory_.GetWeakPtr()));
}

return RespondNow(NoArguments());
}

void BraveRewardsTipTwitterUserFunction::OnTwitterPublisherInfoSaved(
std::unique_ptr<::brave_rewards::ContentSite> publisher_info) {
std::unique_ptr<brave_rewards::TipTwitterUser::Params> params(
brave_rewards::TipTwitterUser::Params::Create(*args_));

if (!publisher_info) {
// TODO(nejczdovc): what should we do in this case?
Release();
return;
}

// Get web contents for this tab
content::WebContents* contents = nullptr;
if (!ExtensionTabUtil::GetTabById(
params->tab_id,
Profile::FromBrowserContext(browser_context()),
false,
nullptr,
nullptr,
&contents,
nullptr)) {
return;
}

auto params_dict = std::make_unique<base::DictionaryValue>();
params_dict->SetString("publisherKey", publisher_info->id);

auto tweet_meta_data_dict = std::make_unique<base::DictionaryValue>();
tweet_meta_data_dict->SetString("name", publisher_info->name);
tweet_meta_data_dict->SetString("screenName",
params->tweet_meta_data.screen_name);
tweet_meta_data_dict->SetString("userId", params->tweet_meta_data.user_id);
tweet_meta_data_dict->SetString("tweetId", params->tweet_meta_data.tweet_id);
tweet_meta_data_dict->SetInteger("tweetTimestamp",
params->tweet_meta_data.tweet_timestamp);
tweet_meta_data_dict->SetString("tweetText",
params->tweet_meta_data.tweet_text);
params_dict->SetDictionary("tweetMetaData", std::move(tweet_meta_data_dict));

::brave_rewards::OpenTipDialog(contents, std::move(params_dict));

Release();
}

BraveRewardsGetPublisherDataFunction::~BraveRewardsGetPublisherDataFunction() {
}

Expand Down Expand Up @@ -492,5 +578,33 @@ BraveRewardsGetAllNotificationsFunction::Run() {
return RespondNow(OneArgument(std::move(list)));
}

BraveRewardsGetInlineTipSettingFunction::
~BraveRewardsGetInlineTipSettingFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetInlineTipSettingFunction::Run() {
std::unique_ptr<brave_rewards::GetInlineTipSetting::Params> params(
brave_rewards::GetInlineTipSetting::Params::Create(*args_));

Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service =
RewardsServiceFactory::GetForProfile(profile);
if (!rewards_service) {
return RespondNow(OneArgument(std::make_unique<base::Value>(false)));
}

rewards_service->GetInlineTipSetting(
params->key,
base::BindOnce(
&BraveRewardsGetInlineTipSettingFunction::OnInlineTipSetting,
this));
return RespondLater();
}

void BraveRewardsGetInlineTipSettingFunction::OnInlineTipSetting(bool value) {
Respond(OneArgument(std::make_unique<base::Value>(value)));
}

} // namespace api
} // namespace extensions
34 changes: 33 additions & 1 deletion browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#include <memory>
#include <string>

#include "extensions/browser/extension_function.h"
#include "base/memory/weak_ptr.h"
#include "brave/components/brave_rewards/browser/content_site.h"
#include "brave/components/brave_rewards/browser/publisher_banner.h"
#include "extensions/browser/extension_function.h"

namespace extensions {
namespace api {
Expand All @@ -36,6 +37,23 @@ class BraveRewardsTipSiteFunction : public UIThreadExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsTipTwitterUserFunction
: public UIThreadExtensionFunction {
public:
BraveRewardsTipTwitterUserFunction();
DECLARE_EXTENSION_FUNCTION("braveRewards.tipTwitterUser", UNKNOWN)

protected:
~BraveRewardsTipTwitterUserFunction() override;

ResponseAction Run() override;

private:
base::WeakPtrFactory<BraveRewardsTipTwitterUserFunction> weak_factory_;
void OnTwitterPublisherInfoSaved(
std::unique_ptr<brave_rewards::ContentSite> publisher_info);
};

class BraveRewardsGetPublisherDataFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getPublisherData", UNKNOWN)
Expand Down Expand Up @@ -247,6 +265,20 @@ class BraveRewardsGetAllNotificationsFunction :
ResponseAction Run() override;
};

class BraveRewardsGetInlineTipSettingFunction :
public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getInlineTipSetting", UNKNOWN)

protected:
~BraveRewardsGetInlineTipSettingFunction() override;

ResponseAction Run() override;

private:
void OnInlineTipSetting(bool value);
};

} // namespace api
} // namespace extensions

Expand Down
17 changes: 17 additions & 0 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnGetOneTimeTips(
std::unique_ptr<brave_rewards::ContentSiteList> list);

void SetInlineTipSetting(const base::ListValue* args);

// RewardsServiceObserver implementation
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
uint32_t result) override;
Expand Down Expand Up @@ -292,6 +294,9 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getExcludedPublishersNumber",
base::BindRepeating(&RewardsDOMHandler::GetExcludedPublishersNumber,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.setInlineTipSetting",
base::BindRepeating(&RewardsDOMHandler::SetInlineTipSetting,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand Down Expand Up @@ -1079,6 +1084,18 @@ void RewardsDOMHandler::OnContributionSaved(
"brave_rewards.onContributionSaved", result);
}

void RewardsDOMHandler::SetInlineTipSetting(const base::ListValue* args) {
std::string key;
args->GetString(0, &key);

std::string value;
args->GetString(1, &value);

if (rewards_service_) {
rewards_service_->SetInlineTipSetting(key, value == "true");
}
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
Loading

0 comments on commit d79fd0d

Please sign in to comment.