Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

typing_notifications: Frontend work to display notifs for streams.

Dineshfccdc054a11e
diff --git a/web/src/typing_data.ts b/web/src/typing_data.ts
index d92f7027ca9..284dd0af9e8 100644
--- a/web/src/typing_data.ts
+++ b/web/src/typing_data.ts
@@ -13,7 +13,12 @@ export function clear_for_testing(): void {
export function get_direct_message_conversation_key(group: number[]): string {
const ids = util.sorted_ids(group);
- return ids.join(",");
+ return "direct:" + ids.join(",");
+}
+
+export function get_topic_key(stream_id: number, topic: string): string {
+ topic = topic.toLowerCase(); // Topics are case-insensitive
+ return "topic:" + JSON.stringify({stream_id, topic});
}
export function add_typist(key: string, typist: number): void {
@@ -44,11 +49,20 @@ export function get_group_typists(group: number[]): number[] {
}
export function get_all_direct_message_typists(): number[] {
- let typists = [...typists_dict.values()].flat();
+ let typists: number[] = [];
+ for (const [key, value] of typists_dict) {
+ if (key.startsWith("direct:")) {
+ typists.push(...value);
+ }
+ }
typists = util.sorted_ids(typists);
return muted_users.filter_muted_user_ids(typists);
}
+export function get_topic_typists(stream_id: number, topic: string): number[] {
+ return typists_dict.get(get_topic_key(stream_id, topic)) ?? [];
+}
+
// The next functions aren't pure data, but it is easy
// enough to mock the setTimeout/clearTimeout functions.
export function clear_inbound_timer(key: string): void {
diff --git a/web/src/typing_events.js b/web/src/typing_events.js
index 3ce0aa4fe3b..545ee8a5162 100644
--- a/web/src/typing_events.js
+++ b/web/src/typing_events.js
@@ -2,6 +2,7 @@ import $ from "jquery";
import render_typing_notifications from "../templates/typing_notifications.hbs";
+import * as blueslip from "./blueslip";
import * as narrow_state from "./narrow_state";
import {page_params} from "./page_params";
import * as people from "./people";
@@ -27,8 +28,12 @@ const MAX_USERS_TO_DISPLAY_NAME = 3;
// that make typing indicators work.
function get_users_typing_for_narrow() {
+ if (narrow_state.narrowed_to_topic()) {
+ return typing_data.get_topic_typists(narrow_state.stream_id(), narrow_state.topic());
+ }
+
if (!narrow_state.narrowed_to_pms()) {
- // Narrow is neither "dm:" nor "is:dm".
+ // Narrow is neither "dm:" nor "is:dm" nor topic.
return [];
}
@@ -69,14 +74,31 @@ export function render_notifications_for_narrow() {
}
}
-export function hide_notification(event) {
- const recipients = event.recipients.map((user) => user.user_id);
- recipients.sort();
+function get_key(event) {
+ let key;
+ let recipients;
+ switch (event.message_type) {
+ case "stream":
+ key = typing_data.get_topic_key(event.stream_id, event.topic);
+ break;
+ case "direct":
+ recipients = event.recipients.map((user) => user.user_id);
+ recipients.sort();
+
+ key = typing_data.get_direct_message_conversation_key(recipients);
+ break;
+ default:
+ blueslip.error("Unexpected message_type in typing event: " + event.message_type);
+ break;
+ }
+ return key;
+}
- const conversation_key = typing_data.get_direct_message_conversation_key(recipients);
- typing_data.clear_inbound_timer(conversation_key);
+export function hide_notification(event) {
+ const key = get_key(event);
+ typing_data.clear_inbound_timer(key);
- const removed = typing_data.remove_typist(conversation_key, event.sender.user_id);
+ const removed = typing_data.remove_typist(key, event.sender.user_id);
if (removed) {
render_notifications_for_narrow();
@@ -84,18 +106,15 @@ export function hide_notification(event) {
}
export function display_notification(event) {
- const recipients = event.recipients.map((user) => user.user_id);
- recipients.sort();
-
const sender_id = event.sender.user_id;
event.sender.name = people.get_by_user_id(sender_id).full_name;
- const conversation_key = typing_data.get_direct_message_conversation_key(recipients);
- typing_data.add_typist(conversation_key, sender_id);
+ const key = get_key(event);
+ typing_data.add_typist(key, sender_id);
render_notifications_for_narrow();
- typing_data.kickstart_inbound_timer(conversation_key, typing_started_expiry_period, () => {
+ typing_data.kickstart_inbound_timer(key, typing_started_expiry_period, () => {
hide_notification(event);
});
}
diff --git a/web/tests/dispatch.test.js b/web/tests/dispatch.test.js
index 7f796b52b85..8b1621d30ef 100644
--- a/web/tests/dispatch.test.js
+++ b/web/tests/dispatch.test.js
@@ -819,6 +819,37 @@ run_test("typing", ({override}) => {
page_params.user_id = typing_person1.user_id;
event = event_fixtures.typing__start;
dispatch(event);
+ page_params.user_id = undefined; // above change shouldn't effect stream_typing tests below
+});
+
+run_test("stream_typing", ({override}) => {
+ const stream_typing_in_id = events.stream_typing_in_id;
+ const topic_typing_in = events.topic_typing_in;
+ let event = event_fixtures.stream_typing__start;
+ {
+ const stub = make_stub();
+ override(typing_events, "display_notification", stub.f);
+ dispatch(event);
+ assert.equal(stub.num_calls, 1);
+ const args = stub.get_args("event");
+ assert_same(args.event.sender.user_id, typing_person1.user_id);
+ assert_same(args.event.message_type, "stream");
+ assert_same(args.event.stream_id, stream_typing_in_id);
+ assert_same(args.event.topic, topic_typing_in);
+ }
+
+ event = event_fixtures.stream_typing__stop;
+ {
+ const stub = make_stub();
+ override(typing_events, "hide_notification", stub.f);
+ dispatch(event);
+ assert.equal(stub.num_calls, 1);
+ const args = stub.get_args("event");
+ assert_same(args.event.sender.user_id, typing_person1.user_id);
+ assert_same(args.event.message_type, "stream");
+ assert_same(args.event.stream_id, stream_typing_in_id);
+ assert_same(args.event.topic, topic_typing_in);
+ }
});
run_test("user_settings", ({override}) => {
diff --git a/web/tests/lib/events.js b/web/tests/lib/events.js
index fa27815d219..8b2eebad20d 100644
--- a/web/tests/lib/events.js
+++ b/web/tests/lib/events.js
@@ -32,6 +32,8 @@ const typing_person2 = {
exports.typing_person1 = typing_person1;
exports.typing_person2 = typing_person2;
+exports.stream_typing_in_id = 1;
+exports.topic_typing_in = "Typing topic";
const fake_then = 1596710000;
const fake_now = 1596713966;
@@ -689,6 +691,24 @@ exports.fixtures = {
value: "blue",
},
+ stream_typing__start: {
+ type: "typing",
+ op: "start",
+ message_type: "stream",
+ sender: typing_person1,
+ stream_id: this.stream_typing_in_id,
+ topic: this.topic_typing_in,
+ },
+
+ stream_typing__stop: {
+ type: "typing",
+ op: "stop",
+ message_type: "stream",
+ sender: typing_person1,
+ stream_id: this.stream_typing_in_id,
+ topic: this.topic_typing_in,
+ },
+
submessage: {
type: "submessage",
submessage_id: 99,
diff --git a/web/tests/typing_data.test.js b/web/tests/typing_data.test.js
index 38bc2ee3b3e..391350ae7f3 100644
--- a/web/tests/typing_data.test.js
+++ b/web/tests/typing_data.test.js
@@ -20,9 +20,17 @@ test("basics", () => {
// The typing_data needs to be robust with lists of
// user ids being in arbitrary sorting order. So all
// the apparent randomness in these tests has a purpose.
+
+ const stream_id = 1;
+ const topic = "typing notifications";
+ const topic_typing_key = typing_data.get_topic_key(stream_id, topic);
+
typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 15);
assert.deepEqual(typing_data.get_group_typists([15, 10, 5]), [15]);
+ typing_data.add_typist(topic_typing_key, 12);
+ assert.deepEqual(typing_data.get_topic_typists(stream_id, topic), [12]);
+
// test that you can add twice
typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 15);
@@ -30,6 +38,12 @@ test("basics", () => {
typing_data.add_typist(typing_data.get_direct_message_conversation_key([5, 10, 15]), 10);
assert.deepEqual(typing_data.get_group_typists([10, 15, 5]), [10, 15]);
+ typing_data.add_typist(topic_typing_key, 12);
+
+ // add another typist to our stream/topic
+ typing_data.add_typist(topic_typing_key, 13);
+ assert.deepEqual(typing_data.get_topic_typists(stream_id, topic), [12, 13]);
+
// start adding to a new group
typing_data.add_typist(typing_data.get_direct_message_conversation_key([7, 15]), 7);
typing_data.add_typist(typing_data.get_direct_message_conversation_key([7, 15]), 15);
@@ -42,6 +56,8 @@ test("basics", () => {
typing_data.remove_typist(typing_data.get_direct_message_conversation_key([15, 7]), 7),
);
assert.deepEqual(typing_data.get_group_typists([7, 15]), [15]);
+ assert.ok(typing_data.remove_typist(topic_typing_key, 12));
+ assert.deepEqual(typing_data.get_topic_typists(stream_id, topic), [13]);
// test removing an id that is not there
assert.ok(
@@ -89,6 +105,9 @@ test("timers", () => {
const stub_group = [5, 10, 15];
const stub_delay = 99;
const stub_f = "function";
+ const stub_stream_id = 1;
+ const stub_topic = "typing notifications";
+ const topic_typing_key = typing_data.get_topic_key(stub_stream_id, stub_topic);
function set_timeout(f, delay) {
assert.equal(delay, stub_delay);
@@ -124,6 +143,16 @@ test("timers", () => {
);
}
+ function streams_kickstart() {
+ reset_events();
+ typing_data.kickstart_inbound_timer(topic_typing_key, stub_delay, stub_f);
+ }
+
+ function streams_clear() {
+ reset_events();
+ typing_data.clear_inbound_timer(topic_typing_key);
+ }
+
set_global("setTimeout", set_timeout);
set_global("clearTimeout", clear_timeout);
@@ -158,4 +187,36 @@ test("timers", () => {
timer_cleared: false,
timer_set: false,
});
+
+ // first time, we set
+ streams_kickstart();
+ assert.deepEqual(events, {
+ f: stub_f,
+ timer_cleared: false,
+ timer_set: true,
+ });
+
+ // second time we clear and set
+ streams_kickstart();
+ assert.deepEqual(events, {
+ f: stub_f,
+ timer_cleared: true,
+ timer_set: true,
+ });
+
+ // first time clearing, we clear
+ streams_clear();
+ assert.deepEqual(events, {
+ f: undefined,
+ timer_cleared: true,
+ timer_set: false,
+ });
+
+ // second time clearing, we noop
+ streams_clear();
+ assert.deepEqual(events, {
+ f: undefined,
+ timer_cleared: false,
+ timer_set: false,
+ });
});
diff --git a/zerver/lib/home.py b/zerver/lib/home.py
index eb1741bf41c..6767547e780 100644
--- a/zerver/lib/home.py
+++ b/zerver/lib/home.py
@@ -137,7 +137,7 @@ def build_page_params_for_home_page_load(
"notification_settings_null": True,
"bulk_message_deletion": True,
"user_avatar_url_field_optional": True,
- "stream_typing_notifications": False, # Set this to True when frontend support is implemented.
+ "stream_typing_notifications": True,
"user_settings_object": True,
"linkifier_url_template": True,
}