Skip to content

Commit

Permalink
Inline Tipping support for GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-browser-releases authored and jdkuki committed Aug 2, 2019
1 parent 0dc4b5e commit 081bf56
Show file tree
Hide file tree
Showing 26 changed files with 827 additions and 44 deletions.
105 changes: 95 additions & 10 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,50 @@ ExtensionFunction::ResponseAction BraveRewardsTipRedditUserFunction::Run() {
return RespondNow(NoArguments());
}

void BraveRewardsTipRedditUserFunction::OnRedditPublisherInfoSaved(
std::unique_ptr<::brave_rewards::ContentSite> publisher_info) {
std::unique_ptr<brave_rewards::TipRedditUser::Params> params(
brave_rewards::TipRedditUser::Params::Create(*args_));

if (!publisher_info) {
Release();
return;
}

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

std::unique_ptr<base::DictionaryValue> params_dict =
std::make_unique<base::DictionaryValue>();
params_dict->SetStringKey("publisherKey", publisher_info->id);
params_dict->SetStringKey("url", publisher_info->url);

base::Value media_meta_data_dict(base::Value::Type::DICTIONARY);
media_meta_data_dict.SetStringKey("name", publisher_info->name);
media_meta_data_dict.SetStringKey(
"userName", params->media_meta_data.user_name);
media_meta_data_dict.SetStringKey(
"postText", params->media_meta_data.post_text);
media_meta_data_dict.SetStringKey(
"postRelDate", params->media_meta_data.post_rel_date);
params_dict->SetPath(
"mediaMetaData", std::move(media_meta_data_dict));

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

Release();
}

void BraveRewardsTipTwitterUserFunction::OnTwitterPublisherInfoSaved(
std::unique_ptr<::brave_rewards::ContentSite> publisher_info) {
std::unique_ptr<brave_rewards::TipTwitterUser::Params> params(
Expand Down Expand Up @@ -205,17 +249,60 @@ void BraveRewardsTipTwitterUserFunction::OnTwitterPublisherInfoSaved(

Release();
}
///////////////////////////////////////////////////
BraveRewardsTipGitHubUserFunction::BraveRewardsTipGitHubUserFunction()
: weak_factory_(this) {
}

void BraveRewardsTipRedditUserFunction::OnRedditPublisherInfoSaved(
BraveRewardsTipGitHubUserFunction::~BraveRewardsTipGitHubUserFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsTipGitHubUserFunction::Run() {
std::unique_ptr<brave_rewards::TipGitHubUser::Params> params(
brave_rewards::TipGitHubUser::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_name"] = params->media_meta_data.user_name;
if (args["user_name"].empty()) {
LOG(ERROR) << "Cannot tip user without username";
} else {
rewards_service->SaveInlineMediaInfo(
params->media_meta_data.media_type,
args,
base::Bind(&BraveRewardsTipGitHubUserFunction::
OnGitHubPublisherInfoSaved,
weak_factory_.GetWeakPtr()));
}
}
return RespondNow(NoArguments());
}


void BraveRewardsTipGitHubUserFunction::OnGitHubPublisherInfoSaved(
std::unique_ptr<::brave_rewards::ContentSite> publisher_info) {
std::unique_ptr<brave_rewards::TipRedditUser::Params> params(
brave_rewards::TipRedditUser::Params::Create(*args_));
std::unique_ptr<brave_rewards::TipGitHubUser::Params> params(
brave_rewards::TipGitHubUser::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,
Expand All @@ -235,19 +322,17 @@ void BraveRewardsTipRedditUserFunction::OnRedditPublisherInfoSaved(
base::Value media_meta_data_dict(base::Value::Type::DICTIONARY);
media_meta_data_dict.SetStringKey("mediaType",
params->media_meta_data.media_type);
media_meta_data_dict.SetStringKey("name", publisher_info->name);
media_meta_data_dict.SetStringKey("userName",
params->media_meta_data.user_name);
media_meta_data_dict.SetStringKey("postText",
params->media_meta_data.post_text);
media_meta_data_dict.SetStringKey("postRelDate",
params->media_meta_data.post_rel_date);
params_dict->SetPath("mediaMetaData", std::move(media_meta_data_dict));
params_dict->SetPath("mediaMetaData",
std::move(media_meta_data_dict));

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

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

BraveRewardsGetPublisherDataFunction::~BraveRewardsGetPublisherDataFunction() {
}
Expand Down
17 changes: 16 additions & 1 deletion browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ class BraveRewardsTipTwitterUserFunction
std::unique_ptr<brave_rewards::ContentSite> publisher_info);
};

class BraveRewardsTipGitHubUserFunction
: public UIThreadExtensionFunction {
public:
BraveRewardsTipGitHubUserFunction();
DECLARE_EXTENSION_FUNCTION("braveRewards.tipGitHubUser", UNKNOWN)

protected:
~BraveRewardsTipGitHubUserFunction() override;

ResponseAction Run() override;
private:
base::WeakPtrFactory<BraveRewardsTipGitHubUserFunction> weak_factory_;
void OnGitHubPublisherInfoSaved(
std::unique_ptr<brave_rewards::ContentSite> publisher_info);
};

class BraveRewardsTipRedditUserFunction : public UIThreadExtensionFunction {
public:
BraveRewardsTipRedditUserFunction();
Expand All @@ -65,7 +81,6 @@ class BraveRewardsTipRedditUserFunction : public UIThreadExtensionFunction {
~BraveRewardsTipRedditUserFunction() override;

ResponseAction Run() override;

private:
base::WeakPtrFactory<BraveRewardsTipRedditUserFunction> weak_factory_;
void OnRedditPublisherInfoSaved(
Expand Down
3 changes: 3 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "donationAbilityYT", IDS_BRAVE_REWARDS_LOCAL_DONAT_ABILITY_YT },
{ "donationAbilityReddit", IDS_BRAVE_REWARDS_LOCAL_DONAT_ABILITY_REDT},
{ "donationAbilityTwitter", IDS_BRAVE_REWARDS_LOCAL_DONAT_ABILITY_TW },
{ "donationAbilityGitHub", IDS_BRAVE_REWARDS_LOCAL_DONAT_ABILITY_GH },
{ "donationDisabledText1", IDS_BRAVE_REWARDS_LOCAL_DONAT_DISABLED_TEXT1 }, // NOLINT
{ "donationDisabledText2", IDS_BRAVE_REWARDS_LOCAL_DONAT_DISABLED_TEXT2 }, // NOLINT
{ "donationNextDate", IDS_BRAVE_REWARDS_LOCAL_DONAT_NEXT_DATE },
Expand Down Expand Up @@ -616,6 +617,8 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "monthlyText", IDS_BRAVE_UI_MONTHLY_TEXT },
{ "redditTipTitle", IDS_BRAVE_UI_REDDIT_TIP_TITLE },
{ "redditTipTitleEmpty", IDS_BRAVE_UI_REDDIT_TIP_TITLE_EMPTY },
{ "githubTipTitle", IDS_BRAVE_UI_GITHUB_TIP_TITLE },
{ "githubTipTitleEmpty", IDS_BRAVE_UI_GITHUB_TIP_TITLE_EMPTY },
{ "rewardsBannerText1", IDS_BRAVE_UI_REWARDS_BANNER_TEXT1 },
{ "rewardsBannerText2", IDS_BRAVE_UI_REWARDS_BANNER_TEXT2 },
{ "sendDonation", IDS_BRAVE_UI_SEND_DONATION },
Expand Down
25 changes: 25 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,31 @@
}
]
},
{
"name": "tipGitHubUser",
"type": "function",
"description": "Allow the user to perform a donation to a GitHub user",
"parameters": [
{
"name": "tabID",
"type": "integer"
},
{
"name": "mediaMetaData",
"type": "object",
"properties": {
"mediaType": {
"type": "string",
"description": "description used in UI to identify the media type"
},
"userName": {
"type": "string",
"description": "Username of the GitHub author"
}
}
}
]
},
{
"name": "getPublisherData",
"type": "function",
Expand Down
56 changes: 56 additions & 0 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
" </div>"
" </body>"
"</html>");
} else if (request.relative_url == "/github") {
http_response->set_content(
"<html>"
" <head></head>"
" <body>"
" <div class='timeline-comment-actions'>"
" <div>GitHubCommentReactsButton</div>"
" <div>GitHubCommentElipsesButton</div>"
" </div>"
" </body>"
"</html>");
} else {
http_response->set_content(
"<html>"
Expand Down Expand Up @@ -1923,6 +1934,51 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
EXPECT_FALSE(IsMediaTipsInjected());
}

// Brave tip icon is injected when visiting GitHub
IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, GitHubTipsInjectedOnGitHub) {
// Enable Rewards
EnableRewards();

// Navigate to GitHub in a new tab
GURL url = https_server()->GetURL("github.com", "/github");
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);

// Ensure that Media Tips injection is active
EXPECT_TRUE(IsMediaTipsInjected());
}

// Brave tip icon is not injected when visiting GitHub while Brave
// Rewards is disabled
IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
GitHubTipsNotInjectedWhenRewardsDisabled) {
// Navigate to GitHub in a new tab
GURL url = https_server()->GetURL("github.com", "/github");
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);

// Ensure that Media Tips injection is not active
EXPECT_FALSE(IsMediaTipsInjected());
}

// Brave tip icon is not injected when not visiting GitHub
IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
GitHubTipsNotInjectedOnNonGitHub) {
// Enable Rewards
EnableRewards();

// Navigate to GitHub in a new tab
GURL url = https_server()->GetURL("brave.com", "/github");
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);

// Ensure that Media Tips injection is not active
EXPECT_FALSE(IsMediaTipsInjected());
}

// Check pending contributions
IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest,
PendingContributionTip) {
Expand Down
1 change: 0 additions & 1 deletion components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,6 @@ class RewardsServiceImpl : public RewardsService,
SaveMediaInfoCallback callback,
const ledger::Result result,
ledger::PublisherInfoPtr publisher);

void OnContributeUnverifiedPublishers(
ledger::Result result,
const std::string& publisher_key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ transpile_web_ui("brave_rewards_panel") {
["brave_rewards_panel", rebase_path("brave_rewards_panel.tsx")],
["brave_rewards_panel_background", rebase_path("background.ts")],
["brave_rewards_panel_content_twitter", rebase_path("content_scripts/twitter.ts")],
["brave_rewards_panel_content_reddit", rebase_path("content_scripts/reddit.ts")]
["brave_rewards_panel_content_reddit", rebase_path("content_scripts/reddit.ts")],
["brave_rewards_panel_content_github", rebase_path("content_scripts/github.ts")]
]

resource_name = "brave_rewards_panel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@
"message": "Tip",
"description": "Icon label for Reddit tips"
},
"githubTipsHoverText": {
"message": "Send a Tip",
"description": "Hover text for GitHub tips action"
},
"githubTipsIconLabel": {
"message": "Tip",
"description": "Icon label for GitHub tips"
},
"verifiedPublisherNotification": {
"message": "Creator $name$ recently verified",
"description": "Notification text that tells user which publisher just verified",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ const tipTwitterMedia = (mediaMetaData: RewardsTip.MediaMetaData) => {
})
}

const tipGitHubMedia = (mediaMetaData: RewardsTip.MediaMetaData) => {
mediaMetaData.mediaType = 'github'
chrome.tabs.query({
active: true,
windowId: chrome.windows.WINDOW_ID_CURRENT
}, (tabs) => {
if (!tabs || tabs.length === 0) {
return
}
const tabId = tabs[0].id
if (tabId === undefined) {
return
}
chrome.braveRewards.tipGitHubUser(tabId, mediaMetaData)
})
}

const tipRedditMedia = (mediaMetaData: RewardsTip.MediaMetaData) => {
mediaMetaData.mediaType = 'reddit'
chrome.tabs.query({
Expand Down Expand Up @@ -125,6 +142,9 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
case 'reddit':
tipRedditMedia(msg.mediaMetaData)
break
case 'github':
tipGitHubMedia(msg.mediaMetaData)
break
}
return false
}
Expand Down
Loading

0 comments on commit 081bf56

Please sign in to comment.