Skip to content

Commit

Permalink
Fetch more older if any messsage is unread
Browse files Browse the repository at this point in the history
- Its safer that way, and also it is fine
  • Loading branch information
rottabonus committed May 10, 2024
1 parent b50f46d commit 46f2cb2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/api/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,14 @@ export const getParamsForUnreadMessages = (
messages: MessageMapping,
params: PollingParams,
): Array<PollingParams> => {
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: {
Expand All @@ -226,17 +225,21 @@ export const getParamsForUnreadMessages = (
}
};

export const getOlderThanParamsIfOldestUnread =
export const getOlderThanParamsIfHasUnread =
(messages: MessageMapping) =>
(buddyId: string): Array<PollingParams> => {
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 }]
: [];
};

Expand Down
4 changes: 4 additions & 0 deletions src/state/reducers/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,15 @@ export const reducer: automaton.Reducer<State, actions.Action> = (
// - 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,
Expand Down

0 comments on commit 46f2cb2

Please sign in to comment.