Skip to content

Commit

Permalink
Fixed a bug where quoted tweets with limited visibility threw an error
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishikant181 committed Oct 17, 2024
1 parent 78b7506 commit b4aa795
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"commander": "11.1.0",
"https-proxy-agent": "7.0.2",
"rettiwt-auth": "2.1.0",
"rettiwt-core": "4.3.0"
"rettiwt-core": "4.3.1"
},
"devDependencies": {
"@types/node": "20.4.1",
Expand Down
63 changes: 59 additions & 4 deletions src/models/data/Tweet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
EMediaType,
ILimitedVisibilityTweet,
IExtendedMedia as IRawExtendedMedia,
ITweet as IRawTweet,
IEntities as IRawTweetEntities,
Expand Down Expand Up @@ -77,7 +78,7 @@ export class Tweet {
this.tweetBy = new User(tweet.core.user_results.result);
this.entities = new TweetEntities(tweet.legacy.entities);
this.media = tweet.legacy.extended_entities?.media?.map((media) => new TweetMedia(media));
this.quoted = tweet.quoted_status_result ? new Tweet(tweet.quoted_status_result.result) : undefined;
this.quoted = this.getQuotedTweet(tweet);
this.fullText = tweet.note_tweet ? tweet.note_tweet.note_tweet_results.result.text : tweet.legacy.full_text;
this.replyTo = tweet.legacy.in_reply_to_status_id_str;
this.lang = tweet.legacy.lang;
Expand All @@ -87,9 +88,63 @@ export class Tweet {
this.likeCount = tweet.legacy.favorite_count;
this.viewCount = tweet.views.count ? parseInt(tweet.views.count) : 0;
this.bookmarkCount = tweet.legacy.bookmark_count;
this.retweetedTweet = tweet.legacy.retweeted_status_result?.result?.rest_id
? new Tweet(tweet.legacy.retweeted_status_result.result)
: undefined;
this.retweetedTweet = this.getRetweetedTweet(tweet);
}

/**
* Extract and deserialize the original quoted tweet from the given raw tweet.
*
* @param tweet - The raw tweet.
*
* @returns - The deserialized original quoted tweet.
*/
private getQuotedTweet(tweet: IRawTweet): Tweet | undefined {
// If tweet with limited visibility
if (
tweet.quoted_status_result &&
Object.entries(tweet.quoted_status_result).length &&
tweet.quoted_status_result.result.__typename == 'TweetWithVisibilityResults'
) {
return new Tweet((tweet.quoted_status_result.result as ILimitedVisibilityTweet).tweet);
}
// If normal tweet
else if (tweet.quoted_status_result && Object.entries(tweet.quoted_status_result).length) {
return new Tweet(tweet);
}
// Else, skip
else {
// Logging
LogService.log(ELogActions.WARNING, {
action: ELogActions.DESERIALIZE,
message: 'Quoted tweet not found, skipping',
});

return undefined;
}
}

/**
* Extract and deserialize the original retweeted tweet from the given raw tweet.
*
* @param tweet - The raw tweet.
*
* @returns - The deserialized original retweeted tweet.
*/
private getRetweetedTweet(tweet: IRawTweet): Tweet | undefined {
// If valid retweeted tweet
if (tweet.legacy.retweeted_status_result?.result?.rest_id) {
return new Tweet(tweet.legacy.retweeted_status_result.result);
}
// Else, skip
else {
// Logging
LogService.log(ELogActions.WARNING, {
action: ELogActions.DESERIALIZE,
message: 'Retweeted tweet not found, skipping',
});

return undefined;
}
}

/**
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2743,7 +2743,7 @@ __metadata:
nodemon: "npm:2.0.20"
prettier: "npm:3.0.0"
rettiwt-auth: "npm:2.1.0"
rettiwt-core: "npm:4.3.0"
rettiwt-core: "npm:4.3.1"
typedoc: "npm:0.24.8"
typescript: "npm:5.1.6"
bin:
Expand All @@ -2765,13 +2765,13 @@ __metadata:
languageName: node
linkType: hard

"rettiwt-core@npm:4.3.0":
version: 4.3.0
resolution: "rettiwt-core@npm:4.3.0"
"rettiwt-core@npm:4.3.1":
version: 4.3.1
resolution: "rettiwt-core@npm:4.3.1"
dependencies:
axios: "npm:1.6.3"
form-data: "npm:4.0.0"
checksum: 10c0/d81adf975057c45142d01cfd244589787b1a275d18d6d31a1d0dfe5499524aac54528ba3910b81a6e1cdf17ce73bde4ae275659b1dbf50e839b5888a09e9d4c1
checksum: 10c0/bd3c2fec2ce96e70b65657a9ea3b0449d329d7b1d7d378453624266520480f404d131cf770ea3bff85e41be9c97cdd158b66a4840ea4a55cf7765992aa864bc1
languageName: node
linkType: hard

Expand Down

0 comments on commit b4aa795

Please sign in to comment.