2024-11-12 03:59:37 +01:00
|
|
|
import type {Message} from "./message_store.ts";
|
|
|
|
import * as people from "./people.ts";
|
|
|
|
import {get_key_from_message} from "./recent_view_util.ts";
|
2021-06-10 14:18:46 +02:00
|
|
|
|
2023-10-31 04:25:47 +01:00
|
|
|
export type ConversationData = {
|
2023-09-22 09:58:32 +02:00
|
|
|
last_msg_id: number;
|
|
|
|
participated: boolean;
|
|
|
|
type: "private" | "stream";
|
|
|
|
};
|
2023-10-31 04:25:47 +01:00
|
|
|
export const conversations = new Map<string, ConversationData>();
|
2022-11-18 17:00:42 +01:00
|
|
|
// For stream messages, key is stream-id:topic.
|
|
|
|
// For pms, key is the user IDs to whom the message is being sent.
|
2021-06-10 14:18:46 +02:00
|
|
|
|
2023-09-22 09:58:32 +02:00
|
|
|
export function process_message(msg: Message): boolean {
|
2023-10-17 12:16:46 +02:00
|
|
|
// Important: This function must correctly handle processing a
|
|
|
|
// given message more than once; this happens during the loading
|
|
|
|
// process because of how recent_view_message_list_data duplicates
|
|
|
|
// all_messages_data.
|
|
|
|
|
2022-11-18 17:00:42 +01:00
|
|
|
// Return whether any conversation data is updated.
|
|
|
|
let conversation_data_updated = false;
|
|
|
|
|
|
|
|
// Initialize conversation data
|
2022-04-24 06:13:19 +02:00
|
|
|
const key = get_key_from_message(msg);
|
2023-10-31 04:25:47 +01:00
|
|
|
let conversation_data = conversations.get(key);
|
|
|
|
if (conversation_data === undefined) {
|
|
|
|
conversation_data = {
|
2021-06-10 14:18:46 +02:00
|
|
|
last_msg_id: -1,
|
|
|
|
participated: false,
|
2022-04-24 06:13:19 +02:00
|
|
|
type: msg.type,
|
2023-09-22 09:58:32 +02:00
|
|
|
};
|
2023-10-31 04:25:47 +01:00
|
|
|
conversations.set(key, conversation_data);
|
2022-11-18 17:00:42 +01:00
|
|
|
conversation_data_updated = true;
|
2021-06-10 14:18:46 +02:00
|
|
|
}
|
2022-11-18 17:00:42 +01:00
|
|
|
// Update conversation data
|
2023-10-31 04:25:47 +01:00
|
|
|
if (conversation_data.last_msg_id < msg.id) {
|
2021-06-10 14:18:46 +02:00
|
|
|
// NOTE: This also stores locally echoed msg_id which
|
|
|
|
// has not been successfully received from the server.
|
|
|
|
// We store it now and reify it when response is available
|
|
|
|
// from server.
|
2023-10-31 04:25:47 +01:00
|
|
|
conversation_data.last_msg_id = msg.id;
|
2022-11-18 17:00:42 +01:00
|
|
|
conversation_data_updated = true;
|
2021-06-10 14:18:46 +02:00
|
|
|
}
|
|
|
|
// TODO: Add backend support for participated topics.
|
|
|
|
// Currently participated === recently participated
|
|
|
|
// 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
|
|
|
|
// to topic info fetched from backend, which is currently not a thing.
|
2023-10-31 04:25:47 +01:00
|
|
|
if (!conversation_data.participated && people.is_my_user_id(msg.sender_id)) {
|
|
|
|
conversation_data.participated = true;
|
2022-11-18 17:00:42 +01:00
|
|
|
conversation_data_updated = true;
|
|
|
|
}
|
|
|
|
return conversation_data_updated;
|
2021-06-10 14:18:46 +02:00
|
|
|
}
|
|
|
|
|
2023-10-31 04:25:47 +01:00
|
|
|
function get_sorted_conversations(): Map<string | undefined, ConversationData> {
|
|
|
|
// Sort all recent conversations by last message time.
|
|
|
|
return new Map(
|
|
|
|
[...conversations.entries()].sort((a, b) => b[1].last_msg_id - a[1].last_msg_id),
|
|
|
|
);
|
2021-06-10 14:18:46 +02:00
|
|
|
}
|
|
|
|
|
2023-10-31 04:25:47 +01:00
|
|
|
export function get_conversations(): Map<string | undefined, ConversationData> {
|
|
|
|
return get_sorted_conversations();
|
2021-06-10 14:18:46 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 09:58:32 +02:00
|
|
|
export function reify_message_id_if_available(opts: {old_id: number; new_id: number}): boolean {
|
2023-10-31 04:25:47 +01:00
|
|
|
// We don't need to reify the message_id of the conversation
|
|
|
|
// if a new message arrives in the conversation from another user,
|
|
|
|
// since it replaces the last_msg_id of the conversation which
|
2021-06-10 14:18:46 +02:00
|
|
|
// we were trying to reify.
|
2023-10-31 04:25:47 +01:00
|
|
|
for (const value of conversations.values()) {
|
2021-06-10 14:18:46 +02:00
|
|
|
if (value.last_msg_id === opts.old_id) {
|
|
|
|
value.last_msg_id = opts.new_id;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|