Skip to content

Commit

Permalink
📒
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Feb 22, 2024
1 parent 0964351 commit 793ae98
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bin/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function main() {
// console.log(user)

const id = '1628578692707532800'
const res = await tryGetTweetById(id, { twitterClient }, { force: true })
const res = await tryGetTweetById(id, { twitterClient })
console.log(JSON.stringify(res, null, 2))
}

Expand Down
4 changes: 2 additions & 2 deletions src/answer-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export abstract class AnswerEngine {
// Resolve any previous non-bot-related tweets in the thread
if (resolvePrevTweetsInThread) {
let tweet = await db.tryGetTweetById(message.promptTweetId, ctx, {
force: true
fetchFromTwitter: true
})

while (tweet) {
Expand All @@ -98,7 +98,7 @@ export abstract class AnswerEngine {
repliedToTweetRef.id,
ctx,
{
force: true
fetchFromTwitter: true
}
)
if (!repliedToTweet) break
Expand Down
22 changes: 16 additions & 6 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ export async function tryGetTweetById(
tweetId: string,
ctx: Pick<types.Context, 'twitterClient'>,
{
force = false
fetchFromTwitter = false
}: {
// If true, will fetch the tweet from twitter if it's not found in the cache
force?: boolean
// Whether or not to fetch tweets from twitter if they're missing from the cache
fetchFromTwitter?: boolean
} = {}
): Promise<types.Tweet | undefined> {
if (!tweetId) return
Expand All @@ -215,7 +215,7 @@ export async function tryGetTweetById(
return tweet
}

if (force) {
if (fetchFromTwitter) {
try {
const { data: tweet, includes } = await twitter.findTweetById(
tweetId,
Expand Down Expand Up @@ -256,8 +256,13 @@ export async function tryGetTweetById(
return tweet
}

/**
* Prune most tweet entities since they're verbose and can be recomputed easily.
*
* We still want to fetch `entities` because some mof them are useful, but the
* twitter api doesn't allow us to only fetch some entities.
*/
function pruneTweet(tweet: types.Tweet) {
// Prune most tweet entities since they're verbose and can be recomputed easily
if (tweet.entities) {
if (tweet.entities.urls) {
tweet.entities = { urls: tweet.entities?.urls }
Expand All @@ -267,8 +272,13 @@ function pruneTweet(tweet: types.Tweet) {
}
}

/**
* Prune most user entities since they're verbose and can be recomputed easily.
*
* We still want to fetch `entities` because some mof them are useful, but the
* twitter api doesn't allow us to only fetch some entities.
*/
function pruneTwitterUser(user: types.TwitterUser) {
// Prune most user entities since they're verbose and can be recomputed easily
if (user.entities) {
if (user.entities.description?.urls) {
user.entities.description = {
Expand Down

0 comments on commit 793ae98

Please sign in to comment.