Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: latest tweets retrieval optimization is unpredictable #234

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/services/tweets-getter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ export const tweetsGetterService = async (
}).start();
log.text = "filtering";

let preventPostsSynchronization = false;
let preventPostsSynchronization = true;
const LATEST_TWEETS_COUNT = 5;

/**
* Synchronization optimization: prevent excessive API calls & potential rate-limiting
*
* Pull the ${LATEST_TWEETS_COUNT}, filter eligible ones.
* This optimization prevents the post sync if the latest eligible tweet is cached.
* This optimization prevents the post sync if all latest eligible tweets are cached.
* The order in which the tweets are retrieved in unpredictable, we therefore cannot
* check only the first one and assume it's also the latest one.
*/
const latestTweets = twitterClient.getTweets(
TWITTER_HANDLE,
Expand All @@ -50,18 +52,16 @@ export const tweetsGetterService = async (

for await (const latestTweet of latestTweets) {
log.text = "post: → checking for synchronization needs";
if (!preventPostsSynchronization) {
if (preventPostsSynchronization) {
// Only consider eligible tweets.
const tweet = await getEligibleTweet(tweetFormatter(latestTweet));

if (tweet) {
// If the latest eligible tweet is cached, mark sync as unneeded.
if (isTweetCached(tweet, cachedPosts)) {
preventPostsSynchronization = true;
// If this tweet is not cached, mark sync as needed
if (!isTweetCached(tweet, cachedPosts)) {
preventPostsSynchronization = false;
break;
}
// If the latest tweet is not cached,
// skip the current optimization and go to synchronization step.
break;
}
}
}
Expand Down
Loading