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.

drafts: Create a section to show drafts from the current conversation.

evykassirer69685c286442
diff --git a/frontend_tests/node_tests/drafts.js b/frontend_tests/node_tests/drafts.js
index e1b7574c157..acf581adddc 100644
--- a/frontend_tests/node_tests/drafts.js
+++ b/frontend_tests/node_tests/drafts.js
@@ -7,6 +7,15 @@ const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
const {user_settings} = require("../zjsunit/zpage_params");
+const people = zrequire("people");
+
+const aaron = {
+ email: "[email protected]",
+ user_id: 6,
+ full_name: "Aaron",
+};
+people.add_active_user(aaron);
+
const noop = () => {};
const setTimeout_delay = 3000;
@@ -308,7 +317,7 @@ test("delete_all_drafts", () => {
assert.deepEqual(draft_model.get(), {});
});
-test("format_drafts", ({override_rewire, mock_template}) => {
+test("format_drafts", ({override, override_rewire, mock_template}) => {
function feb12() {
return new Date(1549958107000); // 2/12/2019 07:55:07 AM (UTC+0)
}
@@ -368,21 +377,21 @@ test("format_drafts", ({override_rewire, mock_template}) => {
{
draft_id: "id2",
is_stream: false,
- recipients: "[email protected]",
+ recipients: "Aaron",
raw_content: "Test private message",
time_stamp: "Jan 30",
},
{
draft_id: "id5",
is_stream: false,
- recipients: "[email protected]",
+ recipients: "Aaron",
raw_content: "Test private message 3",
time_stamp: "Jan 29",
},
{
draft_id: "id4",
is_stream: false,
- recipients: "[email protected]",
+ recipients: "Aaron",
raw_content: "Test private message 2",
time_stamp: "Jan 26",
},
@@ -416,9 +425,14 @@ test("format_drafts", ({override_rewire, mock_template}) => {
return {name: "stream"};
};
+ override(compose_state, "composing", () => true);
+ override(compose_state, "get_message_type", () => "private");
+ override(compose_state, "private_message_recipient", () => "");
+
mock_template("draft_table_body.hbs", false, (data) => {
- // Tests formatting and sorting of drafts
- assert.deepEqual(data.drafts, expected);
+ // Tests formatting and time-sorting of drafts
+ assert.deepEqual(data.narrow_drafts, []);
+ assert.deepEqual(data.other_drafts, expected);
return "<draft table stub>";
});
@@ -443,3 +457,141 @@ test("format_drafts", ({override_rewire, mock_template}) => {
drafts.launch();
});
+
+test("filter_drafts", ({override, override_rewire, mock_template}) => {
+ function feb12() {
+ return new Date(1549958107000); // 2/12/2019 07:55:07 AM (UTC+0)
+ }
+
+ function date(offset) {
+ return feb12().setDate(offset);
+ }
+
+ const stream_draft_1 = {
+ stream: "stream",
+ topic: "topic",
+ type: "stream",
+ content: "Test stream message",
+ stream_id: 30,
+ updatedAt: feb12().getTime(),
+ };
+ const pm_draft_1 = {
+ private_message_recipient: "[email protected]",
+ reply_to: "[email protected]",
+ type: "private",
+ content: "Test private message",
+ updatedAt: date(-1),
+ };
+ const stream_draft_2 = {
+ stream: "stream 2",
+ subject: "topic",
+ type: "stream",
+ content: "Test stream message 2",
+ updatedAt: date(-10),
+ };
+ const pm_draft_2 = {
+ private_message_recipient: "[email protected]",
+ reply_to: "[email protected]",
+ type: "private",
+ content: "Test private message 2",
+ updatedAt: date(-5),
+ };
+ const pm_draft_3 = {
+ private_message_recipient: "[email protected]",
+ reply_to: "[email protected]",
+ type: "private",
+ content: "Test private message 3",
+ updatedAt: date(-2),
+ };
+
+ const expected_pm_drafts = [
+ {
+ draft_id: "id2",
+ is_stream: false,
+ recipients: "Aaron",
+ raw_content: "Test private message",
+ time_stamp: "Jan 30",
+ },
+ {
+ draft_id: "id5",
+ is_stream: false,
+ recipients: "Aaron",
+ raw_content: "Test private message 3",
+ time_stamp: "Jan 29",
+ },
+ {
+ draft_id: "id4",
+ is_stream: false,
+ recipients: "Aaron",
+ raw_content: "Test private message 2",
+ time_stamp: "Jan 26",
+ },
+ ];
+
+ const expected_other_drafts = [
+ {
+ draft_id: "id1",
+ is_stream: true,
+ stream_name: "stream",
+ stream_color: "#FFFFFF",
+ dark_background: "",
+ topic: "topic",
+ raw_content: "Test stream message",
+ time_stamp: "7:55 AM",
+ },
+ {
+ draft_id: "id3",
+ is_stream: true,
+ stream_name: "stream 2",
+ stream_color: "#FFFFFF",
+ dark_background: "",
+ topic: "topic",
+ raw_content: "Test stream message 2",
+ time_stamp: "Jan 21",
+ },
+ ];
+
+ $("#drafts_table").append = noop;
+
+ const draft_model = drafts.draft_model;
+ const ls = localstorage();
+ const data = {
+ id1: stream_draft_1,
+ id2: pm_draft_1,
+ id3: stream_draft_2,
+ id4: pm_draft_2,
+ id5: pm_draft_3,
+ };
+ ls.set("drafts", data);
+ assert.deepEqual(draft_model.get(), data);
+
+ const stub_render_now = timerender.render_now;
+ override_rewire(timerender, "render_now", (time) =>
+ stub_render_now(time, new Date(1549958107000)),
+ );
+
+ sub_store.get = function (stream_id) {
+ assert.equal(stream_id, 30);
+ return {name: "stream"};
+ };
+
+ mock_template("draft_table_body.hbs", false, (data) => {
+ // Tests splitting up drafts by current narrow.
+ assert.deepEqual(data.narrow_drafts, expected_pm_drafts);
+ assert.deepEqual(data.other_drafts, expected_other_drafts);
+ return "<draft table stub>";
+ });
+
+ override_rewire(drafts, "set_initial_element", noop);
+
+ override(compose_state, "composing", () => true);
+ override(compose_state, "get_message_type", () => "private");
+ override(
+ compose_state,
+ "private_message_recipient",
+ () => pm_draft_1.private_message_recipient,
+ );
+
+ $.create("#drafts_table .draft-row", {children: []});
+ drafts.launch();
+});
diff --git a/frontend_tests/puppeteer_tests/drafts.ts b/frontend_tests/puppeteer_tests/drafts.ts
index 4699df2cfc7..caf224d23a9 100644
--- a/frontend_tests/puppeteer_tests/drafts.ts
+++ b/frontend_tests/puppeteer_tests/drafts.ts
@@ -171,7 +171,7 @@ async function test_edited_draft_message(page: Page): Promise<void> {
async function test_restore_private_message_draft(page: Page): Promise<void> {
console.log("Restoring private message draft.");
- await page.click("#drafts_table .message_row.private-message .restore-draft");
+ await page.click(".message_row.private-message .restore-draft");
await wait_for_drafts_to_disappear(page);
await page.waitForSelector("#private-message", {visible: true});
await common.check_form_contents(page, "form#send_message_form", {
diff --git a/static/js/drafts.js b/static/js/drafts.js
index fd54a00f2d0..c712ef17ef0 100644
--- a/static/js/drafts.js
+++ b/static/js/drafts.js
@@ -20,6 +20,7 @@ import {$t, $t_html} from "./i18n";
import {localstorage} from "./localstorage";
import * as markdown from "./markdown";
import * as narrow from "./narrow";
+import * as narrow_state from "./narrow_state";
import * as overlays from "./overlays";
import * as people from "./people";
import * as rendered_markdown from "./rendered_markdown";
@@ -365,12 +366,35 @@ function row_with_focus() {
function row_before_focus() {
const $focused_row = row_with_focus();
- return $focused_row.prev(".draft-row:visible");
+ const $prev_row = $focused_row.prev(".draft-row:visible");
+ // The draft modal can have two sub-sections. This handles the edge case
+ // when the user moves from the second "Other drafts" section to the first
+ // section which contains drafts from a particular narrow.
+ if (
+ $prev_row.length === 0 &&
+ $focused_row.parent().attr("id") === "other-drafts" &&
+ $("#drafts-from-conversation").is(":visible")
+ ) {
+ return $($("#drafts-from-conversation").children(".draft-row:visible").last());
+ }
+
+ return $prev_row;
}
function row_after_focus() {
const $focused_row = row_with_focus();
- return $focused_row.next(".draft-row:visible");
+ const $next_row = $focused_row.next(".draft-row:visible");
+ // The draft modal can have two sub-sections. This handles the edge case
+ // when the user moves from the first section (drafts from a particular
+ // narrow) to the second section which contains the rest of the drafts.
+ if (
+ $next_row.length === 0 &&
+ $focused_row.parent().attr("id") === "drafts-from-conversation" &&
+ $("#other-drafts").is(":visible")
+ ) {
+ return $("#other-drafts").children(".draft-row:visible").first();
+ }
+ return $next_row;
}
function remove_draft($draft_row) {
@@ -384,6 +408,24 @@ function remove_draft($draft_row) {
if ($("#drafts_table .draft-row").length === 0) {
$("#drafts_table .no-drafts").show();
}
+ update_rendered_drafts(
+ $("#drafts-from-conversation .draft-row").length > 0,
+ $("#other-drafts .draft-row").length > 0,
+ );
+}
+
+function update_rendered_drafts(has_drafts_from_conversation, has_other_drafts) {
+ if (has_drafts_from_conversation) {
+ $("#drafts-from-conversation").show();
+ } else {
+ // Since there are no relevant drafts from this conversation left, switch to the "all drafts" view and remove headers.
+ $("#drafts-from-conversation").hide();
+ $("#other-drafts-header").hide();
+ }
+
+ if (!has_other_drafts) {
+ $("#other-drafts").hide();
+ }
}
export function launch() {
@@ -405,10 +447,29 @@ export function launch() {
return sorted_formatted_drafts;
}
- function render_widgets(drafts) {
+ function get_header_for_narrow_drafts() {
+ const {stream_name, topic, private_recipients} = current_recipient_data();
+ if (private_recipients) {
+ return $t(
+ {defaultMessage: "Drafts from conversation with {recipient}"},
+ {
+ recipient: people.emails_to_full_names_string(private_recipients.split(",")),
+ },
+ );
+ }
+ const recipient = topic ? `#${stream_name} > ${topic}` : `#${stream_name}`;
+ return $t({defaultMessage: "Drafts from {recipient}"}, {recipient});
+ }
+
+ function render_widgets(narrow_drafts, other_drafts) {
$("#drafts_table").empty();
+
+ const narrow_drafts_header = get_header_for_narrow_drafts();
+
const rendered = render_draft_table_body({
- drafts,
+ narrow_drafts_header,
+ narrow_drafts,
+ other_drafts,
draft_lifetime: DRAFT_LIFETIME,
});
const $drafts_table = $("#drafts_table");
@@ -423,6 +484,7 @@ export function launch() {
rendered_markdown.update_elements($(this));
});
}
+ update_rendered_drafts(narrow_drafts.length > 0, other_drafts.length > 0);
}
function setup_event_handlers() {
@@ -445,15 +507,100 @@ export function launch() {
});
}
- const drafts = format_drafts(draft_model.get());
- render_widgets(drafts);
+ function current_recipient_data() {
+ // Prioritize recipients from the compose box first. If the compose
+ // box isn't open, just return data from the current narrow.
+ if (!compose_state.composing()) {
+ const stream_name = narrow_state.stream();
+ return {
+ stream_name,
+ topic: narrow_state.topic(),
+ private_recipients: narrow_state.pm_emails_string(),
+ };
+ }
+
+ if (compose_state.get_message_type() === "stream") {
+ const stream_name = compose_state.stream_name();
+ return {
+ stream_name,
+ topic: compose_state.topic(),
+ private_recipients: undefined,
+ };
+ } else if (compose_state.get_message_type() === "private") {
+ return {
+ stream_name: undefined,
+ topic: undefined,
+ private_recipients: compose_state.private_message_recipient(),
+ };
+ }
+ return {
+ stream_name: undefined,
+ topic: undefined,
+ private_recipients: undefined,
+ };
+ }
+
+ function filter_drafts_by_compose_box_and_recipient(drafts) {
+ const {stream_name, topic, private_recipients} = current_recipient_data();
+ const stream_id = stream_name ? stream_data.get_stream_id(stream_name) : undefined;
+ const narrow_drafts_ids = [];
+ for (const [id, draft] of Object.entries(drafts)) {
+ // Match by stream and topic.
+ if (
+ stream_id &&
+ topic &&
+ draft.topic &&
+ util.same_recipient(draft, {type: "stream", stream_id, topic})
+ ) {
+ narrow_drafts_ids.push(id);
+ }
+ // Match by only stream.
+ else if (
+ draft.type === "stream" &&
+ stream_id &&
+ !topic &&
+ draft.stream_id === stream_id
+ ) {
+ narrow_drafts_ids.push(id);
+ }
+ // Match by private message recipient.
+ else if (
+ draft.type === "private" &&
+ private_recipients &&
+ _.isEqual(
+ draft.private_message_recipient
+ .split(",")
+ .map((s) => s.trim())
+ .sort(),
+ private_recipients
+ .split(",")
+ .map((s) => s.trim())
+ .sort(),
+ )
+ ) {
+ narrow_drafts_ids.push(id);
+ }
+ }
+ return _.pick(drafts, narrow_drafts_ids);
+ }
+
+ const drafts = draft_model.get();
+ const narrow_drafts = filter_drafts_by_compose_box_and_recipient(drafts);
+ const other_drafts = _.pick(
+ drafts,
+ _.difference(Object.keys(drafts), Object.keys(narrow_drafts)),
+ );
+ const formatted_narrow_drafts = format_drafts(narrow_drafts);
+ const formatted_other_drafts = format_drafts(other_drafts);
+
+ render_widgets(formatted_narrow_drafts, formatted_other_drafts);
// We need to force a style calculation on the newly created
// element in order for the CSS transition to take effect.
$("#draft_overlay").css("opacity");
open_overlay();
- set_initial_element(drafts);
+ set_initial_element(formatted_narrow_drafts.concat(formatted_other_drafts));
setup_event_handlers();
}
diff --git a/static/styles/drafts.css b/static/styles/drafts.css
index 76c286e3a0e..f7d85f40abf 100644
--- a/static/styles/drafts.css
+++ b/static/styles/drafts.css
@@ -69,6 +69,12 @@
color: hsl(0, 0%, 67%);
pointer-events: none;
}
+
+ h2 {
+ font-size: 1.1em;
+ line-height: normal;
+ margin-bottom: 5px;
+ }
}
}
diff --git a/static/templates/draft_table_body.hbs b/static/templates/draft_table_body.hbs
index 8912f1ded56..212ad9693e0 100644
--- a/static/templates/draft_table_body.hbs
+++ b/static/templates/draft_table_body.hbs
@@ -17,9 +17,18 @@
{{t 'No drafts.'}}
</div>
- {{#each drafts}}
- {{> draft}}
- {{/each}}
+ <div id="drafts-from-conversation">
+ <h2>{{narrow_drafts_header}}</h2>
+ {{#each narrow_drafts}}
+ {{> draft}}
+ {{/each}}
+ </div>
+ <div id="other-drafts">
+ <h2 id="other-drafts-header">Other drafts</h2>
+ {{#each other_drafts}}
+ {{> draft}}
+ {{/each}}
+ </div>
</div>
</div>
</div>