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

Add support for Twitter tips #2195

Merged
merged 37 commits into from
May 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
8527f20
Add Rewards tip icon to all tweets via content script
emerick Apr 4, 2019
7e5687b
Show tipping banner when user clicks Tip button on a tweet
emerick Apr 9, 2019
29385bc
Refactor donations dialog to take a dictionary of parameters
emerick Apr 15, 2019
149ac70
Add DonateToTwitterUser component and small refactor to accomodate it
emerick Apr 15, 2019
95bafd6
Add SaveTwitterPublisherInfo and call when displaying twitter tip dialog
emerick Apr 19, 2019
e8d5a42
Moves logic into native ledger
NejcZdovc Apr 23, 2019
f8c05a0
Add tweetId to TweetMetaData and pass it to donate dialog
emerick Apr 24, 2019
35acf1e
Allow retweeting tip
emerick Apr 24, 2019
a5db4df
Include style info directly in content script
emerick Apr 24, 2019
3daca99
Pass tweet metadata to SiteBanner
emerick Apr 24, 2019
22dc33f
Add timestamp to TweetMetaData and refactor passing of data
emerick Apr 26, 2019
730bfa2
Add TweetBox component
emerick May 4, 2019
c9376d6
Remove isTwitterAccount
emerick May 4, 2019
02734ae
Use innerText to get tweet content, get timestamp in seconds
emerick May 4, 2019
a7a71aa
Pass screenName and other metadata to SiteBanner component
emerick May 7, 2019
ffca9a1
Update brave-ui DEPS to point to twitter-tips branch (temporary)
emerick May 7, 2019
b44b276
Adds twitter setting
NejcZdovc May 8, 2019
3095dfc
Donation -> Tip
emerick May 8, 2019
c67d795
Move TweetBox component to brave-ui
emerick May 9, 2019
0a4467e
Ensure intent_url is valid before using it
emerick May 10, 2019
f22a434
Don't include incognito info when getting tab
emerick May 13, 2019
8d18c41
Add callback to save visit
NejcZdovc May 14, 2019
977f3f7
Add new elements to shadow DOM
emerick May 14, 2019
42685d6
Correctly handle Twitter-based publishers
emerick May 14, 2019
80c7ecc
Handle second visit correctly
NejcZdovc May 15, 2019
4247d10
Account for refactoring of PublisherInfo data type
emerick May 16, 2019
faaa32a
Incorporate review feedback
emerick May 20, 2019
100220c
Allow tweeting any tip
emerick May 14, 2019
7df2a0b
Add browser tests for Twitter tips
emerick May 21, 2019
f592ec2
Fixes settings button being visible when rewards is off
NejcZdovc May 22, 2019
d3efb22
Do not show tweet out button for recurring
NejcZdovc May 22, 2019
e0cf857
Make timeout optional in TransientTipOverlay component
emerick May 22, 2019
e3b6501
Move GetTwitterShareURL into ledger
emerick May 22, 2019
f68b76b
Fix wording of Tweet button
emerick May 22, 2019
fce01cc
Move Twitter content script and make naming more Twitter-specific
emerick May 22, 2019
95e9eab
Avoid potential problems with timer and visibilitychange
emerick May 22, 2019
09729d5
Add button tooltip and hover state
emerick May 22, 2019
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
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