Browse Source

Filter out replies in the proper place

Thomas Dy 2 years ago
parent
commit
87f73512f5
2 changed files with 10 additions and 8 deletions
  1. 6 0
      index.ts
  2. 4 8
      store.ts

+ 6 - 0
index.ts

@@ -15,6 +15,7 @@ class Server {
   async getLatestTweets(username: string, interval: number = 600): Promise<Tweet[]> {
     const storedAuthorId = this.store.getAuthorId(username);
     const latestTweet = storedAuthorId === null ? null : this.store.getLatestTweet(storedAuthorId);
+    let authorId = storedAuthorId;
 
     let timeline = await this.client.getTimeline(username, latestTweet?.id_str);
     console.log(`Fetched ${timeline.length} new tweets`);
@@ -35,7 +36,12 @@ class Server {
       const lastTweet = timeline[timeline.length - 1];
       const olderTweets = this.store.getLatestTweets(lastTweet.user.id_str, lastTweet.id_str, newerThan);
       timeline = timeline.concat(olderTweets);
+      authorId = lastTweet.user.id_str;
     }
+
+    timeline = timeline.filter(({ in_reply_to_user_id_str }) => {
+      return in_reply_to_user_id_str === null || in_reply_to_user_id_str === authorId;
+    });
     return timeline;
   }
 

+ 4 - 8
store.ts

@@ -90,14 +90,10 @@ export class SqliteStore extends Store {
       ORDER BY id DESC
     `, [ BigInt(authorId), BigInt(olderThanId), dateToEpoch(newerThan) ]);
 
-    return result
-      .map(([ tweetStr ]) => {
-        const tweet = JSON.parse(tweetStr);
-        return TweetSchema.parse(tweet);
-      })
-      .filter(({ in_reply_to_user_id_str }) => {
-        return in_reply_to_user_id_str === null || in_reply_to_user_id_str === authorId;
-      });
+    return result.map(([ tweetStr ]) => {
+      const tweet = JSON.parse(tweetStr);
+      return TweetSchema.parse(tweet);
+    });
   }
 
   getAuthorId(username: string): string | null {