Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Commit

Permalink
Add safety check during Twitter response URL parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Tuleja committed Aug 9, 2018
1 parent 76413a7 commit 832287f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions services/newsfeed/TwitterService.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ const client = new Twitter({
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});

const safeRef = (obj, path) => {
try {
// eslint-disable-next-line
return path.split('.').reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, obj);
} catch (e) {
return null;
}
};

const tweetUrl = (o) => {
if (o.retweeted_status) {
return o.retweeted_status.entities.urls[0].url;
return safeRef(o, 'retweeted_status.entities.urls.0.url');
} else if (o.extended_entities) {
return o.extended_entities.media[0].url;
return safeRef(o, 'extended_entities.media.0.url');
} else if (o.entities) {
return o.entities.urls[0].url;
return safeRef(o, 'entities.urls.0.url');
}
return null;
};
Expand Down

0 comments on commit 832287f

Please sign in to comment.