Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2959 from csadilek/2940
Browse files Browse the repository at this point in the history
feat(systemaddon): Add HTTP Referrer to Top Stories
  • Loading branch information
csadilek committed Jul 25, 2017
2 parents 729c47c + ff2491f commit 01db044
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 38 deletions.
1 change: 1 addition & 0 deletions system-addon/common/Actions.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ for (const type of [
"NEW_TAB_LOAD",
"NEW_TAB_UNLOAD",
"NEW_TAB_VISIBLE",
"OPEN_LINK",
"OPEN_NEW_WINDOW",
"OPEN_PRIVATE_WINDOW",
"PINNED_SITES_UPDATED",
Expand Down
8 changes: 7 additions & 1 deletion system-addon/content-src/components/Card/Card.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const LinkMenu = require("content-src/components/LinkMenu/LinkMenu");
const shortURL = require("content-src/lib/short-url");
const {FormattedMessage} = require("react-intl");
const cardContextTypes = require("./types");
const {actionCreators: ac, actionTypes: at} = require("common/Actions.jsm");

/**
* Card component.
Expand All @@ -19,6 +20,7 @@ class Card extends React.Component {
this.state = {showContextMenu: false, activeCard: null};
this.onMenuButtonClick = this.onMenuButtonClick.bind(this);
this.onMenuUpdate = this.onMenuUpdate.bind(this);
this.onLinkClick = this.onLinkClick.bind(this);
}
onMenuButtonClick(event) {
event.preventDefault();
Expand All @@ -27,6 +29,10 @@ class Card extends React.Component {
showContextMenu: true
});
}
onLinkClick(event) {
event.preventDefault();
this.props.dispatch(ac.SendToMain({type: at.OPEN_LINK, data: this.props.link}));
}
onMenuUpdate(showContextMenu) {
this.setState({showContextMenu});
}
Expand All @@ -37,7 +43,7 @@ class Card extends React.Component {
const {icon, intlID} = cardContextTypes[link.type];

return (<li className={`card-outer${isContextMenuOpen ? " active" : ""}`}>
<a href={link.url}>
<a href={link.url} onClick={this.onLinkClick}>
<div className="card">
{link.image && <div className="card-preview-image" style={{backgroundImage: `url(${link.image})`}} />}
<div className="card-details">
Expand Down
4 changes: 2 additions & 2 deletions system-addon/content-src/lib/link-menu-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
icon: "new-window",
action: ac.SendToMain({
type: at.OPEN_NEW_WINDOW,
data: {url: site.url}
data: {url: site.url, referrer: site.referrer}
}),
userEvent: "OPEN_NEW_WINDOW"
}),
Expand All @@ -40,7 +40,7 @@ module.exports = {
icon: "new-window-private",
action: ac.SendToMain({
type: at.OPEN_PRIVATE_WINDOW,
data: {url: site.url}
data: {url: site.url, referrer: site.referrer}
}),
userEvent: "OPEN_PRIVATE_WINDOW"
}),
Expand Down
1 change: 1 addition & 0 deletions system-addon/lib/ActivityStream.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const PREFS_CONFIG = new Map([
title: "Configuration options for top stories feed",
value: `{
"stories_endpoint": "https://getpocket.com/v3/firefox/global-recs?consumer_key=$apiKey",
"stories_referrer": "https://getpocket.com/recommendations",
"topics_endpoint": "https://getpocket.com/v3/firefox/trending-topics?consumer_key=$apiKey",
"read_more_endpoint": "https://getpocket.com/explore/trending?src=ff_new_tab",
"learn_more_endpoint": "https://getpocket.com/firefox_learnmore?src=ff_newtab",
Expand Down
22 changes: 22 additions & 0 deletions system-addon/lib/PlacesFeed.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ class PlacesFeed {
}
}

openNewWindow(action, isPrivate = false) {
const win = action._target.browser.ownerGlobal;
const privateParam = {private: isPrivate};
const params = (action.data.referrer) ?
Object.assign(privateParam, {referrerURI: Services.io.newURI(action.data.referrer)}) : privateParam;
win.openLinkIn(action.data.url, "window", params);
}

onAction(action) {
switch (action.type) {
case at.INIT:
Expand All @@ -207,9 +215,23 @@ class PlacesFeed {
case at.DELETE_HISTORY_URL:
NewTabUtils.activityStreamLinks.deleteHistoryEntry(action.data);
break;
case at.OPEN_NEW_WINDOW:
this.openNewWindow(action);
break;
case at.OPEN_PRIVATE_WINDOW:
this.openNewWindow(action, true);
break;
case at.SAVE_TO_POCKET:
Pocket.savePage(action._target.browser, action.data.site.url, action.data.site.title);
break;
case at.OPEN_LINK: {
if (action.data.referrer) {
action._target.browser.loadURI(action.data.url, Services.io.newURI(action.data.referrer));
} else {
action._target.browser.loadURI(action.data.url);
}
break;
}
}
}
}
Expand Down
10 changes: 0 additions & 10 deletions system-addon/lib/TopSitesFeed.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ this.TopSitesFeed = class TopSitesFeed {
}
this.lastUpdated = Date.now();
}
openNewWindow(action, isPrivate = false) {
const win = action._target.browser.ownerGlobal;
win.openLinkIn(action.data.url, "window", {private: isPrivate});
}
_getPinnedWithData() {
// Augment the pinned links with any other extra data we have for them already in the store
const links = this.store.getState().TopSites.rows;
Expand Down Expand Up @@ -134,12 +130,6 @@ this.TopSitesFeed = class TopSitesFeed {
this.refresh(action.meta.fromTarget);
}
break;
case at.OPEN_NEW_WINDOW:
this.openNewWindow(action);
break;
case at.OPEN_PRIVATE_WINDOW:
this.openNewWindow(action, true);
break;
case at.PLACES_HISTORY_CLEARED:
this.refresh();
break;
Expand Down
2 changes: 2 additions & 0 deletions system-addon/lib/TopStoriesFeed.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ this.TopStoriesFeed = class TopStoriesFeed {
this.stories_endpoint = this._produceUrlWithApiKey(options.stories_endpoint, apiKey);
this.topics_endpoint = this._produceUrlWithApiKey(options.topics_endpoint, apiKey);
this.read_more_endpoint = options.read_more_endpoint;
this.stories_referrer = options.stories_referrer;

// TODO https://github.com/mozilla/activity-stream/issues/2902
const sectionOptions = {
Expand Down Expand Up @@ -85,6 +86,7 @@ this.TopStoriesFeed = class TopStoriesFeed {
"title": s.title,
"description": s.excerpt,
"image": this._normalizeUrl(s.image_src),
"referrer": this.stories_referrer,
"url": s.dedupe_url
}));
return items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ describe("<LinkMenu>", () => {
describe(".onClick", () => {
const FAKE_INDEX = 3;
const FAKE_SOURCE = "TOP_SITES";
const FAKE_SITE = {url: "https://foo.com", title: "bar", bookmarkGuid: 1234};
const FAKE_SITE = {url: "https://foo.com", referrer: "https://foo.com/ref", title: "bar", bookmarkGuid: 1234};
const dispatch = sinon.stub();
const propOptions = ["Separator", "RemoveBookmark", "AddBookmark", "OpenInNewWindow", "OpenInPrivateWindow", "BlockUrl", "DeleteUrl", "PinTopSite", "UnpinTopSite", "SaveToPocket"];
const expectedActionData = {
menu_action_remove_bookmark: FAKE_SITE.bookmarkGuid,
menu_action_bookmark: FAKE_SITE.url,
menu_action_open_new_window: {url: FAKE_SITE.url},
menu_action_open_private_window: {url: FAKE_SITE.url},
menu_action_open_new_window: {url: FAKE_SITE.url, referrer: FAKE_SITE.referrer},
menu_action_open_private_window: {url: FAKE_SITE.url, referrer: FAKE_SITE.referrer},
menu_action_dismiss: FAKE_SITE.url,
menu_action_delete: FAKE_SITE.url,
menu_action_pin: {site: {url: FAKE_SITE.url, title: shortURL(FAKE_SITE)}, index: FAKE_INDEX},
Expand Down
43 changes: 43 additions & 0 deletions system-addon/test/unit/lib/PlacesFeed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,49 @@ describe("PlacesFeed", () => {
feed.onAction({type: at.DELETE_HISTORY_URL, data: "guava.com"});
assert.calledWith(global.NewTabUtils.activityStreamLinks.deleteHistoryEntry, "guava.com");
});
it("should call openNewWindow with the correct url on OPEN_NEW_WINDOW", () => {
sinon.stub(feed, "openNewWindow");
const openWindowAction = {type: at.OPEN_NEW_WINDOW, data: {url: "foo.com"}};
feed.onAction(openWindowAction);
assert.calledWith(feed.openNewWindow, openWindowAction);
});
it("should call openNewWindow with the correct url and privacy args on OPEN_PRIVATE_WINDOW", () => {
sinon.stub(feed, "openNewWindow");
const openWindowAction = {type: at.OPEN_PRIVATE_WINDOW, data: {url: "foo.com"}};
feed.onAction(openWindowAction);
assert.calledWith(feed.openNewWindow, openWindowAction, true);
});
it("should call openNewWindow with the correct url on OPEN_NEW_WINDOW", () => {
const openWindowAction = {
type: at.OPEN_NEW_WINDOW,
data: {url: "foo.com"},
_target: {browser: {ownerGlobal: {openLinkIn: () => {}}}}
};
sinon.stub(openWindowAction._target.browser.ownerGlobal, "openLinkIn");
feed.onAction(openWindowAction);
assert.calledOnce(openWindowAction._target.browser.ownerGlobal.openLinkIn);
});
it("should open link on OPEN_LINK", () => {
sinon.stub(feed, "openNewWindow");
const openLinkAction = {
type: at.OPEN_LINK,
data: {url: "foo.com"},
_target: {browser: {loadURI: sinon.spy()}}
};
feed.onAction(openLinkAction);
assert.calledWith(openLinkAction._target.browser.loadURI, openLinkAction.data.url);
});
it("should open link with referrer on OPEN_LINK", () => {
globals.set("Services", {io: {newURI: url => `URI:${url}`}});
sinon.stub(feed, "openNewWindow");
const openLinkAction = {
type: at.OPEN_LINK,
data: {url: "foo.com", referrer: "foo.com/ref"},
_target: {browser: {loadURI: sinon.spy()}}
};
feed.onAction(openLinkAction);
assert.calledWith(openLinkAction._target.browser.loadURI, openLinkAction.data.url, `URI:${openLinkAction.data.referrer}`);
});
it("should save to Pocket on SAVE_TO_POCKET", () => {
feed.onAction({type: at.SAVE_TO_POCKET, data: {site: {url: "raspberry.com", title: "raspberry"}}, _target: {browser: {}}});
assert.calledWith(global.Pocket.savePage, {}, "raspberry.com", "raspberry");
Expand Down
22 changes: 0 additions & 22 deletions system-addon/test/unit/lib/TopSitesFeed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,28 +165,6 @@ describe("Top Sites Feed", () => {
feed.onAction(newTabAction);
assert.notCalled(feed.refresh);
});
it("should call openNewWindow with the correct url on OPEN_NEW_WINDOW", () => {
sinon.stub(feed, "openNewWindow");
const openWindowAction = {type: at.OPEN_NEW_WINDOW, data: {url: "foo.com"}};
feed.onAction(openWindowAction);
assert.calledWith(feed.openNewWindow, openWindowAction);
});
it("should call openNewWindow with the correct url and privacy args on OPEN_PRIVATE_WINDOW", () => {
sinon.stub(feed, "openNewWindow");
const openWindowAction = {type: at.OPEN_PRIVATE_WINDOW, data: {url: "foo.com"}};
feed.onAction(openWindowAction);
assert.calledWith(feed.openNewWindow, openWindowAction, true);
});
it("should call openNewWindow with the correct url on OPEN_NEW_WINDOW", () => {
const openWindowAction = {
type: at.OPEN_NEW_WINDOW,
data: {url: "foo.com"},
_target: {browser: {ownerGlobal: {openLinkIn: () => {}}}}
};
sinon.stub(openWindowAction._target.browser.ownerGlobal, "openLinkIn");
feed.onAction(openWindowAction);
assert.calledOnce(openWindowAction._target.browser.ownerGlobal.openLinkIn);
});
it("should call with correct parameters on TOP_SITES_PIN", () => {
const pinAction = {
type: at.TOP_SITES_PIN,
Expand Down
4 changes: 4 additions & 0 deletions system-addon/test/unit/lib/TopStoriesFeed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe("Top Stories Feed", () => {
beforeEach(() => {
FakePrefs.prototype.prefs["feeds.section.topstories.options"] = `{
"stories_endpoint": "https://somedomain.org/stories?key=$apiKey",
"stories_referrer": "https://somedomain.org/referrer",
"topics_endpoint": "https://somedomain.org/topics?key=$apiKey",
"survey_link": "https://www.surveymonkey.com/r/newtabffx",
"api_key_pref": "apiKeyPref",
Expand All @@ -42,6 +43,7 @@ describe("Top Stories Feed", () => {
it("should initialize endpoints based on prefs", () => {
instance.onAction({type: at.INIT});
assert.equal("https://somedomain.org/stories?key=test-api-key", instance.stories_endpoint);
assert.equal("https://somedomain.org/referrer", instance.stories_referrer);
assert.equal("https://somedomain.org/topics?key=test-api-key", instance.topics_endpoint);
});
it("should register section", () => {
Expand Down Expand Up @@ -145,10 +147,12 @@ describe("Top Stories Feed", () => {
"title": "title",
"description": "description",
"image": "image-url",
"referrer": "referrer",
"url": "rec-url"
}];

instance.stories_endpoint = "stories-endpoint";
instance.stories_referrer = "referrer";
fetchStub.resolves({ok: true, status: 200, text: () => response});
await instance.fetchStories();

Expand Down

0 comments on commit 01db044

Please sign in to comment.