Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 0df985e

Browse files
committed
fix: latest tweets retrieval optimization is unpredictable
`getTweets` does not always return results in a predictable order, which means that the first tweet in not necessarily the latest one. This breaks the synchronization optimization because it relies on this assumption. This fix reverses the implementation for this optimization: instead of preventing the sync when the latest tweet if cached, we prevent the sync by default but allow it to run if we find that *any* of the ${LATEST_TWEETS_COUNT} tweets is not cached.
1 parent 10dfac4 commit 0df985e

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/services/tweets-getter.service.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ export const tweetsGetterService = async (
3434
}).start();
3535
log.text = "filtering";
3636

37-
let preventPostsSynchronization = false;
37+
let preventPostsSynchronization = true;
3838
const LATEST_TWEETS_COUNT = 5;
3939

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

5153
for await (const latestTweet of latestTweets) {
5254
log.text = "post: → checking for synchronization needs";
53-
if (!preventPostsSynchronization) {
55+
if (preventPostsSynchronization) {
5456
// Only consider eligible tweets.
5557
const tweet = await getEligibleTweet(tweetFormatter(latestTweet));
5658

5759
if (tweet) {
58-
// If the latest eligible tweet is cached, mark sync as unneeded.
59-
if (isTweetCached(tweet, cachedPosts)) {
60-
preventPostsSynchronization = true;
60+
// If this tweet is not cached, mark sync as needed
61+
if (!isTweetCached(tweet, cachedPosts)) {
62+
preventPostsSynchronization = false;
63+
break;
6164
}
62-
// If the latest tweet is not cached,
63-
// skip the current optimization and go to synchronization step.
64-
break;
6565
}
6666
}
6767
}

0 commit comments

Comments
 (0)