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

Rewards Greaselion - Twitter #6671

Merged
merged 3 commits into from
Oct 5, 2020
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
194 changes: 116 additions & 78 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ BraveRewardsSavePublisherInfoFunction::Run() {
}

void BraveRewardsSavePublisherInfoFunction::OnSavePublisherInfo(
const ledger::type::Result result) {
const ledger::type::Result result) {
Respond(OneArgument(std::make_unique<base::Value>(static_cast<int>(result))));
}

Expand Down Expand Up @@ -282,45 +282,130 @@ ExtensionFunction::ResponseAction BraveRewardsTipSiteFunction::Run() {
return RespondNow(NoArguments());
}

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

BraveRewardsTipTwitterUserFunction::~BraveRewardsTipTwitterUserFunction() {
BraveRewardsTipUserFunction::~BraveRewardsTipUserFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsTipTwitterUserFunction::Run() {
std::unique_ptr<brave_rewards::TipTwitterUser::Params> params(
brave_rewards::TipTwitterUser::Params::Create(*args_));
ExtensionFunction::ResponseAction BraveRewardsTipUserFunction::Run() {
std::unique_ptr<brave_rewards::TipUser::Params> params(
brave_rewards::TipUser::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.
if (!brave::IsRegularProfile(browser_context())) {
return RespondNow(
Error("Cannot tip Twitter user in a private context"));
return RespondNow(Error("Cannot tip user in a private context"));
}

Profile* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);
if (rewards_service) {
AddRef();
std::map<std::string, std::string> args;
args["user_id"] = params->media_meta_data.user_id;
args["twitter_name"] = params->media_meta_data.twitter_name;
args["screen_name"] = params->media_meta_data.screen_name;
rewards_service->SaveInlineMediaInfo(
params->media_meta_data.media_type,
args,
base::Bind(&BraveRewardsTipTwitterUserFunction::
OnTwitterPublisherInfoSaved,
weak_factory_.GetWeakPtr()));
if (!rewards_service) {
return RespondNow(Error("Rewards service is not initialized"));
}

AddRef();

rewards_service->GetPublisherInfo(
params->publisher_key,
base::Bind(
&BraveRewardsTipUserFunction::OnTipUserGetPublisherInfo,
this));

return RespondNow(NoArguments());
}

void BraveRewardsTipUserFunction::OnTipUserGetPublisherInfo(
const ledger::type::Result result,
ledger::type::PublisherInfoPtr info) {
if (result != ledger::type::Result::LEDGER_OK &&
result != ledger::type::Result::NOT_FOUND) {
Release();
return;
}

if (result == ledger::type::Result::LEDGER_OK) {
ShowTipDialog();
Release();
return;
}

std::unique_ptr<brave_rewards::TipUser::Params> params(
brave_rewards::TipUser::Params::Create(*args_));

auto publisher_info = ledger::type::PublisherInfo::New();
publisher_info->id = params->publisher_key;
publisher_info->name = params->publisher_name;
publisher_info->url = params->url;
publisher_info->provider = params->media_type;
publisher_info->favicon_url = params->fav_icon_url;

Profile* profile = Profile::FromBrowserContext(browser_context());
auto* rewards_service = RewardsServiceFactory::GetForProfile(profile);
if (!rewards_service) {
Release();
return;
}

rewards_service->SavePublisherInfo(
0,
std::move(publisher_info),
base::Bind(&BraveRewardsTipUserFunction::
OnTipUserSavePublisherInfo,
weak_factory_.GetWeakPtr()));
}

void BraveRewardsTipUserFunction::OnTipUserSavePublisherInfo(
const ledger::type::Result result) {
if (result != ledger::type::Result::LEDGER_OK) {
Release();
return;
}

ShowTipDialog();
Release();
}

void BraveRewardsTipUserFunction::ShowTipDialog() {
std::unique_ptr<brave_rewards::TipUser::Params> params(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible that this is null? if so please add null check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

brave_rewards::TipUser::Params::Create(*args_));
if (!params) {
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)) {
Release();
return;
}

base::Value media_meta_data_dict(base::Value::Type::DICTIONARY);
media_meta_data_dict.SetStringKey("mediaType", params->media_type);
media_meta_data_dict.SetStringKey("publisherKey", params->publisher_key);
media_meta_data_dict.SetStringKey("publisherName", params->publisher_name);
media_meta_data_dict.SetStringKey("postId", params->post_id);
media_meta_data_dict.SetStringKey("postTimestamp", params->post_timestamp);
media_meta_data_dict.SetStringKey("postText", params->post_text);

auto params_dict = std::make_unique<base::DictionaryValue>();
params_dict->SetString("publisherKey", params->publisher_key);
params_dict->SetString("url", params->url);
params_dict->SetPath("mediaMetaData", std::move(media_meta_data_dict));

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

BraveRewardsTipRedditUserFunction::BraveRewardsTipRedditUserFunction()
: weak_factory_(this) {
}
Expand Down Expand Up @@ -371,14 +456,15 @@ void BraveRewardsTipRedditUserFunction::OnRedditPublisherInfoSaved(

content::WebContents* contents = nullptr;
if (!ExtensionTabUtil::GetTabById(
params->tab_id,
Profile::FromBrowserContext(browser_context()),
false,
nullptr,
nullptr,
&contents,
nullptr)) {
return;
params->tab_id,
Profile::FromBrowserContext(browser_context()),
false,
nullptr,
nullptr,
&contents,
nullptr)) {
Release();
return;
}

std::unique_ptr<base::DictionaryValue> params_dict =
Expand All @@ -405,54 +491,6 @@ void BraveRewardsTipRedditUserFunction::OnRedditPublisherInfoSaved(
Release();
}

void BraveRewardsTipTwitterUserFunction::OnTwitterPublisherInfoSaved(
ledger::type::PublisherInfoPtr publisher) {
std::unique_ptr<brave_rewards::TipTwitterUser::Params> params(
brave_rewards::TipTwitterUser::Params::Create(*args_));

if (!publisher) {
// 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->id);
params_dict->SetString("url", publisher->url);

base::Value media_meta_data_dict(base::Value::Type::DICTIONARY);
media_meta_data_dict.SetStringKey("twitter_name", publisher->name);
media_meta_data_dict.SetStringKey("mediaType",
params->media_meta_data.media_type);
media_meta_data_dict.SetStringKey("screenName",
params->media_meta_data.screen_name);
media_meta_data_dict.SetStringKey("userId", params->media_meta_data.user_id);
media_meta_data_dict.SetStringKey("tweetId",
params->media_meta_data.tweet_id);
media_meta_data_dict.SetDoubleKey("tweetTimestamp",
params->media_meta_data.tweet_timestamp);
media_meta_data_dict.SetStringKey("tweetText",
params->media_meta_data.tweet_text);
params_dict->SetPath("mediaMetaData", std::move(media_meta_data_dict));

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

Release();
}
///////////////////////////////////////////////////
BraveRewardsTipGitHubUserFunction::BraveRewardsTipGitHubUserFunction()
: weak_factory_(this) {
}
Expand Down Expand Up @@ -515,6 +553,7 @@ void BraveRewardsTipGitHubUserFunction::OnGitHubPublisherInfoSaved(
nullptr,
&contents,
nullptr)) {
Release();
return;
}

Expand All @@ -535,7 +574,6 @@ void BraveRewardsTipGitHubUserFunction::OnGitHubPublisherInfoSaved(

Release();
}
//////////////////

BraveRewardsGetPublisherDataFunction::~BraveRewardsGetPublisherDataFunction() {
}
Expand Down
18 changes: 11 additions & 7 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,24 @@ class BraveRewardsTipSiteFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveRewardsTipTwitterUserFunction
: public ExtensionFunction {
class BraveRewardsTipUserFunction : public ExtensionFunction {
public:
BraveRewardsTipTwitterUserFunction();
DECLARE_EXTENSION_FUNCTION("braveRewards.tipTwitterUser", UNKNOWN)
BraveRewardsTipUserFunction();
DECLARE_EXTENSION_FUNCTION("braveRewards.tipUser", UNKNOWN)

protected:
~BraveRewardsTipTwitterUserFunction() override;
~BraveRewardsTipUserFunction() override;

ResponseAction Run() override;

private:
base::WeakPtrFactory<BraveRewardsTipTwitterUserFunction> weak_factory_;
void OnTwitterPublisherInfoSaved(ledger::type::PublisherInfoPtr publisher);
void OnTipUserGetPublisherInfo(
const ledger::type::Result result,
ledger::type::PublisherInfoPtr info);
void OnTipUserSavePublisherInfo(const ledger::type::Result result);
void ShowTipDialog();

base::WeakPtrFactory<BraveRewardsTipUserFunction> weak_factory_;
};

class BraveRewardsTipGitHubUserFunction : public ExtensionFunction {
Expand Down
8 changes: 4 additions & 4 deletions browser/ui/webui/brave_tip_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class RewardsTipDOMHandler : public WebUIMessageHandler,

void OnPublisherBanner(ledger::type::PublisherBannerPtr banner);

void OnTwitterShareURL(const std::string& url);
void OnGetShareURL(const std::string& url);

void FetchBalance(const base::ListValue* args);
void OnFetchBalance(
Expand Down Expand Up @@ -376,12 +376,12 @@ void RewardsTipDOMHandler::TweetTip(const base::ListValue *args) {
share_url_args["name"] = name.erase(0, 1);
share_url_args["tweet_id"] = tweet_id;
rewards_service_->GetShareURL(
"twitter", share_url_args,
base::BindOnce(&RewardsTipDOMHandler::OnTwitterShareURL,
share_url_args,
base::BindOnce(&RewardsTipDOMHandler::OnGetShareURL,
base::Unretained(this)));
}

void RewardsTipDOMHandler::OnTwitterShareURL(const std::string& url) {
void RewardsTipDOMHandler::OnGetShareURL(const std::string& url) {
GURL gurl(url);
if (!gurl.is_valid())
return;
Expand Down
70 changes: 34 additions & 36 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -565,47 +565,45 @@
]
},
{
"name": "tipTwitterUser",
"name": "tipUser",
"type": "function",
"description": "Allow the user to perform a donation to a Twitter user",
"description": "Allow the user to tip a user on a particular site",
"parameters": [
{
"name": "tabID",
"type": "integer"
"name": "tabId",
"type": "number"
},
{
"name": "mediaMetaData",
"type": "object",
"properties": {
"mediaType": {
"type": "string",
"description": "description used in UI to identify the media type"
},
"userId": {
"type": "string",
"description": "User ID of tweet author"
},
"twitterName": {
"type": "string",
"description": "Name of tweet author"
},
"screenName": {
"type": "string",
"description": "Twitter handle of tweet author"
},
"tweetId": {
"type": "string",
"description": "Unique tweet ID"
},
"tweetTimestamp": {
"type": "number",
"description": "Timestamp (in milliseconds) of tweet"
},
"tweetText": {
"type": "string",
"description": "Textual contents of tweet"
}
}
"name": "mediaType",
"type": "string"
},
{
"name": "url",
"type": "string"
},
{
"name": "publisherKey",
"type": "string"
},
{
"name": "publisherName",
"type": "string"
},
{
"name": "favIconUrl",
"type": "string"
},
{
"name": "postId",
"type": "string"
},
{
"name": "postTimestamp",
"type": "string"
},
{
"name": "postText",
"type": "string"
}
]
},
Expand Down
Loading