diff --git a/lib/types/message-types.js b/lib/types/message-types.js --- a/lib/types/message-types.js +++ b/lib/types/message-types.js @@ -468,6 +468,7 @@ +currentAsOf: number, }; export const defaultNumberPerThread = 20; +export const defaultMaxMessageAge = 14 * 24 * 60 * 60 * 1000; // 2 weeks export type SendMessageResponse = { +newMessageInfo: RawMessageInfo, diff --git a/server/src/fetchers/message-fetchers.js b/server/src/fetchers/message-fetchers.js --- a/server/src/fetchers/message-fetchers.js +++ b/server/src/fetchers/message-fetchers.js @@ -19,6 +19,7 @@ type MessageTruncationStatus, messageTruncationStatus, type FetchMessageInfosResult, + defaultMaxMessageAge, } from 'lib/types/message-types'; import { threadPermissions } from 'lib/types/thread-types'; import { ServerError } from 'lib/utils/errors'; @@ -353,8 +354,19 @@ function messageSelectionCriteriaToSQLClause( criteria: MessageSelectionCriteria, ) { + const minMessageTime = Date.now() - defaultMaxMessageAge; + + let globalTimeFilter; + if (criteria.newerThan) { + globalTimeFilter = SQL`m.time > ${criteria.newerThan}`; + } else if (!criteria.threadCursors) { + globalTimeFilter = SQL`m.time > ${minMessageTime}`; + } + const threadConditions = []; - if (criteria.joinedThreads === true) { + if (criteria.joinedThreads === true && !globalTimeFilter) { + threadConditions.push(SQL`(mm.role > 0 AND m.time > ${minMessageTime})`); + } else if (criteria.joinedThreads === true) { threadConditions.push(SQL`mm.role > 0`); } if (criteria.threadCursors) { @@ -374,10 +386,7 @@ } const threadClause = mergeOrConditions(threadConditions); - const conditions = [threadClause]; - if (criteria.newerThan) { - conditions.push(SQL`m.time > ${criteria.newerThan}`); - } + const conditions = [globalTimeFilter, threadClause].filter(Boolean); return mergeAndConditions(conditions); }