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: Store DM drafts with user ids instead of emails.

Evy Kassirer2cbf9e130e35
diff --git a/web/src/drafts.ts b/web/src/drafts.ts
index 7b72fb7391f..25e6fe001a1 100644
--- a/web/src/drafts.ts
+++ b/web/src/drafts.ts
@@ -56,7 +56,7 @@ const draft_schema = z.intersection(
z.object({
type: z.literal("private"),
reply_to: z.string(),
- private_message_recipient: z.string(),
+ private_message_recipient_ids: z.array(z.number()),
}),
]),
);
@@ -84,7 +84,8 @@ const possibly_buggy_draft_schema = z.intersection(
z.object({
type: z.literal("private"),
reply_to: z.string(),
- private_message_recipient: z.string(),
+ private_message_recipient: z.string().optional(),
+ private_message_recipient_ids: z.array(z.number()).optional(),
}),
]),
);
@@ -117,8 +118,23 @@ export const draft_model = (function () {
const parsed_drafts = possibly_buggy_drafts_schema.parse(drafts);
const valid_drafts: Record<string, LocalStorageDraft> = {};
for (const [draft_id, draft] of Object.entries(parsed_drafts)) {
- if (draft.type !== "stream") {
- valid_drafts[draft_id] = draft;
+ // TODO/compatibility: We should eventually be able to delete this. But
+ // probably not anytime soon. Once you can no longer upgrade to `main` without
+ // first upgrading to 11.0, we can be certain clients that have actually logged
+ // in have experienced this conversion code... but even after that, a client
+ // may still have old-style drafts for several months.
+ if (draft.type === "private") {
+ if (draft.private_message_recipient_ids === undefined) {
+ assert(draft.private_message_recipient !== undefined);
+ draft.private_message_recipient_ids = people.emails_string_to_user_ids(
+ draft.private_message_recipient,
+ );
+ delete draft.private_message_recipient;
+ }
+ valid_drafts[draft_id] = {
+ ...draft,
+ private_message_recipient_ids: draft.private_message_recipient_ids,
+ };
continue;
}
@@ -314,12 +330,12 @@ export function snapshot_message(): LocalStorageDraft | undefined {
updatedAt: getTimestamp(),
};
if (message.type === "private") {
- const recipient = compose_state.private_message_recipient_emails();
+ const recipient_emails = compose_state.private_message_recipient_emails();
return {
...message,
type: "private",
- reply_to: recipient,
- private_message_recipient: recipient,
+ reply_to: recipient_emails,
+ private_message_recipient_ids: compose_state.private_message_recipient_ids(),
is_sending_saving: false,
drafts_version: CURRENT_DRAFT_VERSION,
};
@@ -344,7 +360,7 @@ type ComposeArguments =
}
| {
type: "private";
- private_message_recipient: string;
+ private_message_recipient_ids: number[];
content: string;
};
@@ -363,13 +379,13 @@ export function restore_message(draft: LocalStorageDraft): ComposeArguments {
};
}
- const recipient_emails = draft.private_message_recipient
- .split(",")
- .filter((email) => people.is_valid_email_for_compose(email));
- const sorted_recipient_emails = people.sort_emails_by_username(recipient_emails);
+ const recipient_ids = draft.private_message_recipient_ids.filter((user_id) =>
+ people.is_valid_user_id_for_compose(user_id),
+ );
+ const sorted_recipient_ids = people.sort_user_ids_by_username(recipient_ids);
return {
type: "private",
- private_message_recipient: sorted_recipient_emails.join(","),
+ private_message_recipient_ids: sorted_recipient_ids,
content: draft.content,
};
}
@@ -460,7 +476,7 @@ export function rewire_update_draft(value: typeof update_draft): void {
export function current_recipient_data(): {
stream_name: string | undefined;
topic: string | undefined;
- private_recipients: string | undefined;
+ private_recipient_ids: number[] | undefined;
} {
// Prioritize recipients from the compose box first. If the compose
// box isn't open, just return data from the current narrow.
@@ -469,7 +485,7 @@ export function current_recipient_data(): {
return {
stream_name,
topic: narrow_state.topic(),
- private_recipients: narrow_state.pm_emails_string(),
+ private_recipient_ids: [...narrow_state.pm_ids_set()],
};
}
@@ -478,26 +494,26 @@ export function current_recipient_data(): {
return {
stream_name,
topic: compose_state.topic(),
- private_recipients: undefined,
+ private_recipient_ids: undefined,
};
} else if (compose_state.get_message_type() === "private") {
return {
stream_name: undefined,
topic: undefined,
- private_recipients: compose_state.private_message_recipient_emails(),
+ private_recipient_ids: compose_state.private_message_recipient_ids(),
};
}
return {
stream_name: undefined,
topic: undefined,
- private_recipients: undefined,
+ private_recipient_ids: undefined,
};
}
export function filter_drafts_by_compose_box_and_recipient(
drafts = draft_model.get(),
): Record<string, LocalStorageDraft> {
- const {stream_name, topic, private_recipients} = current_recipient_data();
+ const {stream_name, topic, private_recipient_ids} = 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)) {
@@ -527,17 +543,9 @@ export function filter_drafts_by_compose_box_and_recipient(
// Match by direct 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(),
- )
+ private_recipient_ids &&
+ private_recipient_ids.length > 0 &&
+ _.isEqual(new Set(draft.private_message_recipient_ids), new Set(private_recipient_ids))
) {
narrow_drafts_ids.push(id);
}
@@ -655,7 +663,7 @@ export function format_draft(draft: LocalStorageDraftWithId): FormattedDraft | u
};
}
- if (draft.private_message_recipient === "") {
+ if (draft.private_message_recipient_ids.length === 0) {
// No users were set as DM recipients when the draft was created.
return {
draft_id: draft.id,
@@ -669,14 +677,14 @@ export function format_draft(draft: LocalStorageDraftWithId): FormattedDraft | u
}
let is_dm_with_self = false;
- const emails = util.extract_pm_recipients(draft.private_message_recipient);
- if (emails.length === 1) {
- const user = people.get_by_email(emails[0]!);
+ const user_ids = draft.private_message_recipient_ids;
+ if (user_ids.length === 1) {
+ const user = people.get_by_user_id(user_ids[0]!);
if (user && people.is_direct_message_conversation_with_self([user.user_id])) {
is_dm_with_self = true;
}
}
- const recipients = people.emails_to_full_names_string(emails);
+ const recipients = people.user_ids_to_full_names_string(user_ids);
return {
draft_id: draft.id,
is_stream: false,
diff --git a/web/src/drafts_overlay_ui.ts b/web/src/drafts_overlay_ui.ts
index 56c844e5094..2c880aee99b 100644
--- a/web/src/drafts_overlay_ui.ts
+++ b/web/src/drafts_overlay_ui.ts
@@ -45,8 +45,10 @@ function restore_draft(draft_id: string): void {
);
}
} else {
- if (compose_args.private_message_recipient !== "") {
- message_view.show([{operator: "dm", operand: compose_args.private_message_recipient}], {
+ if (compose_args.private_message_recipient_ids.length > 0) {
+ const private_message_recipient_emails =
+ people.user_ids_to_emails_string(compose_args.private_message_recipient_ids) ?? "";
+ message_view.show([{operator: "dm", operand: private_message_recipient_emails}], {
trigger: "restore draft",
});
}
@@ -148,10 +150,10 @@ export function launch(): void {
}
function get_header_for_narrow_drafts(): string {
- const {stream_name, topic, private_recipients} = drafts.current_recipient_data();
- if (private_recipients) {
- if (!private_recipients.includes(",")) {
- const user = people.get_by_email(private_recipients);
+ const {stream_name, topic, private_recipient_ids} = drafts.current_recipient_data();
+ if (private_recipient_ids && private_recipient_ids.length > 0) {
+ if (private_recipient_ids.length === 1) {
+ const user = people.get_by_user_id(private_recipient_ids[0]!);
if (user && people.is_direct_message_conversation_with_self([user.user_id])) {
return $t({defaultMessage: "Drafts from conversation with yourself"});
}
@@ -159,7 +161,7 @@ export function launch(): void {
return $t(
{defaultMessage: "Drafts from conversation with {recipient}"},
{
- recipient: people.emails_to_full_names_string(private_recipients.split(",")),
+ recipient: people.user_ids_to_full_names_string(private_recipient_ids),
},
);
}
diff --git a/web/src/people.ts b/web/src/people.ts
index 27860a63bfb..342bcabd4b9 100644
--- a/web/src/people.ts
+++ b/web/src/people.ts
@@ -189,6 +189,17 @@ export function update_email(user_id: number, new_email: string): void {
// still work correctly.
}
+export function sort_user_ids_by_username(user_ids: number[]): number[] {
+ const name_id_dict = user_ids.map((user_id, index) => ({
+ name: people_by_user_id_dict.has(user_id)
+ ? people_by_user_id_dict.get(user_id)?.full_name
+ : "?",
+ user_id: user_ids[index]!,
+ }));
+
+ return name_id_dict.sort((a, b) => util.strcmp(a.name!, b.name!)).map(({user_id}) => user_id);
+}
+
// A function that sorts Email according to the user's full name
export function sort_emails_by_username(emails: string[]): (string | undefined)[] {
const user_ids = emails.map((email) => get_user_id(email));
@@ -346,10 +357,9 @@ export function reply_to_to_user_ids_string(emails_string: string): string | und
return user_ids.join(",");
}
-export function emails_to_full_names_string(emails: string[]): string {
- const names = emails.map((email) => {
- email = email.trim();
- const person = get_by_email(email);
+export function user_ids_to_full_names_string(user_ids: number[]): string {
+ const names = user_ids.map((user_id) => {
+ const person = maybe_get_user_by_id(user_id);
if (person !== undefined) {
return person.full_name;
}
@@ -959,6 +969,21 @@ export function small_avatar_url(message: Message): string {
return gravatar_url_for_email(email);
}
+export function is_valid_user_id_for_compose(user_id: number): boolean {
+ if (cross_realm_dict.has(user_id)) {
+ return true;
+ }
+
+ const person = maybe_get_user_by_id(user_id);
+ if (!person || person.is_inaccessible_user) {
+ return false;
+ }
+
+ // we allow deactivated users in compose so that
+ // one can attempt to reply to threads that contained them.
+ return true;
+}
+
export function is_valid_email_for_compose(email: string): boolean {
if (is_cross_realm_email(email)) {
return true;
diff --git a/web/tests/drafts.test.cjs b/web/tests/drafts.test.cjs
index 72f2e9b2c70..5b8a085400d 100644
--- a/web/tests/drafts.test.cjs
+++ b/web/tests/drafts.test.cjs
@@ -103,7 +103,7 @@ const draft_1 = {
drafts_version: 1,
};
const draft_2 = {
- private_message_recipient: "[email protected]",
+ private_message_recipient_ids: [aaron.user_id],
reply_to: "[email protected]",
type: "private",
content: "Test direct message",
@@ -159,9 +159,18 @@ test("fix buggy drafts", ({override_rewire}) => {
content: "Test stream message",
updatedAt: Date.now(),
};
- const data = {id1: buggy_draft};
+ const draft_with_pm_emails = {
+ private_message_recipient: "[email protected],[email protected]",
+ type: "private",
+ content: "Test direct message",
+ updatedAt: Date.now(),
+ };
const ls = localstorage();
- ls.set("drafts", data);
+ ls.set("drafts", {
+ id1: buggy_draft,
+ id2: draft_with_pm_emails,
+ });
const draft_model = drafts.draft_model;
// The draft is fixed in this codepath.
@@ -175,6 +184,10 @@ test("fix buggy drafts", ({override_rewire}) => {
const draft = draft_model.getDraft("id1");
assert.equal(draft.stream_id, stream_B.stream_id);
assert.equal(draft.topic, "");
+
+ const fixed_draft = draft_model.getDraft("id2");
+ assert.equal(fixed_draft.private_message_recipient, undefined);
+ assert.deepEqual(fixed_draft.private_message_recipient_ids, [iago.user_id, zoe.user_id]);
});
test("draft_model add", ({override_rewire}) => {
@@ -467,7 +480,7 @@ test("format_drafts", ({override, override_rewire, mock_template}) => {
drafts_version: 1,
};
const draft_2 = {
- private_message_recipient: "[email protected]",
+ private_message_recipient_ids: [aaron.user_id],
reply_to: "[email protected]",
type: "private",
content: "Test direct message",
@@ -485,7 +498,7 @@ test("format_drafts", ({override, override_rewire, mock_template}) => {
drafts_version: 1,
};
const draft_4 = {
- private_message_recipient: "[email protected]",
+ private_message_recipient_ids: [iago.user_id],
reply_to: "[email protected]",
type: "private",
content: "Test direct message 2",
@@ -494,7 +507,7 @@ test("format_drafts", ({override, override_rewire, mock_template}) => {
drafts_version: 1,
};
const draft_5 = {
- private_message_recipient: "[email protected],[email protected]",
+ private_message_recipient_ids: [zoe.user_id, iago.user_id],
type: "private",
content: "Test direct message 3",
@@ -512,7 +525,7 @@ test("format_drafts", ({override, override_rewire, mock_template}) => {
drafts_version: 1,
};
const draft_7 = {
- private_message_recipient: "",
+ private_message_recipient_ids: [],
reply_to: "",
type: "private",
content: "Test direct message 4",
@@ -642,6 +655,7 @@ test("format_drafts", ({override, override_rewire, mock_template}) => {
// Tests formatting and time-sorting of drafts
assert.deepEqual(data.narrow_drafts, []);
assert.deepEqual(data.other_drafts, expected);
+ assert.ok(data);
return "<draft table stub>";
});
@@ -694,7 +708,7 @@ test("filter_drafts", ({override, override_rewire, mock_template}) => {
drafts_version: 1,
};
const pm_draft_1 = {
- private_message_recipient: "[email protected]",
+ private_message_recipient_ids: [aaron.user_id],
reply_to: "[email protected]",
type: "private",
content: "Test direct message",
@@ -712,7 +726,7 @@ test("filter_drafts", ({override, override_rewire, mock_template}) => {
drafts_version: 1,
};
const pm_draft_2 = {
- private_message_recipient: "[email protected]",
+ private_message_recipient_ids: [aaron.user_id],
reply_to: "[email protected]",
type: "private",
content: "Test direct message 2",
@@ -721,7 +735,7 @@ test("filter_drafts", ({override, override_rewire, mock_template}) => {
drafts_version: 1,
};
const pm_draft_3 = {
- private_message_recipient: "[email protected]",
+ private_message_recipient_ids: [aaron.user_id],
reply_to: "[email protected]",
type: "private",
content: "Test direct message 3",
diff --git a/web/tests/people.test.cjs b/web/tests/people.test.cjs
index e811d39fb55..608cad5f924 100644
--- a/web/tests/people.test.cjs
+++ b/web/tests/people.test.cjs
@@ -357,6 +357,7 @@ test_people("basics", ({override}) => {
assert.deepEqual(active_user_ids, [me.user_id, isaac.user_id]);
assert.equal(people.is_active_user_for_popover(isaac.user_id), true);
assert.ok(people.is_valid_email_for_compose(isaac.email));
+ assert.ok(people.is_valid_user_id_for_compose(isaac.user_id));
let bot_user_ids = people.get_bot_ids();
assert.equal(bot_user_ids.length, 0);
@@ -368,6 +369,7 @@ test_people("basics", ({override}) => {
assert.equal(people.get_active_human_count(), 1);
assert.equal(people.is_active_user_for_popover(isaac.user_id), false);
assert.equal(people.is_valid_email_for_compose(isaac.email), true);
+ assert.equal(people.is_valid_user_id_for_compose(isaac.user_id), true);
people.add_active_user(bot_botson);
assert.equal(people.is_active_user_for_popover(bot_botson.user_id), true);
@@ -923,20 +925,17 @@ test_people("multi_user_methods", () => {
assert.equal(people.reply_to_to_user_ids_string("[email protected]"), undefined);
});
-test_people("emails_to_full_names_string", () => {
+test_people("user_ids_to_full_names_string", () => {
people.add_active_user(charles);
people.add_active_user(maria);
assert.equal(
- people.emails_to_full_names_string([charles.email, maria.email]),
+ people.user_ids_to_full_names_string([charles.user_id, maria.user_id]),
`${charles.full_name}, ${maria.full_name}`,
);
+ blueslip.expect("error", "Unknown user_id in maybe_get_user_by_id");
assert.equal(
- people.emails_to_full_names_string([
- charles.email,
- maria.email,
- ]),
+ people.user_ids_to_full_names_string([charles.user_id, 9999, maria.user_id]),
`${charles.full_name}, ${maria.full_name}, translated: Unknown user`,
);
});
@@ -1336,28 +1335,25 @@ test_people("initialize", () => {
const params = {};
- params.realm_non_active_users = [
- {
- email: "[email protected]",
- user_id: 15,
- full_name: "Retiree",
- },
- ];
+ const retiree = {
+ email: "[email protected]",
+ user_id: 15,
+ full_name: "Retiree",
+ };
+ params.realm_non_active_users = [retiree];
- params.realm_users = [
- {
- email: "[email protected]",
- user_id: 16,
- full_name: "Alice",
- },
- ];
- params.cross_realm_bots = [
- {
- email: "[email protected]",
- user_id: 17,
- full_name: "Test Bot",
- },
- ];
+ const alice = {
+ email: "[email protected]",
+ user_id: 16,
+ full_name: "Alice",
+ };
+ params.realm_users = [alice];
+ const test_bot = {
+ email: "[email protected]",
+ user_id: 17,
+ full_name: "Test Bot",
+ };
+ params.cross_realm_bots = [test_bot];
const my_user_id = 42;
people.initialize(my_user_id, params);
@@ -1365,9 +1361,14 @@ test_people("initialize", () => {
assert.equal(people.is_active_user_for_popover(17), true);
assert.ok(people.is_cross_realm_email("[email protected]"));
assert.ok(people.is_valid_email_for_compose("[email protected]"));
+ assert.ok(people.is_valid_user_id_for_compose(test_bot.user_id));
assert.ok(people.is_valid_email_for_compose("[email protected]"));
+ assert.ok(people.is_valid_user_id_for_compose(alice.user_id));
assert.ok(people.is_valid_email_for_compose("[email protected]"));
+ assert.ok(people.is_valid_user_id_for_compose(retiree.user_id));
assert.ok(!people.is_valid_email_for_compose("[email protected]"));
+ blueslip.expect("error", "Unknown user_id in maybe_get_user_by_id");
+ assert.ok(!people.is_valid_user_id_for_compose(9999));
assert.ok(people.is_valid_bulk_emails_for_compose(["[email protected]", "[email protected]"]));
assert.ok(!people.is_valid_bulk_emails_for_compose(["[email protected]", "[email protected]"]));
assert.ok(people.is_my_user_id(42));
@@ -1605,6 +1606,23 @@ test_people("user_can_initiate_direct_message_thread", ({override}) => {
assert.ok(people.user_can_initiate_direct_message_thread("32"));
});
+test_people("sort_by_username", () => {
+ people.add_active_user(maria);
+ people.add_active_user(cedar);
+ people.add_active_user(leo);
+
+ assert.deepEqual(
+ people.sort_user_ids_by_username([maria.user_id, cedar.user_id, leo.user_id]),
+ [cedar.user_id, leo.user_id, maria.user_id],
+ );
+
+ assert.deepEqual(people.sort_emails_by_username([maria.email, cedar.email, leo.email]), [
+ cedar.email,
+ leo.email,
+ maria.email,
+ ]);
+});
+
// reset to native Date()
run_test("reset MockDate", () => {
MockDate.reset();