2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-08-18 19:53:22 +02:00
|
|
|
const pm_conversations = require("./pm_conversations");
|
|
|
|
|
2017-04-06 05:40:40 +02:00
|
|
|
exports.sub_list_generator = function (lst, lower, upper) {
|
|
|
|
// lower/upper has Python range semantics so if you pass
|
|
|
|
// in lower=5 and upper=8, you get elements 5/6/7
|
2019-11-02 00:06:25 +01:00
|
|
|
let i = lower;
|
2017-04-06 05:40:40 +02:00
|
|
|
|
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
next() {
|
2017-04-06 05:40:40 +02:00
|
|
|
if (i >= upper) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-04-06 05:40:40 +02:00
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = lst[i];
|
2017-04-06 05:40:40 +02:00
|
|
|
i += 1;
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-08-16 22:37:05 +02:00
|
|
|
exports.reverse_sub_list_generator = function (lst, lower, upper) {
|
|
|
|
// lower/upper has Python range semantics so if you pass
|
|
|
|
// in lower=5 and upper=8, you get elements 7/6/5
|
2019-11-02 00:06:25 +01:00
|
|
|
let i = upper - 1;
|
2017-08-16 22:37:05 +02:00
|
|
|
|
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
next() {
|
2017-08-16 22:37:05 +02:00
|
|
|
if (i < lower) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-08-16 22:37:05 +02:00
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = lst[i];
|
2017-08-16 22:37:05 +02:00
|
|
|
i -= 1;
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-04-06 05:40:40 +02:00
|
|
|
exports.list_generator = function (lst) {
|
|
|
|
return exports.sub_list_generator(lst, 0, lst.length);
|
|
|
|
};
|
|
|
|
|
2017-08-16 22:37:05 +02:00
|
|
|
exports.reverse_list_generator = function (lst) {
|
|
|
|
return exports.reverse_sub_list_generator(lst, 0, lst.length);
|
|
|
|
};
|
|
|
|
|
2017-04-06 05:40:40 +02:00
|
|
|
exports.fchain = function (outer_gen, get_inner_gen) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let outer_val = outer_gen.next();
|
|
|
|
let inner_gen;
|
2017-04-06 05:40:40 +02:00
|
|
|
|
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
next() {
|
2017-04-06 05:40:40 +02:00
|
|
|
while (outer_val !== undefined) {
|
|
|
|
if (inner_gen === undefined) {
|
|
|
|
inner_gen = get_inner_gen(outer_val);
|
|
|
|
if (!inner_gen || !inner_gen.next) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Invalid generator returned.");
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-04-06 05:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const inner = inner_gen.next();
|
2017-04-06 05:40:40 +02:00
|
|
|
if (inner !== undefined) {
|
|
|
|
return inner;
|
|
|
|
}
|
|
|
|
outer_val = outer_gen.next();
|
|
|
|
inner_gen = undefined;
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-04-06 05:40:40 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.chain = function (gen_lst) {
|
|
|
|
function get(which) {
|
|
|
|
return which;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const outer_gen = exports.list_generator(gen_lst);
|
2017-04-06 05:40:40 +02:00
|
|
|
|
|
|
|
return exports.fchain(outer_gen, get);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.wrap = function (lst, val) {
|
|
|
|
if (val === undefined) {
|
|
|
|
return exports.list_generator(lst);
|
|
|
|
}
|
|
|
|
|
2020-02-08 05:48:46 +01:00
|
|
|
const i = lst.indexOf(val);
|
2017-04-06 05:40:40 +02:00
|
|
|
if (i < 0) {
|
|
|
|
return exports.list_generator(lst);
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const inners = [
|
2018-05-06 21:43:17 +02:00
|
|
|
exports.sub_list_generator(lst, i, lst.length),
|
|
|
|
exports.sub_list_generator(lst, 0, i),
|
2017-04-06 05:40:40 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
return exports.chain(inners);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.wrap_exclude = function (lst, val) {
|
|
|
|
if (val === undefined) {
|
|
|
|
return exports.list_generator(lst);
|
|
|
|
}
|
|
|
|
|
2020-02-08 05:48:46 +01:00
|
|
|
const i = lst.indexOf(val);
|
2017-04-06 05:40:40 +02:00
|
|
|
if (i < 0) {
|
|
|
|
return exports.list_generator(lst);
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const inners = [
|
2018-06-04 21:13:07 +02:00
|
|
|
exports.sub_list_generator(lst, i + 1, lst.length),
|
2018-05-06 21:43:17 +02:00
|
|
|
exports.sub_list_generator(lst, 0, i),
|
2017-04-06 05:40:40 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
return exports.chain(inners);
|
|
|
|
};
|
|
|
|
|
2017-08-16 22:37:05 +02:00
|
|
|
exports.reverse_wrap_exclude = function (lst, val) {
|
|
|
|
if (val === undefined) {
|
|
|
|
return exports.reverse_list_generator(lst);
|
|
|
|
}
|
|
|
|
|
2020-02-08 05:48:46 +01:00
|
|
|
const i = lst.indexOf(val);
|
2017-08-16 22:37:05 +02:00
|
|
|
if (i < 0) {
|
|
|
|
return exports.reverse_list_generator(lst);
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const inners = [
|
2018-05-06 21:43:17 +02:00
|
|
|
exports.reverse_sub_list_generator(lst, 0, i),
|
2018-06-04 21:13:07 +02:00
|
|
|
exports.reverse_sub_list_generator(lst, i + 1, lst.length),
|
2017-08-16 22:37:05 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
return exports.chain(inners);
|
|
|
|
};
|
|
|
|
|
2017-04-06 05:40:40 +02:00
|
|
|
exports.filter = function (gen, filter_func) {
|
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
next() {
|
2017-04-06 05:40:40 +02:00
|
|
|
while (true) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const val = gen.next();
|
2017-04-06 05:40:40 +02:00
|
|
|
if (val === undefined) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-04-06 05:40:40 +02:00
|
|
|
}
|
|
|
|
if (filter_func(val)) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-04-21 17:26:40 +02:00
|
|
|
exports.map = function (gen, map_func) {
|
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
next() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const val = gen.next();
|
2017-04-21 17:26:40 +02:00
|
|
|
if (val === undefined) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-04-21 17:26:40 +02:00
|
|
|
}
|
|
|
|
return map_func(val);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-04-06 05:40:40 +02:00
|
|
|
exports.next_topic = function (streams, get_topics, has_unread_messages, curr_stream, curr_topic) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_gen = exports.wrap(streams, curr_stream);
|
2017-04-06 05:40:40 +02:00
|
|
|
|
|
|
|
function get_topic_gen(which_stream) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let gen;
|
2017-04-06 05:40:40 +02:00
|
|
|
|
|
|
|
if (which_stream === curr_stream) {
|
|
|
|
gen = exports.wrap_exclude(get_topics(which_stream), curr_topic);
|
|
|
|
} else {
|
|
|
|
gen = exports.list_generator(get_topics(which_stream));
|
|
|
|
}
|
2017-04-21 17:37:20 +02:00
|
|
|
|
|
|
|
function has_unread(topic) {
|
2017-04-06 05:40:40 +02:00
|
|
|
return has_unread_messages(which_stream, topic);
|
2017-04-21 17:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function make_object(topic) {
|
|
|
|
return {
|
|
|
|
stream: which_stream,
|
2020-07-20 22:18:43 +02:00
|
|
|
topic,
|
2017-04-21 17:37:20 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
gen = exports.filter(gen, has_unread);
|
|
|
|
gen = exports.map(gen, make_object);
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2017-04-21 17:37:20 +02:00
|
|
|
return gen;
|
2017-04-06 05:40:40 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const outer_gen = exports.fchain(stream_gen, get_topic_gen);
|
2017-04-06 05:40:40 +02:00
|
|
|
return outer_gen.next();
|
|
|
|
};
|
|
|
|
|
2017-04-21 20:36:12 +02:00
|
|
|
exports.get_next_topic = function (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);
|
2020-07-02 01:39:34 +02:00
|
|
|
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) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return exports.next_topic(
|
|
|
|
my_streams,
|
2017-05-17 16:33:41 +02:00
|
|
|
get_unmuted_topics,
|
2017-04-21 20:36:12 +02:00
|
|
|
has_unread_messages,
|
|
|
|
curr_stream,
|
2020-07-02 02:16:03 +02:00
|
|
|
curr_topic,
|
2017-04-21 20:36:12 +02:00
|
|
|
);
|
|
|
|
};
|
2017-04-06 05:40:40 +02:00
|
|
|
|
2018-02-09 21:38:53 +01:00
|
|
|
exports._get_pm_gen = function (curr_pm) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const my_pm_strings = pm_conversations.recent.get_strings();
|
|
|
|
const gen = exports.wrap_exclude(my_pm_strings, curr_pm);
|
2018-02-09 21:38:53 +01:00
|
|
|
return gen;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports._get_unread_pm_gen = function (curr_pm) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const pm_gen = exports._get_pm_gen(curr_pm);
|
2018-02-09 21:38:53 +01:00
|
|
|
|
|
|
|
function has_unread(user_ids_string) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const num_unread = unread.num_unread_for_person(user_ids_string);
|
2018-02-09 21:38:53 +01:00
|
|
|
return num_unread > 0;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const gen = exports.filter(pm_gen, has_unread);
|
2018-02-09 21:38:53 +01:00
|
|
|
return gen;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_next_unread_pm_string = function (curr_pm) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const gen = exports._get_unread_pm_gen(curr_pm);
|
2018-02-09 21:38:53 +01:00
|
|
|
return gen.next();
|
|
|
|
};
|
|
|
|
|
2017-08-16 19:06:07 +02:00
|
|
|
exports.get_next_stream = function (curr_stream) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const my_streams = stream_sort.get_streams();
|
|
|
|
const stream_gen = exports.wrap_exclude(my_streams, curr_stream);
|
2017-08-16 19:06:07 +02:00
|
|
|
return stream_gen.next();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_prev_stream = function (curr_stream) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const my_streams = stream_sort.get_streams();
|
|
|
|
const stream_gen = exports.reverse_wrap_exclude(my_streams, curr_stream);
|
2017-08-16 19:06:07 +02:00
|
|
|
return stream_gen.next();
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.topic_generator = exports;
|