Skip to content

Commit 039a49e

Browse files
authored
Merge pull request #131 from IAOON/fix/126-filter-future-posts
2 parents dce0e34 + 76e3723 commit 039a49e

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ PLAUSIBLE=false
2525
LOG_QUERY=false
2626
# graphql requires EMAIL_FROM or MAILGUN_FROM
2727
28+
FUTURE_TIMESTAMP_TOLERANCE=300000

CONTRIBUTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ and set the values of the variables according to your environment.
142142
> for summarizing and translating posts using LLMs. You can use
143143
> [Anthropic] and [Google Generative AI] accounts for this. However, if
144144
> you won't test these features, you can omit these variables.
145+
>
146+
> - `FUTURE_TIMESTAMP_TOLERANCE` is the tolerance period in milliseconds for
147+
> posts with future timestamps. Posts published more than this time in the
148+
> future will be filtered out from timelines. Default value is 300000 (5
149+
> minutes). This helps prevent malicious or misconfigured remote servers
150+
> from disrupting timeline order with posts that have future timestamps.
145151
146152

147153
Creating a database schema

models/timeline.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ import {
2020
timelineItemTable,
2121
} from "./schema.ts";
2222

23+
export const FUTURE_TIMESTAMP_TOLERANCE = (() => {
24+
const envValue = Deno.env.get("FUTURE_TIMESTAMP_TOLERANCE");
25+
if (!envValue) return 300000;
26+
27+
const parsed = parseInt(envValue, 10);
28+
if (isNaN(parsed) || parsed < 0) {
29+
console.warn(
30+
`Invalid FUTURE_TIMESTAMP_TOLERANCE: "${envValue}", using default 300000`,
31+
);
32+
return 300000;
33+
}
34+
35+
return parsed;
36+
})();
37+
38+
function getFutureTimestampLimit(): Date {
39+
return new Date(Date.now() + FUTURE_TIMESTAMP_TOLERANCE);
40+
}
41+
2342
export async function addPostToTimeline(
2443
db: Database,
2544
post: Post,
@@ -235,6 +254,7 @@ export async function getPublicTimeline(
235254
window,
236255
}: PublicTimelineOptions,
237256
): Promise<TimelineEntry[]> {
257+
const futureTimestampLimit = getFutureTimestampLimit();
238258
const posts = await db.query.postTable.findMany({
239259
with: {
240260
actor: {
@@ -401,6 +421,7 @@ export async function getPublicTimeline(
401421
...(withoutShares ? { sharedPostId: { isNull: true } } : undefined),
402422
...(postType == null ? undefined : { type: postType }),
403423
...(until == null ? undefined : { published: { lte: until } }),
424+
published: { lte: futureTimestampLimit },
404425
},
405426
],
406427
},
@@ -430,6 +451,7 @@ export async function getPersonalTimeline(
430451
window,
431452
}: PersonalTimelineOptions,
432453
): Promise<TimelineEntry[]> {
454+
const futureTimestampLimit = getFutureTimestampLimit();
433455
const timeline = await db.query.timelineItemTable.findMany({
434456
with: {
435457
post: {
@@ -559,6 +581,11 @@ export async function getPersonalTimeline(
559581
currentAccount.hideForeignLanguages && currentAccount.locales != null
560582
? { language: { in: currentAccount.locales } }
561583
: {},
584+
{
585+
published: {
586+
lte: futureTimestampLimit,
587+
},
588+
},
562589
],
563590
},
564591
...(withoutShares ? { originalAuthorId: { isNotNull: true } } : {}),

0 commit comments

Comments
 (0)