Skip to content

Commit

Permalink
Adaptable search endpoint
Browse files Browse the repository at this point in the history
When the app is not configured with academic research prodcut track keys
it will not try to search the '/all' endpoint even when asked to. Fixes

This commit also fixes a bug in server.twitter when performing a single
search (no paging). The problem was that the search callback was getting
called twice, the second time with no next token, when search was told
to just do one search (once: true). This was a problem for the
SearchLoader which was receiving that second callback with no next token
and flagging the search as complete, even though there were more
results to fetch.
  • Loading branch information
edsu committed Mar 27, 2022
1 parent 93004cc commit d6f785e
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="/main-0514cc652aa667794cf2.js"></script></body>
<script type="text/javascript" src="/main-325d179e655486119e97.js"></script></body>
</html>

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion dist/server/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,8 @@ var Database = /*#__PURE__*/function () {

return _context15.abrupt("return", new _twitter.Twitter({
consumerKey: settings.appKey,
consumerSecret: settings.appSecret
consumerSecret: settings.appSecret,
academic: settings.academic
}));

case 7:
Expand Down
17 changes: 10 additions & 7 deletions dist/server/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ function decode(s) {

var Twitter = /*#__PURE__*/function () {
function Twitter() {
var keys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
(0, _classCallCheck2["default"])(this, Twitter);
this.consumerKey = keys.consumerKey || process.env.CONSUMER_KEY;
this.consumerSecret = keys.consumerSecret || process.env.CONSUMER_SECRET;
this.accessToken = keys.accessToken || process.env.ACCESS_TOKEN;
this.accessTokenSecret = keys.accessTokenSecret || process.env.ACCESS_TOKEN_SECRET; // user client for Twitter v1.1 and v2 API endpoints
this.consumerKey = opts.consumerKey || process.env.CONSUMER_KEY;
this.consumerSecret = opts.consumerSecret || process.env.CONSUMER_SECRET;
this.accessToken = opts.accessToken || process.env.ACCESS_TOKEN;
this.accessTokenSecret = opts.accessTokenSecret || process.env.ACCESS_TOKEN_SECRET;
this.academic = opts.academic || false; // user client for Twitter v1.1 and v2 API endpoints

if (this.consumerKey && this.consumerSecret && this.accessToken && this.accessTokenSecret) {
this.twit = new _twit["default"]({
Expand Down Expand Up @@ -235,9 +236,11 @@ var Twitter = /*#__PURE__*/function () {

var endpoint = 'tweets/search/recent';

if (opts.all) {
if (opts.all && this.academic) {
endpoint = 'tweets/search/all';
params.start_time = '2006-03-21T00:00:00Z';
} else if (opts.all) {
_logger["default"].warn('unable to search all endpoint since settings indicate no academic access');
}

if (opts.startDate) {
Expand Down Expand Up @@ -285,7 +288,7 @@ var Twitter = /*#__PURE__*/function () {
}), nextToken).then(function () {
if (!opts.once && newTotal < count && nextToken) {
recurse(nextToken, newTotal);
} else {
} else if (!opts.once) {
cb(null, [], null);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/client/components/SearchToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default class SearchToggle extends Component {
// it has academic search turned on for the twitter app credentials
const minDate = this.props.academic
? new Date(2006, 2, 21)
: moment().subtract(1, 'week')
: moment().subtract(6, 'days')

return (
<>
Expand Down
1 change: 1 addition & 0 deletions src/server/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ export class Database {
return new Twitter({
consumerKey: settings.appKey,
consumerSecret: settings.appSecret,
academic: settings.academic
})
} else {
return null
Expand Down
17 changes: 10 additions & 7 deletions src/server/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ function decode(s) {

export class Twitter {

constructor(keys = {}) {
this.consumerKey = keys.consumerKey || process.env.CONSUMER_KEY
this.consumerSecret = keys.consumerSecret || process.env.CONSUMER_SECRET
this.accessToken = keys.accessToken || process.env.ACCESS_TOKEN
this.accessTokenSecret = keys.accessTokenSecret || process.env.ACCESS_TOKEN_SECRET
constructor(opts = {}) {
this.consumerKey = opts.consumerKey || process.env.CONSUMER_KEY
this.consumerSecret = opts.consumerSecret || process.env.CONSUMER_SECRET
this.accessToken = opts.accessToken || process.env.ACCESS_TOKEN
this.accessTokenSecret = opts.accessTokenSecret || process.env.ACCESS_TOKEN_SECRET
this.academic = opts.academic || false

// user client for Twitter v1.1 and v2 API endpoints
if (this.consumerKey && this.consumerSecret && this.accessToken && this.accessTokenSecret) {
Expand Down Expand Up @@ -126,9 +127,11 @@ export class Twitter {

let endpoint = 'tweets/search/recent'

if (opts.all) {
if (opts.all && this.academic) {
endpoint = 'tweets/search/all'
params.start_time = '2006-03-21T00:00:00Z'
} else if (opts.all) {
log.warn('unable to search all endpoint since settings indicate no academic access')
}

if (opts.startDate) {
Expand Down Expand Up @@ -172,7 +175,7 @@ export class Twitter {
.then(() => {
if (! opts.once && newTotal < count && nextToken) {
recurse(nextToken, newTotal)
} else {
} else if (! opts.once) {
cb(null, [], null)
}
})
Expand Down

0 comments on commit d6f785e

Please sign in to comment.