From 4a8ffe20a7ebe064fa9e970fb417000672b7ac27 Mon Sep 17 00:00:00 2001
From: taichanne30 <dev@taichan.site>
Date: Thu, 7 Mar 2024 01:47:41 +0900
Subject: [PATCH] Fix timeline fetch when using sinceId

---
 packages/backend/src/core/FanoutTimelineEndpointService.ts | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/packages/backend/src/core/FanoutTimelineEndpointService.ts b/packages/backend/src/core/FanoutTimelineEndpointService.ts
index dab9f74f76..c3298ec5b9 100644
--- a/packages/backend/src/core/FanoutTimelineEndpointService.ts
+++ b/packages/backend/src/core/FanoutTimelineEndpointService.ts
@@ -70,14 +70,13 @@ export class FanoutTimelineEndpointService {
 		shouldFallbackToDb = ps.useDbFallback && (redisResult.length > 1 && redisResult.some(ids => ids.length === 0));
 
 		// 取得したresultの中で最古のIDのうち、最も新しいものを取得
-		// shouldPrependがtrueの場合は最も新しいものを、falseの場合は最も古いものを取得
-		const thresholdId = shouldPrepend ? redisResult.map(ids => ids[ids.length - 1]).sort(idCompare)[0] : redisResult.map(ids => ids[0]).sort(idCompare)[0];
+		const thresholdId = redisResult.map(ids => ids[0]).sort()[0];
 
 		// TODO: いい感じにgetMulti内でソート済だからuniqするときにredisResultが全てソート済なのを利用して再ソートを避けたい
-		const redisResultIds = shouldFallbackToDb ? [] : Array.from(new Set(redisResult.flat(1))).filter(id => idCompare(id, thresholdId) === 1);
+		const redisResultIds = shouldFallbackToDb ? [] : Array.from(new Set(redisResult.flat(1)));
 
 		redisResultIds.sort(idCompare);
-		noteIds = redisResultIds.slice(0, ps.limit);
+		noteIds = redisResultIds.filter(id => id >= thresholdId).slice(0, ps.limit);
 
 		shouldFallbackToDb = shouldFallbackToDb || (noteIds.length === 0);