2021-02-10 16:50:28 +01:00
|
|
|
import * as pm_conversations from "./pm_conversations";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-02-10 16:50:28 +01:00
|
|
|
import * as stream_sort from "./stream_sort";
|
2021-02-28 00:54:32 +01:00
|
|
|
import * as stream_topic_history from "./stream_topic_history";
|
2021-02-28 21:30:38 +01:00
|
|
|
import * as unread from "./unread";
|
2022-08-14 15:33:13 +02:00
|
|
|
import * as user_topics from "./user_topics";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-10 16:50:28 +01:00
|
|
|
export function next_topic(streams, get_topics, has_unread_messages, curr_stream, curr_topic) {
|
2021-01-23 02:07:22 +01:00
|
|
|
const curr_stream_index = streams.indexOf(curr_stream); // -1 if not found
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2021-01-23 02:07:22 +01:00
|
|
|
if (curr_stream_index >= 0) {
|
|
|
|
const stream = streams[curr_stream_index];
|
|
|
|
const topics = get_topics(stream);
|
|
|
|
const curr_topic_index = topics.indexOf(curr_topic); // -1 if not found
|
2017-08-16 22:37:05 +02:00
|
|
|
|
2021-01-23 02:07:22 +01:00
|
|
|
for (let i = curr_topic_index + 1; i < topics.length; i += 1) {
|
|
|
|
const topic = topics[i];
|
|
|
|
if (has_unread_messages(stream, topic)) {
|
|
|
|
return {stream, topic};
|
2017-08-16 22:37:05 +02:00
|
|
|
}
|
2021-01-23 02:07:22 +01:00
|
|
|
}
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2021-01-23 02:07:22 +01:00
|
|
|
for (let i = 0; i < curr_topic_index; i += 1) {
|
|
|
|
const topic = topics[i];
|
|
|
|
if (has_unread_messages(stream, topic)) {
|
|
|
|
return {stream, topic};
|
2017-04-06 05:40:40 +02:00
|
|
|
}
|
2021-01-23 02:07:22 +01:00
|
|
|
}
|
2017-08-16 22:37:05 +02:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:07:22 +01:00
|
|
|
for (let i = curr_stream_index + 1; i < streams.length; i += 1) {
|
|
|
|
const stream = streams[i];
|
|
|
|
for (const topic of get_topics(stream)) {
|
|
|
|
if (has_unread_messages(stream, topic)) {
|
|
|
|
return {stream, topic};
|
2017-04-21 17:26:40 +02:00
|
|
|
}
|
2017-04-21 17:37:20 +02:00
|
|
|
}
|
2021-01-23 02:07:22 +01:00
|
|
|
}
|
2017-04-21 17:37:20 +02:00
|
|
|
|
2021-01-23 02:07:22 +01:00
|
|
|
for (let i = 0; i < curr_stream_index; i += 1) {
|
|
|
|
const stream = streams[i];
|
|
|
|
for (const topic of get_topics(stream)) {
|
|
|
|
if (has_unread_messages(stream, topic)) {
|
|
|
|
return {stream, topic};
|
|
|
|
}
|
2017-04-21 17:37:20 +02:00
|
|
|
}
|
2017-04-06 05:40:40 +02:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:07:22 +01:00
|
|
|
return undefined;
|
2021-02-10 16:50:28 +01:00
|
|
|
}
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2021-02-10 16:50:28 +01:00
|
|
|
export function get_next_topic(curr_stream, curr_topic) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let my_streams = stream_sort.get_streams();
|
2017-04-21 20:36:12 +02:00
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
my_streams = my_streams.filter((stream_name) => {
|
2019-05-21 09:33:21 +02:00
|
|
|
if (!stream_data.is_stream_muted_by_name(stream_name)) {
|
2017-08-17 10:27:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
2020-07-16 23:29:01 +02:00
|
|
|
if (stream_name === curr_stream) {
|
2017-08-17 10:27:47 +02:00
|
|
|
// We can use n within a muted stream if we are
|
|
|
|
// currently narrowed to it.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-05-17 16:58:06 +02:00
|
|
|
});
|
|
|
|
|
2017-05-17 16:33:41 +02:00
|
|
|
function get_unmuted_topics(stream_name) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = stream_data.get_stream_id(stream_name);
|
2020-03-22 18:40:05 +01:00
|
|
|
let topics = stream_topic_history.get_recent_topic_names(stream_id);
|
2022-08-14 15:33:13 +02:00
|
|
|
topics = topics.filter((topic) => !user_topics.is_topic_muted(stream_id, topic));
|
2017-05-17 16:33:41 +02:00
|
|
|
return topics;
|
2017-04-21 20:36:12 +02:00
|
|
|
}
|
|
|
|
|
2017-05-13 19:26:54 +02:00
|
|
|
function has_unread_messages(stream_name, topic) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = stream_data.get_stream_id(stream_name);
|
2017-05-13 19:26:54 +02:00
|
|
|
return unread.topic_has_any_unread(stream_id, topic);
|
2017-04-21 20:36:12 +02:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:50:28 +01:00
|
|
|
return next_topic(my_streams, get_unmuted_topics, has_unread_messages, curr_stream, curr_topic);
|
|
|
|
}
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2021-02-10 16:50:28 +01:00
|
|
|
export function get_next_unread_pm_string(curr_pm) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const my_pm_strings = pm_conversations.recent.get_strings();
|
2021-01-23 02:07:22 +01:00
|
|
|
const curr_pm_index = my_pm_strings.indexOf(curr_pm); // -1 if not found
|
2018-02-09 21:38:53 +01:00
|
|
|
|
2021-01-23 02:07:22 +01:00
|
|
|
for (let i = curr_pm_index + 1; i < my_pm_strings.length; i += 1) {
|
2022-10-22 07:15:44 +02:00
|
|
|
if (unread.num_unread_for_user_ids_string(my_pm_strings[i]) > 0) {
|
2021-01-23 02:07:22 +01:00
|
|
|
return my_pm_strings[i];
|
|
|
|
}
|
2018-02-09 21:38:53 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 02:07:22 +01:00
|
|
|
for (let i = 0; i < curr_pm_index; i += 1) {
|
2022-10-22 07:15:44 +02:00
|
|
|
if (unread.num_unread_for_user_ids_string(my_pm_strings[i]) > 0) {
|
2021-01-23 02:07:22 +01:00
|
|
|
return my_pm_strings[i];
|
|
|
|
}
|
|
|
|
}
|
2018-02-09 21:38:53 +01:00
|
|
|
|
2021-01-23 02:07:22 +01:00
|
|
|
return undefined;
|
2021-02-10 16:50:28 +01:00
|
|
|
}
|
2018-02-09 21:38:53 +01:00
|
|
|
|
2021-02-10 16:50:28 +01:00
|
|
|
export function get_next_stream(curr_stream) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const my_streams = stream_sort.get_streams();
|
2021-01-23 02:07:22 +01:00
|
|
|
const curr_stream_index = my_streams.indexOf(curr_stream);
|
|
|
|
return my_streams[
|
|
|
|
curr_stream_index < 0 || curr_stream_index === my_streams.length - 1
|
|
|
|
? 0
|
|
|
|
: curr_stream_index + 1
|
|
|
|
];
|
2021-02-10 16:50:28 +01:00
|
|
|
}
|
2017-08-16 19:06:07 +02:00
|
|
|
|
2021-02-10 16:50:28 +01:00
|
|
|
export function get_prev_stream(curr_stream) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const my_streams = stream_sort.get_streams();
|
2021-01-23 02:07:22 +01:00
|
|
|
const curr_stream_index = my_streams.indexOf(curr_stream);
|
|
|
|
return my_streams[curr_stream_index <= 0 ? my_streams.length - 1 : curr_stream_index - 1];
|
2021-02-10 16:50:28 +01:00
|
|
|
}
|