recent view: Rename topic to conversation.

This commit is contained in:
evykassirer 2023-10-30 20:25:47 -07:00 committed by Tim Abbott
parent 83e0f8bd33
commit 2f6d3605d9
3 changed files with 43 additions and 41 deletions

View File

@ -2,12 +2,12 @@ import * as people from "./people";
import {get_key_from_message} from "./recent_view_util";
import type {Message} from "./types";
export type TopicData = {
export type ConversationData = {
last_msg_id: number;
participated: boolean;
type: "private" | "stream";
};
export const topics = new Map<string, TopicData>();
export const conversations = new Map<string, ConversationData>();
// For stream messages, key is stream-id:topic.
// For pms, key is the user IDs to whom the message is being sent.
@ -22,23 +22,23 @@ export function process_message(msg: Message): boolean {
// Initialize conversation data
const key = get_key_from_message(msg);
let topic_data = topics.get(key);
if (topic_data === undefined) {
topic_data = {
let conversation_data = conversations.get(key);
if (conversation_data === undefined) {
conversation_data = {
last_msg_id: -1,
participated: false,
type: msg.type,
};
topics.set(key, topic_data);
conversations.set(key, conversation_data);
conversation_data_updated = true;
}
// Update conversation data
if (topic_data.last_msg_id < msg.id) {
if (conversation_data.last_msg_id < msg.id) {
// 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.
topic_data.last_msg_id = msg.id;
conversation_data.last_msg_id = msg.id;
conversation_data_updated = true;
}
// TODO: Add backend support for participated topics.
@ -46,28 +46,30 @@ export function process_message(msg: Message): boolean {
// 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.
if (!topic_data.participated && people.is_my_user_id(msg.sender_id)) {
topic_data.participated = true;
if (!conversation_data.participated && people.is_my_user_id(msg.sender_id)) {
conversation_data.participated = true;
conversation_data_updated = true;
}
return conversation_data_updated;
}
function get_sorted_topics(): Map<string | undefined, TopicData> {
// Sort all recent topics by last message time.
return new Map([...topics.entries()].sort((a, b) => b[1].last_msg_id - a[1].last_msg_id));
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),
);
}
export function get_topics(): Map<string | undefined, TopicData> {
return get_sorted_topics();
export function get_conversations(): Map<string | undefined, ConversationData> {
return get_sorted_conversations();
}
export function reify_message_id_if_available(opts: {old_id: number; new_id: number}): boolean {
// We don't need to reify the message_id of the topic
// if a new message arrives in the topic from another user,
// since it replaces the last_msg_id of the topic which
// 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
// we were trying to reify.
for (const value of topics.values()) {
for (const value of conversations.values()) {
if (value.last_msg_id === opts.old_id) {
value.last_msg_id = opts.new_id;
return true;

View File

@ -89,7 +89,7 @@ const recent_conversation_key_prefix = "recent_conversation:";
export function clear_for_tests() {
filters.clear();
recent_view_data.topics.clear();
recent_view_data.conversations.clear();
topics_widget = undefined;
}
@ -313,7 +313,7 @@ export function get_focused_row_message() {
const $topic_rows = $("#recent_view_table table tbody tr");
const $topic_row = $topic_rows.eq(row_focus);
const conversation_id = $topic_row.attr("id").slice(recent_conversation_key_prefix.length);
const topic_last_msg_id = recent_view_data.topics.get(conversation_id).last_msg_id;
const topic_last_msg_id = recent_view_data.conversations.get(conversation_id).last_msg_id;
return message_store.get(topic_last_msg_id);
}
return undefined;
@ -339,9 +339,9 @@ export function revive_current_focus() {
if (last_visited_topic) {
// If the only message in the topic was deleted,
// then the topic will not be in Recent Conversations data.
if (recent_view_data.topics.get(last_visited_topic) !== undefined) {
if (recent_view_data.conversations.get(last_visited_topic) !== undefined) {
const topic_last_msg_id =
recent_view_data.topics.get(last_visited_topic).last_msg_id;
recent_view_data.conversations.get(last_visited_topic).last_msg_id;
const current_list = topics_widget.get_current_list();
const last_visited_topic_index = current_list.findIndex(
(topic) => topic.last_msg_id === topic_last_msg_id,
@ -600,7 +600,7 @@ function get_topic_row(topic_data) {
export function process_topic_edit(old_stream_id, old_topic, new_topic, new_stream_id) {
// See `recent_senders.process_topic_edit` for
// logic behind this and important notes on use of this function.
recent_view_data.topics.delete(recent_view_util.get_topic_key(old_stream_id, old_topic));
recent_view_data.conversations.delete(recent_view_util.get_topic_key(old_stream_id, old_topic));
const old_topic_msgs = message_util.get_messages_in_topic(old_stream_id, old_topic);
process_messages(old_topic_msgs);
@ -623,7 +623,7 @@ export function update_topics_of_deleted_message_ids(message_ids) {
const topics_to_rerender = message_util.get_topics_for_message_ids(message_ids);
for (const [stream_id, topic] of topics_to_rerender.values()) {
recent_view_data.topics.delete(recent_view_util.get_topic_key(stream_id, topic));
recent_view_data.conversations.delete(recent_view_util.get_topic_key(stream_id, topic));
const msgs = message_util.get_messages_in_topic(stream_id, topic);
process_messages(msgs);
}
@ -687,11 +687,11 @@ export function inplace_rerender(topic_key) {
if (!recent_view_util.is_visible()) {
return false;
}
if (!recent_view_data.topics.has(topic_key)) {
if (!recent_view_data.conversations.has(topic_key)) {
return false;
}
const topic_data = recent_view_data.topics.get(topic_key);
const topic_data = recent_view_data.conversations.get(topic_key);
const $topic_row = get_topic_row(topic_data);
// We cannot rely on `topic_widget.meta.filtered_list` to know
// if a topic is rendered since the `filtered_list` might have
@ -737,7 +737,7 @@ export function inplace_rerender(topic_key) {
export function update_topic_visibility_policy(stream_id, topic) {
const key = recent_view_util.get_topic_key(stream_id, topic);
if (!recent_view_data.topics.has(key)) {
if (!recent_view_data.conversations.has(key)) {
// we receive mute request for a topic we are
// not tracking currently
return false;
@ -941,7 +941,7 @@ export function complete_rerender() {
}
// Show topics list
const mapped_topic_values = [...recent_view_data.get_topics().values()];
const mapped_topic_values = [...recent_view_data.get_conversations().values()];
if (topics_widget) {
topics_widget.replace_list_data(mapped_topic_values);

View File

@ -952,7 +952,7 @@ test("basic assertions", ({mock_template, override_rewire}) => {
rt.set_default_focus();
rt.set_filter("all");
rt.process_messages(messages);
let all_topics = rt_data.get_topics();
let all_topics = rt_data.get_conversations();
// update a message
generate_topic_data([[1, "topic-7", 1, all_visibility_policies.INHERIT]]);
@ -1014,7 +1014,7 @@ test("basic assertions", ({mock_template, override_rewire}) => {
type: "private",
to_user_ids: "6,7,8",
});
all_topics = rt_data.get_topics();
all_topics = rt_data.get_conversations();
assert.equal(all_topics.size, 12);
assert.equal(
[...all_topics.keys()].toString(),
@ -1039,7 +1039,7 @@ test("basic assertions", ({mock_template, override_rewire}) => {
type: "stream",
});
all_topics = rt_data.get_topics();
all_topics = rt_data.get_conversations();
assert.equal(
[...all_topics.keys()].toString(),
"1:topic-3,6:topic-12,6:topic-11,6:topic-8,4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-2,1:topic-1,6,7,8",
@ -1056,7 +1056,7 @@ test("basic assertions", ({mock_template, override_rewire}) => {
type: "stream",
});
all_topics = rt_data.get_topics();
all_topics = rt_data.get_conversations();
assert.equal(
[...all_topics.keys()].toString(),
"1:topic-7,1:topic-3,6:topic-12,6:topic-11,6:topic-8,4:topic-10,1:topic-6,1:topic-5,1:topic-4,1:topic-2,1:topic-1,6,7,8",
@ -1134,14 +1134,14 @@ test("test_delete_messages", ({override}) => {
let reduced_msgs = messages.slice(1);
override(all_messages_data, "all_messages", () => reduced_msgs);
let all_topics = rt_data.get_topics();
let all_topics = rt_data.get_conversations();
assert.equal(
[...all_topics.keys()].toString(),
"6:topic-12,6:topic-11,6:topic-8,4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-3,1:topic-2,1:topic-1",
);
rt.update_topics_of_deleted_message_ids([messages[0].id]);
all_topics = rt_data.get_topics();
all_topics = rt_data.get_conversations();
assert.equal(
[...all_topics.keys()].toString(),
"6:topic-12,6:topic-11,6:topic-8,4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-3,1:topic-2",
@ -1152,7 +1152,7 @@ test("test_delete_messages", ({override}) => {
rt.update_topics_of_deleted_message_ids([messages[1].id, messages[2].id]);
all_topics = rt_data.get_topics();
all_topics = rt_data.get_conversations();
assert.equal(
[...all_topics.keys()].toString(),
"6:topic-12,6:topic-11,6:topic-8,4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-3",
@ -1172,7 +1172,7 @@ test("test_topic_edit", ({override}) => {
rt.set_filter("all");
rt.process_messages(messages);
let all_topics = rt_data.get_topics();
let all_topics = rt_data.get_conversations();
assert.equal(
[...all_topics.keys()].toString(),
"6:topic-12,6:topic-11,6:topic-8,4:topic-10,1:topic-7,1:topic-6,1:topic-5,1:topic-4,1:topic-3,1:topic-2,1:topic-1",
@ -1186,7 +1186,7 @@ test("test_topic_edit", ({override}) => {
messages[7].topic = topic8;
messages[8].topic = topic8;
rt.process_topic_edit(stream1, topic6, topic8);
all_topics = rt_data.get_topics();
all_topics = rt_data.get_conversations();
verify_topic_data(all_topics, stream1, topic8, messages[8].id, true);
assert.equal(all_topics.get(get_topic_key(stream1, topic6)), undefined);
@ -1197,7 +1197,7 @@ test("test_topic_edit", ({override}) => {
messages[0].stream_id = stream2;
rt.process_topic_edit(stream1, topic1, topic1, stream2);
all_topics = rt_data.get_topics();
all_topics = rt_data.get_conversations();
assert.equal(all_topics.get(get_topic_key(stream1, topic1)), undefined);
verify_topic_data(all_topics, stream2, topic1, messages[0].id, true);
@ -1209,7 +1209,7 @@ test("test_topic_edit", ({override}) => {
messages[0].stream_id = stream3;
messages[0].topic = topic9;
rt.process_topic_edit(stream2, topic1, topic9, stream3);
all_topics = rt_data.get_topics();
all_topics = rt_data.get_conversations();
assert.equal(all_topics.get(get_topic_key(stream2, topic1)), undefined);
verify_topic_data(all_topics, stream3, topic9, messages[0].id, true);
@ -1218,7 +1218,7 @@ test("test_topic_edit", ({override}) => {
messages[0].stream_id = stream5;
messages[0].topic = topic8;
rt.process_topic_edit(stream3, topic9, topic8, stream5);
all_topics = rt_data.get_topics();
all_topics = rt_data.get_conversations();
assert.equal(rt.filters_should_hide_topic(all_topics.get("5:topic-8")), true);
});