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.

pm_conversation: Add new has_conversation helper.

Vector73b306233cab60
diff --git a/web/src/pm_conversations.ts b/web/src/pm_conversations.ts
index 90c3e1c844c..8340fc76a1c 100644
--- a/web/src/pm_conversations.ts
+++ b/web/src/pm_conversations.ts
@@ -90,6 +90,13 @@ class RecentDirectMessages {
.map((conversation) => conversation.user_ids_string);
}
+ has_conversation(user_ids_string: string): boolean {
+ // Returns a boolean indicating whether we have a record proving
+ // this particular direct message conversation exists.
+ const recipient_ids_string = people.pm_lookup_key(user_ids_string);
+ return this.recent_message_ids.get(recipient_ids_string) !== undefined;
+ }
+
initialize(params: StateData["pm_conversations"]): void {
for (const conversation of params.recent_private_conversations) {
this.insert(conversation.user_ids, conversation.max_message_id);
diff --git a/web/tests/pm_conversations.test.js b/web/tests/pm_conversations.test.js
index 53742e12d06..acb9e77b590 100644
--- a/web/tests/pm_conversations.test.js
+++ b/web/tests/pm_conversations.test.js
@@ -4,19 +4,49 @@ const {strict: assert} = require("assert");
const {zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test");
+const {current_user} = require("./lib/zpage_params");
const user_topics = zrequire("user_topics");
const muted_users = zrequire("muted_users");
const people = zrequire("people");
const pmc = zrequire("pm_conversations");
+const alice = {
+ user_id: 1,
+ email: "[email protected]",
+ full_name: "Alice",
+};
+
+const isaac = {
+ user_id: 2,
+ email: "[email protected]",
+ full_name: "Isaac",
+};
+
+const alex = {
+ user_id: 3,
+ email: "[email protected]",
+ full_name: "Alex",
+};
+
+const me = {
+ user_id: 15,
+ email: "[email protected]",
+ full_name: "Me",
+};
+
+people.add_active_user(alice);
+people.add_active_user(isaac);
+people.add_active_user(alex);
+people.add_active_user(me);
+
const params = {
recent_private_conversations: [
- {user_ids: [1], max_message_id: 100},
- {user_ids: [3], max_message_id: 99},
- {user_ids: [1, 2], max_message_id: 98},
- {user_ids: [1, 2, 3], max_message_id: 97},
- {user_ids: [15], max_message_id: 96}, // self
+ {user_ids: [alice.user_id], max_message_id: 100},
+ {user_ids: [alex.user_id], max_message_id: 99},
+ {user_ids: [alice.user_id, isaac.user_id], max_message_id: 98},
+ {user_ids: [alice.user_id, isaac.user_id, alex.user_id], max_message_id: 97},
+ {user_ids: [me.user_id], max_message_id: 96}, // self
],
};
@@ -25,7 +55,7 @@ function test(label, f) {
pmc.clear_for_testing();
user_topics.set_user_topics([]);
muted_users.set_muted_users([]);
- people.initialize_current_user(15);
+ people.initialize_current_user(me.user_id);
f({override});
});
}
@@ -101,3 +131,24 @@ test("muted_users", () => {
]);
assert.deepEqual(pmc.recent.get_strings(), ["3", "1,2,3", "15"]);
});
+
+test("has_conversation", () => {
+ current_user.user_id = me.user_id;
+ pmc.recent.initialize(params);
+
+ // Tests if `has_conversation` returns `true` when there are previous
+ // messages in the conversation.
+ assert.ok(pmc.recent.has_conversation("1"));
+ assert.ok(pmc.recent.has_conversation("15"));
+ assert.ok(pmc.recent.has_conversation("1,2"));
+ assert.ok(pmc.recent.has_conversation("1,2,15"));
+ // Check that we canonicalize to sorted order. This isn't
+ // functionality we rely on, but seems worth testing.
+ assert.ok(pmc.recent.has_conversation("2,1,15"));
+
+ // Since the current filter does not match the DM view, there may be
+ // messages in the conversation which are not fetched yet.
+ assert.ok(!pmc.recent.has_conversation("1,3"));
+ assert.ok(!pmc.recent.has_conversation("2"));
+ assert.ok(!pmc.recent.has_conversation("72"));
+});