2021-02-28 00:44:37 +01:00
|
|
|
import {FoldDict} from "./fold_dict";
|
2021-02-28 01:10:31 +01:00
|
|
|
import * as message_util from "./message_util";
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2020-05-09 02:27:41 +02:00
|
|
|
// topic_senders[stream_id][topic_id][sender_id] = latest_message_id
|
|
|
|
const topic_senders = new Map();
|
|
|
|
// topic_senders[stream_id][sender_id] = latest_message_id
|
|
|
|
const stream_senders = new Map();
|
2017-06-01 07:42:57 +02:00
|
|
|
|
2021-02-28 00:44:37 +01:00
|
|
|
export function process_message_for_senders(message) {
|
2020-02-01 04:47:49 +01:00
|
|
|
const stream_id = message.stream_id;
|
2020-02-19 00:04:12 +01:00
|
|
|
const topic = message.topic;
|
2017-08-29 16:57:47 +02:00
|
|
|
|
|
|
|
// Process most recent sender to topic
|
2019-12-26 15:34:17 +01:00
|
|
|
const topic_dict = topic_senders.get(stream_id) || new FoldDict();
|
2020-05-09 02:27:41 +02:00
|
|
|
const topic_sender_message_ids = topic_dict.get(topic) || new Map();
|
|
|
|
let old_message_id = topic_sender_message_ids.get(message.sender_id);
|
2017-06-01 07:42:57 +02:00
|
|
|
|
2018-02-08 21:08:24 +01:00
|
|
|
if (old_message_id === undefined || old_message_id < message.id) {
|
2020-05-09 02:27:41 +02:00
|
|
|
topic_sender_message_ids.set(message.sender_id, message.id);
|
2017-06-01 07:42:57 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 02:27:41 +02:00
|
|
|
topic_dict.set(topic, topic_sender_message_ids);
|
2017-08-29 16:57:47 +02:00
|
|
|
topic_senders.set(stream_id, topic_dict);
|
|
|
|
|
|
|
|
// Process most recent sender to whole stream
|
2020-05-09 02:27:41 +02:00
|
|
|
const sender_message_ids = stream_senders.get(stream_id) || new Map();
|
2018-02-08 21:08:24 +01:00
|
|
|
old_message_id = sender_message_ids.get(message.sender_id);
|
2017-08-29 16:57:47 +02:00
|
|
|
|
2018-02-08 21:08:24 +01:00
|
|
|
if (old_message_id === undefined || old_message_id < message.id) {
|
|
|
|
sender_message_ids.set(message.sender_id, message.id);
|
2017-08-29 16:57:47 +02:00
|
|
|
}
|
|
|
|
|
2018-02-08 21:08:24 +01:00
|
|
|
stream_senders.set(stream_id, sender_message_ids);
|
2021-02-28 00:44:37 +01:00
|
|
|
}
|
2017-06-01 07:42:57 +02:00
|
|
|
|
2021-02-28 00:44:37 +01:00
|
|
|
export function process_topic_edit(old_stream_id, old_topic, new_topic, new_stream_id) {
|
2020-05-01 08:29:08 +02:00
|
|
|
// When topic-editing occurs, we need to update the set of known
|
|
|
|
// senders in each stream/topic pair. This is complicated by the
|
|
|
|
// fact that the event we receive from the server does not
|
|
|
|
// communicate which senders were present before-and-after; so our
|
|
|
|
// strategy is to just rebuild the data structure for the topic
|
|
|
|
// from message_store data.
|
|
|
|
|
|
|
|
// This removes the old topic_dict
|
|
|
|
const old_topic_dict = topic_senders.get(old_stream_id);
|
|
|
|
old_topic_dict.delete(old_topic);
|
|
|
|
|
|
|
|
// Re-processing every message in both the old and new topics is
|
|
|
|
// expensive. It also potentially loses data, because
|
|
|
|
// `message_list.all()` only has contiguous message history, not
|
|
|
|
// the complete set of message IDs we've received to the
|
|
|
|
// `message_store` from the server (E.g. from when we narrowed to
|
|
|
|
// a stream). But it's the most correct implementation we can
|
|
|
|
// sensibly do with existing data structures.
|
|
|
|
const old_topic_msgs = message_util.get_messages_in_topic(old_stream_id, old_topic);
|
|
|
|
for (const msg of old_topic_msgs) {
|
2021-02-28 00:44:37 +01:00
|
|
|
process_message_for_senders(msg);
|
2020-05-01 08:29:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// use new_stream_id if topic was moved to a new stream,
|
|
|
|
// otherwise we just use old_stream_id, implying that
|
|
|
|
// just topic was renamed.
|
|
|
|
new_stream_id = new_stream_id || old_stream_id;
|
|
|
|
const new_topic_msgs = message_util.get_messages_in_topic(new_stream_id, new_topic);
|
|
|
|
for (const msg of new_topic_msgs) {
|
2021-02-28 00:44:37 +01:00
|
|
|
process_message_for_senders(msg);
|
2020-05-01 08:29:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we don't delete anything from stream_senders here.
|
|
|
|
// Our view is that it's probably better to not do so; users who
|
|
|
|
// recently posted to a stream are relevant for typeahead even if
|
|
|
|
// the messages were moved to another stream or deleted.
|
2021-02-28 00:44:37 +01:00
|
|
|
}
|
2020-05-01 08:29:08 +02:00
|
|
|
|
2021-02-28 00:44:37 +01:00
|
|
|
export function update_topics_of_deleted_message_ids(message_ids) {
|
2020-08-07 09:15:47 +02:00
|
|
|
const topics_to_update = message_util.get_topics_for_message_ids(message_ids);
|
2020-07-15 09:36:03 +02:00
|
|
|
|
|
|
|
for (const [stream_id, topic] of topics_to_update.values()) {
|
|
|
|
const topic_dict = topic_senders.get(stream_id);
|
|
|
|
topic_dict.delete(topic);
|
|
|
|
const topic_msgs = message_util.get_messages_in_topic(stream_id, topic);
|
|
|
|
for (const msg of topic_msgs) {
|
2021-02-28 00:44:37 +01:00
|
|
|
process_message_for_senders(msg);
|
2020-07-15 09:36:03 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-28 00:44:37 +01:00
|
|
|
}
|
2020-07-15 09:36:03 +02:00
|
|
|
|
2021-02-28 00:44:37 +01:00
|
|
|
export function compare_by_recency(user_a, user_b, stream_id, topic) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let a_message_id;
|
|
|
|
let b_message_id;
|
2017-08-29 16:57:47 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const topic_dict = topic_senders.get(stream_id);
|
2017-07-28 19:39:44 +02:00
|
|
|
if (topic !== undefined && topic_dict !== undefined) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const sender_message_ids = topic_dict.get(topic);
|
2018-02-08 21:08:24 +01:00
|
|
|
if (sender_message_ids !== undefined) {
|
|
|
|
b_message_id = sender_message_ids.get(user_b.user_id) || Number.NEGATIVE_INFINITY;
|
|
|
|
a_message_id = sender_message_ids.get(user_a.user_id) || Number.NEGATIVE_INFINITY;
|
2017-08-29 16:57:47 +02:00
|
|
|
|
2018-02-08 21:08:24 +01:00
|
|
|
if (a_message_id !== b_message_id) {
|
|
|
|
return b_message_id - a_message_id;
|
2017-08-29 16:57:47 +02:00
|
|
|
}
|
2017-06-01 07:42:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 16:57:47 +02:00
|
|
|
// Check recency for whole stream as tiebreaker
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_dict = stream_senders.get(stream_id);
|
2017-08-29 16:57:47 +02:00
|
|
|
if (stream_dict !== undefined) {
|
2018-02-08 21:08:24 +01:00
|
|
|
b_message_id = stream_dict.get(user_b.user_id) || Number.NEGATIVE_INFINITY;
|
|
|
|
a_message_id = stream_dict.get(user_a.user_id) || Number.NEGATIVE_INFINITY;
|
2017-08-29 16:57:47 +02:00
|
|
|
|
2018-02-08 21:08:24 +01:00
|
|
|
if (a_message_id !== b_message_id) {
|
|
|
|
return b_message_id - a_message_id;
|
2017-08-29 16:57:47 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 19:39:44 +02:00
|
|
|
|
2017-06-01 07:42:57 +02:00
|
|
|
return 0;
|
2021-02-28 00:44:37 +01:00
|
|
|
}
|
2017-06-01 07:42:57 +02:00
|
|
|
|
2021-02-28 00:44:37 +01:00
|
|
|
export function get_topic_recent_senders(stream_id, topic) {
|
2020-05-01 08:29:08 +02:00
|
|
|
const topic_dict = topic_senders.get(stream_id);
|
|
|
|
if (topic_dict === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const sender_message_ids = topic_dict.get(topic);
|
|
|
|
if (sender_message_ids === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const sorted_senders = Array.from(sender_message_ids.entries()).sort((s1, s2) => s1[1] - s2[1]);
|
2020-05-01 08:29:08 +02:00
|
|
|
const recent_senders = [];
|
|
|
|
for (const item of sorted_senders) {
|
|
|
|
recent_senders.push(item[0]);
|
|
|
|
}
|
|
|
|
return recent_senders;
|
2021-02-28 00:44:37 +01:00
|
|
|
}
|