Skip to content

Commit d6f785e

Browse files
committed
Adaptable search endpoint
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.
1 parent 93004cc commit d6f785e

File tree

8 files changed

+28
-20
lines changed

8 files changed

+28
-20
lines changed

dist/client/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
</head>
1919
<body>
2020
<div id="app"></div>
21-
<script type="text/javascript" src="/main-0514cc652aa667794cf2.js"></script></body>
21+
<script type="text/javascript" src="/main-325d179e655486119e97.js"></script></body>
2222
</html>

dist/client/main-0514cc652aa667794cf2.js renamed to dist/client/main-325d179e655486119e97.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/client/main-0514cc652aa667794cf2.js.map renamed to dist/client/main-325d179e655486119e97.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/server/db.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,8 @@ var Database = /*#__PURE__*/function () {
933933

934934
return _context15.abrupt("return", new _twitter.Twitter({
935935
consumerKey: settings.appKey,
936-
consumerSecret: settings.appSecret
936+
consumerSecret: settings.appSecret,
937+
academic: settings.academic
937938
}));
938939

939940
case 7:

dist/server/twitter.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ function decode(s) {
5959

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

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

236237
var endpoint = 'tweets/search/recent';
237238

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

243246
if (opts.startDate) {
@@ -285,7 +288,7 @@ var Twitter = /*#__PURE__*/function () {
285288
}), nextToken).then(function () {
286289
if (!opts.once && newTotal < count && nextToken) {
287290
recurse(nextToken, newTotal);
288-
} else {
291+
} else if (!opts.once) {
289292
cb(null, [], null);
290293
}
291294
});

src/client/components/SearchToggle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export default class SearchToggle extends Component {
111111
// it has academic search turned on for the twitter app credentials
112112
const minDate = this.props.academic
113113
? new Date(2006, 2, 21)
114-
: moment().subtract(1, 'week')
114+
: moment().subtract(6, 'days')
115115

116116
return (
117117
<>

src/server/db.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ export class Database {
313313
return new Twitter({
314314
consumerKey: settings.appKey,
315315
consumerSecret: settings.appSecret,
316+
academic: settings.academic
316317
})
317318
} else {
318319
return null

src/server/twitter.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ function decode(s) {
1717

1818
export class Twitter {
1919

20-
constructor(keys = {}) {
21-
this.consumerKey = keys.consumerKey || process.env.CONSUMER_KEY
22-
this.consumerSecret = keys.consumerSecret || process.env.CONSUMER_SECRET
23-
this.accessToken = keys.accessToken || process.env.ACCESS_TOKEN
24-
this.accessTokenSecret = keys.accessTokenSecret || process.env.ACCESS_TOKEN_SECRET
20+
constructor(opts = {}) {
21+
this.consumerKey = opts.consumerKey || process.env.CONSUMER_KEY
22+
this.consumerSecret = opts.consumerSecret || process.env.CONSUMER_SECRET
23+
this.accessToken = opts.accessToken || process.env.ACCESS_TOKEN
24+
this.accessTokenSecret = opts.accessTokenSecret || process.env.ACCESS_TOKEN_SECRET
25+
this.academic = opts.academic || false
2526

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

127128
let endpoint = 'tweets/search/recent'
128129

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

134137
if (opts.startDate) {
@@ -172,7 +175,7 @@ export class Twitter {
172175
.then(() => {
173176
if (! opts.once && newTotal < count && nextToken) {
174177
recurse(nextToken, newTotal)
175-
} else {
178+
} else if (! opts.once) {
176179
cb(null, [], null)
177180
}
178181
})

0 commit comments

Comments
 (0)