js: Convert static/js/unread.js to ES6 module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-28 12:30:38 -08:00 committed by Tim Abbott
parent 9997e13032
commit 727208b84c
34 changed files with 123 additions and 124 deletions

View File

@ -152,7 +152,6 @@
"subs": false,
"ui": false,
"ui_init": false,
"unread": false,
"widgetize": false,
"zxcvbn": false
}

View File

@ -90,7 +90,6 @@ rewiremock.enable();
const huddle_data = zrequire("huddle_data");
const compose_fade = zrequire("compose_fade");
zrequire("unread");
const narrow = zrequire("narrow");
const presence = zrequire("presence");
const people = zrequire("people");

View File

@ -111,7 +111,6 @@ rewiremock.enable();
// For data-oriented modules, just use them, don't stub them.
const alert_words = zrequire("alert_words");
zrequire("unread");
const stream_topic_history = zrequire("stream_topic_history");
const stream_list = zrequire("stream_list");
const message_store = zrequire("message_store");

View File

@ -14,7 +14,6 @@ const page_params = set_global("page_params", {});
rewiremock.enable();
zrequire("unread");
const stream_data = zrequire("stream_data");
const people = zrequire("people");
const {Filter} = zrequire("Filter", "js/filter");

View File

@ -14,7 +14,6 @@ rewiremock("../../static/js/starred_messages").with({
rewiremock.enable();
zrequire("unread");
const message_flags = zrequire("message_flags");
const ui = set_global("ui", {});

View File

@ -13,7 +13,6 @@ set_global("setTimeout", (f, delay) => {
});
const muting = zrequire("muting");
zrequire("unread");
const {MessageListData} = zrequire("MessageListData", "js/message_list_data");
function make_msg(msg_id) {

View File

@ -16,7 +16,7 @@ const page_params = set_global("page_params", {
twenty_four_hour_time: false,
});
set_global("home_msg_list", "stub");
set_global("unread", {message_unread() {}});
rewiremock("../../static/js/unread").with({message_unread() {}});
// timerender calls setInterval when imported
rewiremock("../../static/js/timerender").with({
render_date(time1, time2) {

View File

@ -67,7 +67,6 @@ rewiremock.enable();
const util = zrequire("util");
const narrow_state = zrequire("narrow_state");
const stream_data = zrequire("stream_data");
zrequire("unread");
const narrow = zrequire("narrow");
const denmark = {

View File

@ -16,7 +16,8 @@ set_global("ui", {
rewiremock("../../static/js/stream_popover").with({
hide_topic_popover() {},
});
const unread = set_global("unread", {});
const unread = {__esModule: true};
rewiremock("../../static/js/unread").with(unread);
const unread_ui = {__esModule: true};
rewiremock("../../static/js/unread_ui").with(unread_ui);
const vdom = {

View File

@ -33,7 +33,7 @@ rewiremock("../../static/js/timerender").with({
time: "time",
}),
});
set_global("unread", {
rewiremock("../../static/js/unread").with({
unread_topic_counter: {
get: (stream_id, topic) => {
if (stream_id === 1 && topic === "topic-1") {

View File

@ -29,7 +29,6 @@ const narrow_state = zrequire("narrow_state");
const stream_data = zrequire("stream_data");
const stream_topic_history = zrequire("stream_topic_history");
const people = zrequire("people");
zrequire("unread");
const search = zrequire("search_suggestion");
search.__Rewire__("max_num_of_search_results", 15);

View File

@ -27,7 +27,6 @@ const narrow_state = zrequire("narrow_state");
const stream_data = zrequire("stream_data");
const stream_topic_history = zrequire("stream_topic_history");
const people = zrequire("people");
zrequire("unread");
const search = zrequire("search_suggestion");
search.__Rewire__("max_num_of_search_results", 15);

View File

@ -100,8 +100,9 @@ run_test("topics", () => {
stream_data.__Rewire__("is_stream_muted_by_name", (stream_name) => stream_name === "muted");
unread.topic_has_any_unread = (stream_id) =>
[devel_stream_id, muted_stream_id].includes(stream_id);
unread.__Rewire__("topic_has_any_unread", (stream_id) =>
[devel_stream_id, muted_stream_id].includes(stream_id),
);
muting.__Rewire__("is_topic_muted", (stream_name, topic) => topic === "muted");
@ -121,7 +122,7 @@ run_test("topics", () => {
run_test("get_next_unread_pm_string", () => {
pm_conversations.recent.get_strings = () => ["1", "read", "2,3", "4", "unk"];
unread.num_unread_for_person = (user_ids_string) => {
unread.__Rewire__("num_unread_for_person", (user_ids_string) => {
if (user_ids_string === "unk") {
return undefined;
}
@ -131,7 +132,7 @@ run_test("get_next_unread_pm_string", () => {
}
return 5; // random non-zero value
};
});
assert.equal(tg.get_next_unread_pm_string(), "1");
assert.equal(tg.get_next_unread_pm_string("4"), "1");
@ -141,7 +142,7 @@ run_test("get_next_unread_pm_string", () => {
assert.equal(tg.get_next_unread_pm_string("read"), "2,3");
assert.equal(tg.get_next_unread_pm_string("2,3"), "4");
unread.num_unread_for_person = () => 0;
unread.__Rewire__("num_unread_for_person", () => 0);
assert.equal(tg.get_next_unread_pm_string("2,3"), undefined);
});

View File

@ -111,7 +111,6 @@ rewiremock.enable();
const util = zrequire("util");
zrequire("unread");
const upload = zrequire("upload");
const compose = zrequire("compose");
zrequire("composebox_typeahead");

View File

@ -3,6 +3,7 @@ import * as hash_util from "./hash_util";
import * as people from "./people";
import * as presence from "./presence";
import * as timerender from "./timerender";
import * as unread from "./unread";
import * as user_status from "./user_status";
import * as util from "./util";

View File

@ -16,7 +16,6 @@ import "flatpickr/dist/plugins/confirmDate/confirmDate";
import "../i18n";
import "../fold_dict";
import "../setup";
import "../unread";
import "../stream_list";
import "../widgetize";
import "../message_list";

View File

@ -5,6 +5,7 @@ import * as message_store from "./message_store";
import * as message_util from "./message_util";
import * as people from "./people";
import * as stream_data from "./stream_data";
import * as unread from "./unread";
import * as util from "./util";
function zephyr_stream_name_match(message, operand) {

View File

@ -21,7 +21,6 @@ declare let settings_profile_fields: any;
declare let stream_list: any;
declare let subs: any;
declare let ui: any;
declare let unread: any;
declare let widgetize: any;
declare let zulip_test: any;

View File

@ -18,6 +18,7 @@ const recent_topics = require("./recent_topics");
const resize = require("./resize");
const stream_data = require("./stream_data");
const stream_topic_history = require("./stream_topic_history");
const unread = require("./unread");
const unread_ops = require("./unread_ops");
const unread_ui = require("./unread_ui");
const util = require("./util");

View File

@ -3,6 +3,7 @@ import _ from "lodash";
import {FetchStatus} from "./fetch_status";
import {Filter} from "./filter";
import * as muting from "./muting";
import * as unread from "./unread";
import * as util from "./util";
export class MessageListData {

View File

@ -1,6 +1,7 @@
import * as loading from "./loading";
import * as message_store from "./message_store";
import * as resize from "./resize";
import * as unread from "./unread";
import * as unread_ui from "./unread_ui";
export function do_unread_count_updates(messages) {

View File

@ -27,6 +27,7 @@ const top_left_corner = require("./top_left_corner");
const topic_generator = require("./topic_generator");
const typing_events = require("./typing_events");
const ui_util = require("./ui_util");
const unread = require("./unread");
const unread_ops = require("./unread_ops");
const util = require("./util");

View File

@ -1,6 +1,7 @@
import {Filter} from "./filter";
import * as people from "./people";
import * as stream_data from "./stream_data";
import * as unread from "./unread";
let current_filter;

View File

@ -15,6 +15,7 @@ import * as settings_config from "./settings_config";
import * as spoilers from "./spoilers";
import * as stream_data from "./stream_data";
import * as stream_ui_updates from "./stream_ui_updates";
import * as unread from "./unread";
import * as unread_ops from "./unread_ops";
const notice_memory = new Map();

View File

@ -5,6 +5,7 @@ import * as people from "./people";
import * as pm_conversations from "./pm_conversations";
import * as pm_list_dom from "./pm_list_dom";
import * as stream_popover from "./stream_popover";
import * as unread from "./unread";
import * as unread_ui from "./unread_ui";
import * as vdom from "./vdom";

View File

@ -18,6 +18,7 @@ import * as recent_senders from "./recent_senders";
import * as stream_data from "./stream_data";
import * as timerender from "./timerender";
import * as top_left_corner from "./top_left_corner";
import * as unread from "./unread";
const topics = new Map(); // Key is stream-id:topic.
let topics_widget;

View File

@ -18,6 +18,7 @@ const stream_popover = require("./stream_popover");
const stream_sort = require("./stream_sort");
const topic_list = require("./topic_list");
const topic_zoom = require("./topic_zoom");
const unread = require("./unread");
let has_scrolled = false;

View File

@ -2,6 +2,7 @@ import * as channel from "./channel";
import {FoldDict} from "./fold_dict";
import * as message_util from "./message_util";
import * as stream_data from "./stream_data";
import * as unread from "./unread";
const stream_dict = new Map(); // stream_id -> PerStreamHistory object
const fetched_stream_ids = new Set();

View File

@ -3,6 +3,7 @@ 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

View File

@ -2,6 +2,7 @@ import * as hash_util from "./hash_util";
import * as muting from "./muting";
import * as narrow_state from "./narrow_state";
import * as stream_topic_history from "./stream_topic_history";
import * as unread from "./unread";
const max_topics = 5;
const max_topics_with_unread = 8;

View File

@ -62,6 +62,7 @@ import * as topic_zoom from "./topic_zoom";
import * as tutorial from "./tutorial";
import * as typing from "./typing";
import * as ui_util from "./ui_util";
import * as unread from "./unread";
import * as unread_ui from "./unread_ui";
import * as user_groups from "./user_groups";
import * as user_status from "./user_status";

View File

@ -1,12 +1,10 @@
"use strict";
const {FoldDict} = require("./fold_dict");
const message_store = require("./message_store");
const muting = require("./muting");
const people = require("./people");
const settings_notifications = require("./settings_notifications");
const stream_data = require("./stream_data");
const util = require("./util");
import {FoldDict} from "./fold_dict";
import * as message_store from "./message_store";
import * as muting from "./muting";
import * as people from "./people";
import * as settings_notifications from "./settings_notifications";
import * as stream_data from "./stream_data";
import * as util from "./util";
// The unread module tracks the message IDs and locations of the
// user's unread messages. The tracking is initialized with
@ -20,10 +18,11 @@ const util = require("./util");
// See https://zulip.readthedocs.io/en/latest/subsystems/pointer.html
// for more details on how this system is designed.
exports.messages_read_in_narrow = false;
exports.set_messages_read_in_narrow = function (value) {
exports.messages_read_in_narrow = value;
};
export let messages_read_in_narrow = false;
export function set_messages_read_in_narrow(value) {
messages_read_in_narrow = value;
}
const unread_messages = new Set();
@ -184,7 +183,7 @@ class UnreadPMCounter {
return util.sorted_ids(ids);
}
}
exports.unread_pm_counter = new UnreadPMCounter();
export const unread_pm_counter = new UnreadPMCounter();
function make_per_stream_bucketer() {
return new Bucketer({
@ -370,26 +369,25 @@ class UnreadTopicCounter {
return id_set.size !== 0;
}
}
exports.unread_topic_counter = new UnreadTopicCounter();
export const unread_topic_counter = new UnreadTopicCounter();
export const unread_mentions_counter = new Set();
exports.unread_mentions_counter = new Set();
exports.message_unread = function (message) {
export function message_unread(message) {
if (message === undefined) {
return false;
}
return message.unread;
};
}
exports.get_unread_message_ids = function (message_ids) {
export function get_unread_message_ids(message_ids) {
return message_ids.filter((message_id) => unread_messages.has(message_id));
};
}
exports.get_unread_messages = function (messages) {
export function get_unread_messages(messages) {
return messages.filter((message) => unread_messages.has(message.id));
};
}
exports.update_unread_topics = function (msg, event) {
export function update_unread_topics(msg, event) {
const new_topic = util.get_edit_event_topic(event);
const {new_stream_id} = event;
@ -401,16 +399,12 @@ exports.update_unread_topics = function (msg, event) {
return;
}
exports.unread_topic_counter.delete(msg.id);
unread_topic_counter.delete(msg.id);
exports.unread_topic_counter.add(
new_stream_id || msg.stream_id,
new_topic || msg.topic,
msg.id,
);
};
unread_topic_counter.add(new_stream_id || msg.stream_id, new_topic || msg.topic, msg.id);
}
exports.process_loaded_messages = function (messages) {
export function process_loaded_messages(messages) {
for (const message of messages) {
if (!message.unread) {
continue;
@ -419,20 +413,20 @@ exports.process_loaded_messages = function (messages) {
unread_messages.add(message.id);
if (message.type === "private") {
exports.unread_pm_counter.add(message);
unread_pm_counter.add(message);
}
if (message.type === "stream") {
exports.unread_topic_counter.add(message.stream_id, message.topic, message.id);
unread_topic_counter.add(message.stream_id, message.topic, message.id);
}
exports.update_message_for_mention(message);
update_message_for_mention(message);
}
}
};
exports.update_message_for_mention = function (message) {
export function update_message_for_mention(message) {
if (!message.unread) {
exports.unread_mentions_counter.delete(message.id);
unread_mentions_counter.delete(message.id);
return;
}
@ -442,58 +436,58 @@ exports.update_message_for_mention = function (message) {
!muting.is_topic_muted(message.stream_id, message.topic);
if (is_unmuted_mention || message.mentioned_me_directly) {
exports.unread_mentions_counter.add(message.id);
unread_mentions_counter.add(message.id);
} else {
exports.unread_mentions_counter.delete(message.id);
unread_mentions_counter.delete(message.id);
}
}
};
exports.mark_as_read = function (message_id) {
export function mark_as_read(message_id) {
// We don't need to check anything about the message, since all
// the following methods are cheap and work fine even if message_id
// was never set to unread.
exports.unread_pm_counter.delete(message_id);
exports.unread_topic_counter.delete(message_id);
exports.unread_mentions_counter.delete(message_id);
unread_pm_counter.delete(message_id);
unread_topic_counter.delete(message_id);
unread_mentions_counter.delete(message_id);
unread_messages.delete(message_id);
const message = message_store.get(message_id);
if (message) {
message.unread = false;
}
};
}
exports.declare_bankruptcy = function () {
exports.unread_pm_counter.clear();
exports.unread_topic_counter.clear();
exports.unread_mentions_counter.clear();
export function declare_bankruptcy() {
unread_pm_counter.clear();
unread_topic_counter.clear();
unread_mentions_counter.clear();
unread_messages.clear();
};
}
exports.get_counts = function () {
export function get_counts() {
const res = {};
// Return a data structure with various counts. This function should be
// pretty cheap, even if you don't care about all the counts, and you
// should strive to keep it free of side effects on globals or DOM.
res.private_message_count = 0;
res.mentioned_message_count = exports.unread_mentions_counter.size;
res.mentioned_message_count = unread_mentions_counter.size;
// This sets stream_count, topic_count, and home_unread_messages
const topic_res = exports.unread_topic_counter.get_counts();
const topic_res = unread_topic_counter.get_counts();
res.home_unread_messages = topic_res.stream_unread_messages;
res.stream_count = topic_res.stream_count;
const pm_res = exports.unread_pm_counter.get_counts();
const pm_res = unread_pm_counter.get_counts();
res.pm_count = pm_res.pm_dict;
res.private_message_count = pm_res.total_count;
res.home_unread_messages += pm_res.total_count;
return res;
};
}
// Saves us from calling to get_counts() when we can avoid it.
exports.calculate_notifiable_count = function (res) {
export function calculate_notifiable_count(res) {
let new_message_count = 0;
const only_show_notifiable =
@ -513,78 +507,78 @@ exports.calculate_notifiable_count = function (res) {
new_message_count = res.home_unread_messages;
}
return new_message_count;
};
}
exports.get_notifiable_count = function () {
const res = exports.get_counts();
return exports.calculate_notifiable_count(res);
};
export function get_notifiable_count() {
const res = get_counts();
return calculate_notifiable_count(res);
}
exports.num_unread_for_stream = function (stream_id) {
return exports.unread_topic_counter.get_stream_count(stream_id);
};
export function num_unread_for_stream(stream_id) {
return unread_topic_counter.get_stream_count(stream_id);
}
exports.num_unread_for_topic = function (stream_id, topic_name) {
return exports.unread_topic_counter.get(stream_id, topic_name);
};
export function num_unread_for_topic(stream_id, topic_name) {
return unread_topic_counter.get(stream_id, topic_name);
}
exports.topic_has_any_unread = function (stream_id, topic) {
return exports.unread_topic_counter.topic_has_any_unread(stream_id, topic);
};
export function topic_has_any_unread(stream_id, topic) {
return unread_topic_counter.topic_has_any_unread(stream_id, topic);
}
exports.num_unread_for_person = function (user_ids_string) {
return exports.unread_pm_counter.num_unread(user_ids_string);
};
export function num_unread_for_person(user_ids_string) {
return unread_pm_counter.num_unread(user_ids_string);
}
exports.get_msg_ids_for_stream = function (stream_id) {
return exports.unread_topic_counter.get_msg_ids_for_stream(stream_id);
};
export function get_msg_ids_for_stream(stream_id) {
return unread_topic_counter.get_msg_ids_for_stream(stream_id);
}
exports.get_msg_ids_for_topic = function (stream_id, topic_name) {
return exports.unread_topic_counter.get_msg_ids_for_topic(stream_id, topic_name);
};
export function get_msg_ids_for_topic(stream_id, topic_name) {
return unread_topic_counter.get_msg_ids_for_topic(stream_id, topic_name);
}
exports.get_msg_ids_for_person = function (user_ids_string) {
return exports.unread_pm_counter.get_msg_ids_for_person(user_ids_string);
};
export function get_msg_ids_for_person(user_ids_string) {
return unread_pm_counter.get_msg_ids_for_person(user_ids_string);
}
exports.get_msg_ids_for_private = function () {
return exports.unread_pm_counter.get_msg_ids();
};
export function get_msg_ids_for_private() {
return unread_pm_counter.get_msg_ids();
}
exports.get_msg_ids_for_mentions = function () {
const ids = Array.from(exports.unread_mentions_counter);
export function get_msg_ids_for_mentions() {
const ids = Array.from(unread_mentions_counter);
return util.sorted_ids(ids);
};
}
exports.get_all_msg_ids = function () {
export function get_all_msg_ids() {
const ids = Array.from(unread_messages);
return util.sorted_ids(ids);
};
}
exports.get_missing_topics = function (opts) {
return exports.unread_topic_counter.get_missing_topics(opts);
};
export function get_missing_topics(opts) {
return unread_topic_counter.get_missing_topics(opts);
}
exports.get_msg_ids_for_starred = function () {
export function get_msg_ids_for_starred() {
// This is here for API consistency sake--we never
// have unread starred messages. (Some day we may ironically
// want to make starring the same as mark-as-unread, but
// for now starring === reading.)
return [];
};
}
exports.initialize = function () {
export function initialize() {
const unread_msgs = page_params.unread_msgs;
exports.unread_pm_counter.set_huddles(unread_msgs.huddles);
exports.unread_pm_counter.set_pms(unread_msgs.pms);
exports.unread_topic_counter.set_streams(unread_msgs.streams);
unread_pm_counter.set_huddles(unread_msgs.huddles);
unread_pm_counter.set_pms(unread_msgs.pms);
unread_topic_counter.set_streams(unread_msgs.streams);
for (const message_id of unread_msgs.mentions) {
exports.unread_mentions_counter.add(message_id);
unread_mentions_counter.add(message_id);
}
for (const obj of unread_msgs.huddles) {
@ -608,6 +602,4 @@ exports.initialize = function () {
for (const message_id of unread_msgs.mentions) {
unread_messages.add(message_id);
}
};
window.unread = exports;
}

View File

@ -6,6 +6,7 @@ import * as notifications from "./notifications";
import * as overlays from "./overlays";
import * as recent_topics from "./recent_topics";
import * as reload from "./reload";
import * as unread from "./unread";
import * as unread_ui from "./unread_ui";
export function mark_all_as_read() {

View File

@ -3,6 +3,7 @@ import * as notifications from "./notifications";
import * as pm_list from "./pm_list";
import * as top_left_corner from "./top_left_corner";
import * as topic_list from "./topic_list";
import * as unread from "./unread";
let last_mention_count = 0;