zulip/static/js/topic_generator.js

116 lines
3.8 KiB
JavaScript
Raw Normal View History

import * as muting from "./muting";
import * as pm_conversations from "./pm_conversations";
import * as stream_data from "./stream_data";
import * as stream_sort from "./stream_sort";
import * as stream_topic_history from "./stream_topic_history";
import * as unread from "./unread";
export function next_topic(streams, get_topics, has_unread_messages, curr_stream, curr_topic) {
const curr_stream_index = streams.indexOf(curr_stream); // -1 if not found
2017-04-06 05:40:40 +02: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
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-04-06 05:40:40 +02: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
}
}
}
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
}
}
}
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-06 05:40:40 +02:00
}
return undefined;
}
2017-04-06 05:40:40 +02:00
export function get_next_topic(curr_stream, curr_topic) {
let my_streams = stream_sort.get_streams();
2017-04-21 20:36:12 +02:00
my_streams = my_streams.filter((stream_name) => {
if (!stream_data.is_stream_muted_by_name(stream_name)) {
return true;
}
if (stream_name === curr_stream) {
// 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) {
const stream_id = stream_data.get_stream_id(stream_name);
let topics = stream_topic_history.get_recent_topic_names(stream_id);
topics = topics.filter((topic) => !muting.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) {
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
}
return next_topic(my_streams, get_unmuted_topics, has_unread_messages, curr_stream, curr_topic);
}
2017-04-06 05:40:40 +02:00
export function get_next_unread_pm_string(curr_pm) {
const my_pm_strings = pm_conversations.recent.get_strings();
const curr_pm_index = my_pm_strings.indexOf(curr_pm); // -1 if not found
for (let i = curr_pm_index + 1; i < my_pm_strings.length; i += 1) {
if (unread.num_unread_for_person(my_pm_strings[i]) > 0) {
return my_pm_strings[i];
}
}
for (let i = 0; i < curr_pm_index; i += 1) {
if (unread.num_unread_for_person(my_pm_strings[i]) > 0) {
return my_pm_strings[i];
}
}
return undefined;
}
export function get_next_stream(curr_stream) {
const my_streams = stream_sort.get_streams();
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
];
}
export function get_prev_stream(curr_stream) {
const my_streams = stream_sort.get_streams();
const curr_stream_index = my_streams.indexOf(curr_stream);
return my_streams[curr_stream_index <= 0 ? my_streams.length - 1 : curr_stream_index - 1];
}