From 46f2cb2651343589e6c8f5943ea2a5a3439d55ef Mon Sep 17 00:00:00 2001 From: Felix Hallenberg Date: Fri, 10 May 2024 10:38:35 +0300 Subject: [PATCH] Fetch more older if any messsage is unread - Its safer that way, and also it is fine --- src/api/messages.ts | 29 ++++++++++++++++------------- src/state/reducers/messages.ts | 4 ++++ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/api/messages.ts b/src/api/messages.ts index 968a23a9..b87a36f2 100644 --- a/src/api/messages.ts +++ b/src/api/messages.ts @@ -209,15 +209,14 @@ export const getParamsForUnreadMessages = ( messages: MessageMapping, params: PollingParams, ): Array => { + console.log('getParamsForUnreadMessages'); switch (params.type) { case 'OlderThan': { - return getOlderThanParamsIfOldestUnread(messages)(params.buddyId); + return getOlderThanParamsIfHasUnread(messages)(params.buddyId); } case 'InitialMessages': { - return params.buddyIds.flatMap( - getOlderThanParamsIfOldestUnread(messages), - ); + return params.buddyIds.flatMap(getOlderThanParamsIfHasUnread(messages)); } default: { @@ -226,17 +225,21 @@ export const getParamsForUnreadMessages = ( } }; -export const getOlderThanParamsIfOldestUnread = +export const getOlderThanParamsIfHasUnread = (messages: MessageMapping) => (buddyId: string): Array => { - const sorted = Object.keys(messages[buddyId]) - .map(msgId => messages[buddyId][msgId]) - .sort(sortSentTime); - const oldest = sorted.length > 0 ? sorted[sorted.length - 1] : null; - const isOldestUnseen = oldest && !oldest.isSeen; - - return isOldestUnseen - ? [{ type: 'OlderThan', buddyId, messageId: oldest.messageId }] + console.log('getOlderThanParamsIfHasUnread for buddyId:', buddyId); + const buddyMessages = messages[buddyId] ?? {}; + const sorted = Object.keys(buddyMessages).map( + msgId => buddyMessages[msgId], + ); + + const hasUnread = sorted.some(message => !message.isSeen); + + console.log('hasUnread', hasUnread); + + return hasUnread + ? [{ type: 'OlderThan', buddyId, messageId: sorted[0].messageId }] : []; }; diff --git a/src/state/reducers/messages.ts b/src/state/reducers/messages.ts index 97e7ba5b..733b0005 100644 --- a/src/state/reducers/messages.ts +++ b/src/state/reducers/messages.ts @@ -102,11 +102,15 @@ export const reducer: automaton.Reducer = ( // - mentor opens chat // case 2: marking unread message // case 3: marking unread only for messages that are seen 100% + console.log('got messages', newMessages); const newOlderThanParams = messageApi.getParamsForUnreadMessages( newMessages, state.currentParams, ); + console.log('polled with', state.currentParams); + console.log('new older than params', newOlderThanParams); + const [nextCurrent, nextQueue] = messageApi.getNextParams( action.payload, state.pollingQueue,