Skip to content

Commit

Permalink
fix: filter out empty tweets in db
Browse files Browse the repository at this point in the history
  • Loading branch information
prinsss committed Oct 21, 2024
1 parent 9b3ccb8 commit 2e35ba0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
22 changes: 20 additions & 2 deletions src/core/database/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ export class DatabaseManager {
return [];
}
const tweetIds = captures.map((capture) => capture.data_key);
return this.tweets().where('rest_id').anyOf(tweetIds).toArray().catch(this.logError);
return this.tweets()
.where('rest_id')
.anyOf(tweetIds)
.filter((t) => this.filterEmptyData(t))
.toArray()
.catch(this.logError);
}

async extGetCapturedUsers(extName: string) {
Expand All @@ -65,7 +70,12 @@ export class DatabaseManager {
return [];
}
const userIds = captures.map((capture) => capture.data_key);
return this.users().where('rest_id').anyOf(userIds).toArray().catch(this.logError);
return this.users()
.where('rest_id')
.anyOf(userIds)
.filter((t) => this.filterEmptyData(t))
.toArray()
.catch(this.logError);
}

/*
Expand Down Expand Up @@ -173,6 +183,14 @@ export class DatabaseManager {
return this.captures().clear().catch(this.logError);
}

private filterEmptyData(data: Tweet | User) {
if (!data?.legacy) {
logger.warn('Empty data found in DB', data);
return false;
}
return true;
}

/*
|--------------------------------------------------------------------------
| Migrations
Expand Down
13 changes: 11 additions & 2 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ export function isTimelineEntryListSearch(
export function extractTweetUnion(tweet: TweetUnion): Tweet | null {
try {
if (tweet.__typename === 'Tweet') {
return tweet;
return filterEmptyTweet(tweet);
}

if (tweet.__typename === 'TweetWithVisibilityResults') {
return tweet.tweet;
return filterEmptyTweet(tweet.tweet);
}

if (tweet.__typename === 'TweetTombstone') {
Expand Down Expand Up @@ -256,6 +256,15 @@ export function extractTweetFullText(tweet: Tweet): string {
return tweet.note_tweet?.note_tweet_results.result.text ?? tweet.legacy.full_text;
}

export function filterEmptyTweet(tweet: Tweet): Tweet | null {
if (!tweet.legacy) {
logger.warn('Empty tweet received', tweet);
return null;
}

return tweet;
}

/*
|--------------------------------------------------------------------------
| Media operations.
Expand Down

0 comments on commit 2e35ba0

Please sign in to comment.