diff --git a/src/tweets/timeline/timeline.service.ts b/src/tweets/timeline/timeline.service.ts index 71423a91..b21d88a5 100644 --- a/src/tweets/timeline/timeline.service.ts +++ b/src/tweets/timeline/timeline.service.ts @@ -1241,6 +1241,7 @@ export class TimelineService { // 2. Get tweet candidates from the Following timeline cache const followingTweets = await this.getFollowingTimelineCandidates(userId, FOR_YOU_FEED_SIZE); const followingTweetIds = new Set(followingTweets.map((t) => t.id)); // Keep track of which IDs came from this source + const followingAuthorIds = new Set(followingTweets.map((t) => t.authorId)); this.logger.debug( `Got ${followingTweets.length} candidate tweets from Following timeline for user ${userId}`, @@ -1275,14 +1276,22 @@ export class TimelineService { } const candidateTweetIds = candidates.map((c) => BigInt(c.id)); - const candidateAuthorIds = new Set(candidates.map((c) => BigInt(c.authorId))); + const candidateAuthorIds = Array.from(new Set(candidates.map((c) => BigInt(c.authorId)))); - const [validAuthorIds, validTweetIds] = await Promise.all([ - this.tweetsRepository.filterNonMutedAuthors(userId, Array.from(candidateAuthorIds)), + const followingCandidateAuthorIds = candidateAuthorIds.filter((id) => + followingAuthorIds.has(id.toString()), + ); + const interestCandidateAuthorIds = candidateAuthorIds.filter( + (id) => !followingAuthorIds.has(id.toString()), + ); + + const [validFollowingAuthorIds, validInterestAuthorIds, validTweetIds] = await Promise.all([ + this.tweetsRepository.filterValidAuthors(userId, followingCandidateAuthorIds), + this.tweetsRepository.filterNonMutedNonBlockedAuthors(userId, interestCandidateAuthorIds), this.tweetsRepository.filterValidTweets(candidateTweetIds), ]); - validAuthorIds.push(userId); + const validAuthorIds = [userId, ...validFollowingAuthorIds, ...validInterestAuthorIds]; const validAuthorSet = new Set(validAuthorIds.map((id) => id.toString())); const validTweetSet = new Set(validTweetIds.map((id) => id.toString())); diff --git a/src/tweets/tweets.repository.ts b/src/tweets/tweets.repository.ts index 00148433..49c84868 100644 --- a/src/tweets/tweets.repository.ts +++ b/src/tweets/tweets.repository.ts @@ -1450,7 +1450,7 @@ export class TweetsRepository { return validFollows.map((f) => f.followedId); } - async filterNonMutedAuthors(userId: bigint, authorIds: bigint[]): Promise { + async filterNonMutedNonBlockedAuthors(userId: bigint, authorIds: bigint[]): Promise { const validAuthors = await this.prisma.user.findMany({ where: { id: { in: authorIds }, @@ -1460,6 +1460,16 @@ export class TweetsRepository { userId: userId, }, }, + blockedBy: { + none: { + userId: userId, + }, + }, + blockedUsers: { + none: { + blockedId: userId, + }, + }, }, select: { id: true }, });