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: Extract get_quote_context_for_message.

Aman Agrawal12396754233a
diff --git a/web/src/compose_reply.ts b/web/src/compose_reply.ts
index 9a346d67d26..e5368c4f675 100644
--- a/web/src/compose_reply.ts
+++ b/web/src/compose_reply.ts
@@ -41,6 +41,8 @@ type ReplaceContentOpts = {
quoted_message: Message;
raw_markdown: string;
forward_message: boolean | undefined;
+ previous_quoted_message?: Message | undefined;
+ is_first_in_quote_chain?: boolean;
};
const quoting_placeholder = $t({defaultMessage: "[Quoting…]"});
@@ -401,19 +403,77 @@ function generate_private_message_quote_context(message: Message): string {
);
}
+type QuoteContext = "INCLUDE_SENDER" | "INCLUDE_SENDER_AND_RECIPIENT" | "INCLUDE_NOTHING";
+
+// Returns what context the line before the quote block having the quoted message content
+// should contain.
+export function get_quote_context_for_message(info: {
+ forward_message: boolean | undefined;
+ current_message: Message;
+ previous_quoted_message: Message | undefined;
+ is_first_in_quote_chain: boolean | undefined;
+}): QuoteContext {
+ const {current_message, previous_quoted_message, forward_message, is_first_in_quote_chain} =
+ info;
+ if (is_first_in_quote_chain) {
+ return "INCLUDE_SENDER_AND_RECIPIENT";
+ }
+ if (previous_quoted_message) {
+ if (all_messages_have_same_recipient([current_message, previous_quoted_message])) {
+ // We don't include the sender or the recipient details
+ // for a message that has the same (sender, recipient) pair
+ // as the previous message.
+ if (current_message.sender_id === previous_quoted_message.sender_id) {
+ return "INCLUDE_NOTHING";
+ }
+ // We include the sender context in case only the sender
+ // differs compared to the previous message.
+ return "INCLUDE_SENDER";
+ }
+ return "INCLUDE_SENDER_AND_RECIPIENT";
+ }
+
+ // This message is quoted individually and is not part
+ // of some collection of quoted messages.
+ if (!forward_message) {
+ return "INCLUDE_SENDER";
+ }
+ return "INCLUDE_SENDER_AND_RECIPIENT";
+}
+
function generate_replace_content(info: ReplaceContentOpts): string {
- const {quoted_message, raw_markdown, forward_message} = info;
+ const {
+ quoted_message,
+ raw_markdown,
+ forward_message,
+ previous_quoted_message,
+ is_first_in_quote_chain,
+ } = info;
+ const required_quote_context = get_quote_context_for_message({
+ current_message: quoted_message,
+ forward_message,
+ previous_quoted_message,
+ is_first_in_quote_chain,
+ });
+
let content;
- if (!forward_message) {
- content = generate_sender_only_quote_context(quoted_message);
- } else if (quoted_message.type === "stream") {
- content = generate_channel_message_quote_context(quoted_message);
- } else {
- content = generate_private_message_quote_context(quoted_message);
+ switch (required_quote_context) {
+ case "INCLUDE_SENDER":
+ content = generate_sender_only_quote_context(quoted_message) + "\n";
+ break;
+ case "INCLUDE_SENDER_AND_RECIPIENT":
+ if (quoted_message.type === "stream") {
+ content = generate_channel_message_quote_context(quoted_message) + "\n";
+ } else {
+ content = generate_private_message_quote_context(quoted_message) + "\n";
+ }
+ break;
+ case "INCLUDE_NOTHING":
+ content = "";
+ break;
}
- content += "\n";
const fence = fenced_code.get_unused_fence(raw_markdown);
content += `${fence}quote\n${raw_markdown}\n${fence}`;
return content;
@@ -505,6 +565,34 @@ function quote_single_message(opts: QuoteMessageOpts): void {
});
}
+export function all_messages_have_same_recipient(messages: Message[]): boolean {
+ assert(messages.length > 0);
+ // When the current narrow cannot contain multiple conversations,
+ // the highlighted messages are guaranteed to share a recipient.
+ if (!message_lists.current?.data.filter.may_contain_multiple_conversations()) {
+ return true;
+ }
+ const first_message = messages[0]!;
+
+ if (first_message.type === "private") {
+ const target_user_ids = first_message.to_user_ids;
+
+ return messages.every(
+ (msg) => msg.type === "private" && msg.to_user_ids === target_user_ids,
+ );
+ }
+ // Stream messages must match both the stream ID and the topic.
+ const target_stream_id = first_message.stream_id;
+ const target_topic = first_message.topic.toLowerCase();
+
+ return messages.every(
+ (msg) =>
+ msg.type === "stream" &&
+ msg.stream_id === target_stream_id &&
+ msg.topic.toLowerCase() === target_topic,
+ );
+}
+
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
new file mode 100644
index 00000000000..d5bebf7b0b5
--- /dev/null
+++ b/web/tests/compose_reply.test.cjs
@@ -0,0 +1,212 @@
+"use strict";
+
+const assert = require("node:assert/strict");
+
+const {zrequire, mock_esm} = require("./lib/namespace.cjs");
+const {run_test} = require("./lib/test.cjs");
+
+const compose_reply = zrequire("compose_reply");
+const message_lists = mock_esm("../src/message_lists");
+
+const pm_user_ids_1 = "1,2";
+const pm_user_ids_2 = "3,4";
+
+const msg_pm_1 = {type: "private", to_user_ids: pm_user_ids_1};
+const msg_pm_2 = {type: "private", to_user_ids: pm_user_ids_1};
+const msg_pm_3 = {type: "private", to_user_ids: pm_user_ids_2};
+
+const msg_stream_denmark_general = {type: "stream", stream_id: 10, topic: "general"};
+// Topic case should be ignored by all_messages_have_same_recipient
+const msg_stream_denmark_general_caps = {type: "stream", stream_id: 10, topic: "GENERAL"};
+const msg_stream_denmark_design = {type: "stream", stream_id: 10, topic: "design"};
+const msg_stream_sweden_general = {type: "stream", stream_id: 20, topic: "general"};
+
+function set_message_lists_current(may_contain_multiple) {
+ message_lists.current = {
+ data: {
+ filter: {
+ may_contain_multiple_conversations() {
+ return may_contain_multiple;
+ },
+ },
+ },
+ };
+}
+
+run_test("all_messages_have_same_recipient", () => {
+ // When the filter is set to channel+topic or a dm
+ // conversation.
+ set_message_lists_current(false);
+ assert.equal(
+ compose_reply.all_messages_have_same_recipient([
+ msg_stream_denmark_general,
+ msg_stream_denmark_general_caps,
+ ]),
+ true,
+ "Return true if existing narrow cannot possibly have more than one recipients",
+ );
+
+ // Assertions on empty arrays
+ assert.throws(() => compose_reply.all_messages_have_same_recipient([]), Error);
+ set_message_lists_current(true);
+ // Private messages
+ assert.ok(compose_reply.all_messages_have_same_recipient([msg_pm_1]));
+ assert.ok(compose_reply.all_messages_have_same_recipient([msg_pm_1, msg_pm_2]));
+ assert.equal(
+ compose_reply.all_messages_have_same_recipient([msg_pm_1, msg_pm_2, msg_pm_3]),
+ false,
+ "Fails if private message recipients differ",
+ );
+
+ // Channel messages
+ assert.ok(compose_reply.all_messages_have_same_recipient([msg_stream_denmark_general]));
+ assert.ok(
+ compose_reply.all_messages_have_same_recipient([
+ msg_stream_denmark_general,
+ msg_stream_denmark_general_caps,
+ ]),
+ "Topic matching should be case-insensitive",
+ );
+
+ assert.equal(
+ compose_reply.all_messages_have_same_recipient([
+ msg_stream_denmark_general,
+ msg_stream_denmark_design,
+ ]),
+ false,
+ "Return false if topics differ",
+ );
+ assert.equal(
+ compose_reply.all_messages_have_same_recipient([
+ msg_stream_denmark_general,
+ msg_stream_sweden_general,
+ ]),
+ false,
+ "Return false if stream IDs differ for channel messages",
+ );
+
+ // Mixed types
+ assert.equal(
+ compose_reply.all_messages_have_same_recipient([msg_pm_1, msg_stream_denmark_general]),
+ false,
+ "Return false on mixed stream/private messages",
+ );
+ assert.equal(
+ compose_reply.all_messages_have_same_recipient([msg_stream_denmark_general, msg_pm_1]),
+ false,
+ "Return false on mixed stream/private messages",
+ );
+});
+
+run_test("get_quote_context_for_message", () => {
+ set_message_lists_current(true);
+
+ const msg_alice = {type: "stream", stream_id: 10, topic: "general", sender_id: 1};
+ const msg_alice_2 = {type: "stream", stream_id: 10, topic: "general", sender_id: 1};
+ const msg_bob = {type: "stream", stream_id: 10, topic: "general", sender_id: 2};
+ const msg_bob_design = {type: "stream", stream_id: 10, topic: "design", sender_id: 2};
+
+ const pm_alice = {type: "private", to_user_ids: "1,2", sender_id: 1};
+ const pm_alice_2 = {type: "private", to_user_ids: "1,2", sender_id: 1};
+ const pm_bob = {type: "private", to_user_ids: "1,2", sender_id: 2};
+ const pm_charlie = {type: "private", to_user_ids: "1,2,3", sender_id: 3};
+
+ // First message in a quote chain
+ assert.equal(
+ compose_reply.get_quote_context_for_message({
+ current_message: msg_alice,
+ previous_quoted_message: undefined,
+ forward_message: false,
+ is_first_in_quote_chain: true,
+ }),
+ "INCLUDE_SENDER_AND_RECIPIENT",
+ );
+
+ // Continuous channel message from the same sender
+ assert.equal(
+ compose_reply.get_quote_context_for_message({
+ current_message: msg_alice_2,
+ previous_quoted_message: msg_alice,
+ forward_message: false,
+ is_first_in_quote_chain: false,
+ }),
+ "INCLUDE_NOTHING",
+ );
+
+ // Continuous private message from the same sender
+ assert.equal(
+ compose_reply.get_quote_context_for_message({
+ current_message: pm_alice_2,
+ previous_quoted_message: pm_alice,
+ forward_message: false,
+ is_first_in_quote_chain: false,
+ }),
+ "INCLUDE_NOTHING",
+ );
+
+ // Same channel thread, but the sender changed
+ assert.equal(
+ compose_reply.get_quote_context_for_message({
+ current_message: msg_bob,
+ previous_quoted_message: msg_alice,
+ forward_message: false,
+ is_first_in_quote_chain: false,
+ }),
+ "INCLUDE_SENDER",
+ );
+
+ // Same private message thread, but the sender changed
+ assert.equal(
+ compose_reply.get_quote_context_for_message({
+ current_message: pm_bob,
+ previous_quoted_message: pm_alice,
+ forward_message: false,
+ is_first_in_quote_chain: false,
+ }),
+ "INCLUDE_SENDER",
+ );
+
+ // Channel/topic changed completely
+ assert.equal(
+ compose_reply.get_quote_context_for_message({
+ current_message: msg_bob_design,
+ previous_quoted_message: msg_alice,
+ forward_message: false,
+ is_first_in_quote_chain: false,
+ }),
+ "INCLUDE_SENDER_AND_RECIPIENT",
+ );
+
+ // Private message recipients changed completely
+ assert.equal(
+ compose_reply.get_quote_context_for_message({
+ current_message: pm_charlie,
+ previous_quoted_message: pm_alice,
+ forward_message: false,
+ is_first_in_quote_chain: false,
+ }),
+ "INCLUDE_SENDER_AND_RECIPIENT",
+ );
+
+ // Quoted individually (no previous message)
+ assert.equal(
+ compose_reply.get_quote_context_for_message({
+ current_message: msg_alice,
+ previous_quoted_message: undefined,
+ forward_message: false,
+ is_first_in_quote_chain: false,
+ }),
+ "INCLUDE_SENDER",
+ );
+
+ // Forwarded individually
+ assert.equal(
+ compose_reply.get_quote_context_for_message({
+ current_message: msg_alice,
+ previous_quoted_message: undefined,
+ forward_message: true,
+ is_first_in_quote_chain: false,
+ }),
+ "INCLUDE_SENDER_AND_RECIPIENT",
+ );
+});