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.

quote: Add helpers for classifying multiple messages.

Aman Agrawal7c416a437d76
diff --git a/web/src/compose_reply.ts b/web/src/compose_reply.ts
index e5368c4f675..d296f00bcf5 100644
--- a/web/src/compose_reply.ts
+++ b/web/src/compose_reply.ts
@@ -565,6 +565,21 @@ function quote_single_message(opts: QuoteMessageOpts): void {
});
}
+export function all_messages_have_same_channel(messages: Message[]): boolean {
+ assert(messages.length > 0);
+ const first_message = messages[0]!;
+ if (first_message.type !== "stream") {
+ return false;
+ }
+ const target_stream_id = first_message.stream_id;
+ return messages.every((msg) => msg.type === "stream" && msg.stream_id === target_stream_id);
+}
+
+export function all_messages_are_private(messages: Message[]): boolean {
+ assert(messages.length > 0);
+ return messages.every((msg) => msg.type === "private");
+}
+
export function all_messages_have_same_recipient(messages: Message[]): boolean {
assert(messages.length > 0);
// When the current narrow cannot contain multiple conversations,
@@ -593,6 +608,27 @@ export function all_messages_have_same_recipient(messages: Message[]): boolean {
);
}
+type MultipleMessageStatus =
+ | "SAME_CHANNEL_DIFFERENT_TOPICS"
+ | "DIFFERENT_DM_CONVERSATIONS"
+ | "NOTHING_IN_COMMON"
+ | "SAME_RECIPIENT";
+
+export function get_multi_message_quote_status(messages: Message[]): MultipleMessageStatus {
+ if (all_messages_have_same_recipient(messages)) {
+ return "SAME_RECIPIENT";
+ }
+
+ if (all_messages_have_same_channel(messages)) {
+ return "SAME_CHANNEL_DIFFERENT_TOPICS";
+ }
+
+ if (all_messages_are_private(messages)) {
+ return "DIFFERENT_DM_CONVERSATIONS";
+ }
+ return "NOTHING_IN_COMMON";
+}
+
function extract_range_html(range: Range, preserve_ancestors = false): string {
// Returns the html of the range as a string, optionally preserving 2
// levels of ancestors.
diff --git a/web/tests/compose_reply.test.cjs b/web/tests/compose_reply.test.cjs
index d5bebf7b0b5..28442b3410c 100644
--- a/web/tests/compose_reply.test.cjs
+++ b/web/tests/compose_reply.test.cjs
@@ -98,6 +98,109 @@ run_test("all_messages_have_same_recipient", () => {
);
});
+run_test("all_messages_have_same_channel", () => {
+ // Assertions on empty arrays
+ assert.throws(() => compose_reply.all_messages_have_same_channel([]), Error);
+
+ // Messages involving private messages
+ assert.equal(compose_reply.all_messages_have_same_channel([msg_pm_1, msg_pm_2]), false);
+
+ // Channel messages
+ assert.ok(compose_reply.all_messages_have_same_channel([msg_stream_denmark_general]));
+ assert.ok(
+ compose_reply.all_messages_have_same_channel([
+ msg_stream_denmark_general,
+ msg_stream_denmark_design,
+ ]),
+ "Returns true for same stream ID, even if topics differ",
+ );
+ assert.equal(
+ compose_reply.all_messages_have_same_channel([
+ msg_stream_denmark_general,
+ msg_stream_sweden_general,
+ ]),
+ false,
+ "Return false if stream IDs differ",
+ );
+
+ // Mixed types of messages.
+ assert.equal(
+ compose_reply.all_messages_have_same_channel([msg_stream_denmark_general, msg_pm_1]),
+ false,
+ );
+ assert.equal(
+ compose_reply.all_messages_have_same_channel([msg_pm_1, msg_stream_denmark_general]),
+ false,
+ );
+});
+
+run_test("all_messages_are_private", () => {
+ // Assertions on empty arrays
+ assert.throws(() => compose_reply.all_messages_are_private([]), Error);
+
+ // Stream messages immediately return false
+ assert.equal(compose_reply.all_messages_are_private([msg_stream_denmark_general]), false);
+ assert.equal(
+ compose_reply.all_messages_are_private([msg_stream_denmark_general, msg_pm_1]),
+ false,
+ );
+
+ // Private messages
+ assert.ok(compose_reply.all_messages_are_private([msg_pm_1]));
+ assert.ok(
+ compose_reply.all_messages_are_private([msg_pm_1, msg_pm_3]),
+ "Returns true for private messages even if recipients differ",
+ );
+
+ // Mixed types where first is private
+ assert.equal(
+ compose_reply.all_messages_are_private([msg_pm_1, msg_stream_denmark_general]),
+ false,
+ );
+});
+
+run_test("get_multi_message_quote_status", () => {
+ set_message_lists_current(true);
+
+ const channel_1_general = {type: "stream", stream_id: 10, topic: "general"};
+ const channel_1_general_2 = {type: "stream", stream_id: 10, topic: "general"};
+ const channel_1_design = {type: "stream", stream_id: 10, topic: "design"};
+ const channel_2_general = {type: "stream", stream_id: 20, topic: "general"};
+
+ const pm_1 = {type: "private", to_user_ids: "1,2"};
+ const pm_1_b = {type: "private", to_user_ids: "1,2"};
+ const pm_2 = {type: "private", to_user_ids: "3,4"};
+
+ assert.equal(
+ compose_reply.get_multi_message_quote_status([channel_1_general, channel_1_general_2]),
+ "SAME_RECIPIENT",
+ );
+ assert.equal(compose_reply.get_multi_message_quote_status([pm_1, pm_1_b]), "SAME_RECIPIENT");
+
+ assert.equal(
+ compose_reply.get_multi_message_quote_status([channel_1_general, channel_1_design]),
+ "SAME_CHANNEL_DIFFERENT_TOPICS",
+ );
+
+ assert.equal(
+ compose_reply.get_multi_message_quote_status([pm_1, pm_2]),
+ "DIFFERENT_DM_CONVERSATIONS",
+ );
+
+ // NOTHING_IN_COMMON
+ // 1. Belong to different channels.
+ assert.equal(
+ compose_reply.get_multi_message_quote_status([channel_1_general, channel_2_general]),
+ "NOTHING_IN_COMMON",
+ );
+
+ // 2. Mixed channel and private messages
+ assert.equal(
+ compose_reply.get_multi_message_quote_status([channel_1_general, pm_1]),
+ "NOTHING_IN_COMMON",
+ );
+});
+
run_test("get_quote_context_for_message", () => {
set_message_lists_current(true);