recent-conversations: Check for data change when processing message.

Add in a check to `recent_topics_data.process_message` so that we
know if any conversation data was updated and can rerender the
based on that information.
This commit is contained in:
Lauryn Menard
2022-11-18 17:00:42 +01:00
committed by Tim Abbott
parent 6926415c26
commit a67a622adc
2 changed files with 23 additions and 8 deletions

View File

@@ -1,12 +1,15 @@
import * as people from "./people"; import * as people from "./people";
import {get_key_from_message} from "./recent_topics_util"; import {get_key_from_message} from "./recent_topics_util";
export const topics = new Map(); // Key is stream-id:topic. export const topics = new Map();
// For stream messages, key is stream-id:topic.
// For pms, key is the user IDs to whom the message is being sent.
export function process_message(msg) { export function process_message(msg) {
// Initialize topic and pm data // Return whether any conversation data is updated.
// Key for private message is the user id's let conversation_data_updated = false;
// to whom the message is begin sent.
// Initialize conversation data
const key = get_key_from_message(msg); const key = get_key_from_message(msg);
if (!topics.has(key)) { if (!topics.has(key)) {
topics.set(key, { topics.set(key, {
@@ -14,9 +17,9 @@ export function process_message(msg) {
participated: false, participated: false,
type: msg.type, type: msg.type,
}); });
conversation_data_updated = true;
} }
// Update topic data // Update conversation data
const is_ours = people.is_my_user_id(msg.sender_id);
const topic_data = topics.get(key); const topic_data = topics.get(key);
if (topic_data.last_msg_id < msg.id) { if (topic_data.last_msg_id < msg.id) {
// NOTE: This also stores locally echoed msg_id which // NOTE: This also stores locally echoed msg_id which
@@ -24,13 +27,18 @@ export function process_message(msg) {
// We store it now and reify it when response is available // We store it now and reify it when response is available
// from server. // from server.
topic_data.last_msg_id = msg.id; topic_data.last_msg_id = msg.id;
conversation_data_updated = true;
} }
// TODO: Add backend support for participated topics. // TODO: Add backend support for participated topics.
// Currently participated === recently participated // Currently participated === recently participated
// i.e. Only those topics are participated for which we have the user's // i.e. Only those topics are participated for which we have the user's
// message fetched in the topic. Ideally we would want this to be attached // message fetched in the topic. Ideally we would want this to be attached
// to topic info fetched from backend, which is currently not a thing. // to topic info fetched from backend, which is currently not a thing.
topic_data.participated = is_ours || topic_data.participated; if (!topic_data.participated && people.is_my_user_id(msg.sender_id)) {
topic_data.participated = true;
conversation_data_updated = true;
}
return conversation_data_updated;
} }
function get_sorted_topics() { function get_sorted_topics() {

View File

@@ -298,10 +298,17 @@ export function process_messages(messages) {
// the UX can be bad if user wants to scroll down the list as // the UX can be bad if user wants to scroll down the list as
// the UI will be returned to the beginning of the list on every // the UI will be returned to the beginning of the list on every
// update. // update.
let conversation_data_updated = false;
if (messages.length > 0) { if (messages.length > 0) {
for (const msg of messages) { for (const msg of messages) {
process_message(msg); if (process_message(msg)) {
conversation_data_updated = true;
}
} }
}
// Only rerender if conversation data actually changed.
if (conversation_data_updated) {
complete_rerender(); complete_rerender();
} }
} }