ducks/conversations: Ensure metrics agree with in-memory messages

* ducks/conversations: Ensure metrics agree with in-memory messages
* Clean up logic cleaning up metrics
This commit is contained in:
Scott Nonnenberg 2020-01-16 08:45:06 -08:00 committed by Ken Powers
parent 7aaee0cb64
commit 4af7a9ae2c
2 changed files with 25 additions and 4 deletions

View file

@ -795,10 +795,10 @@
let scrollToLatestUnread = true;
if (newestMessageId) {
const message = await getMessageById(newestMessageId, {
const newestInMemoryMessage = await getMessageById(newestMessageId, {
Message: Whisper.Message,
});
if (!message) {
if (!newestInMemoryMessage) {
window.log.warn(
`loadNewestMessages: did not find message ${newestMessageId}`
);
@ -806,7 +806,9 @@
// If newest in-memory message is unread, scrolling down would mean going to
// the very bottom, not the oldest unread.
scrollToLatestUnread = !message.isUnread();
if (newestInMemoryMessage.isUnread()) {
scrollToLatestUnread = false;
}
}
const metrics = await getMessageMetricsForConversation(conversationId);

View file

@ -736,6 +736,21 @@ export function reducer(
const lookup = fromPairs(messages.map(message => [message.id, message]));
let { newest, oldest } = metrics;
// If our metrics are a little out of date, we'll fix them up
if (messages.length > 0) {
const first = messages[0];
if (first && (!oldest || first.received_at < oldest.received_at)) {
oldest = pick(first, ['id', 'received_at']);
}
const last = messages[messages.length - 1];
if (last && (!newest || last.received_at > newest.received_at)) {
newest = pick(last, ['id', 'received_at']);
}
}
return {
...state,
selectedMessage: scrollToMessageId,
@ -753,7 +768,11 @@ export function reducer(
? existingConversation.scrollToMessageCounter + 1
: 0,
messageIds,
metrics,
metrics: {
...metrics,
newest,
oldest,
},
resetCounter,
heightChangeMessageIds: [],
},