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.

channel_email: Make sender configurable when generating channel email.

Prakhar Pratyush1aa048df35f8
diff --git a/web/src/people.ts b/web/src/people.ts
index 600eb2d1290..ec26fa9f290 100644
--- a/web/src/people.ts
+++ b/web/src/people.ts
@@ -51,6 +51,7 @@ let my_user_id: number;
export let INACCESSIBLE_USER_NAME: string;
export let WELCOME_BOT: User;
+export let EMAIL_GATEWAY_BOT: User;
// We have an init() function so that our automated tests
// can easily clear data.
@@ -1451,6 +1452,8 @@ export function add_cross_realm_user(person: User): void {
cross_realm_dict.set(person.user_id, person);
if (person.full_name === "Welcome Bot") {
WELCOME_BOT = person;
+ } else if (person.full_name === "Email Gateway") {
+ EMAIL_GATEWAY_BOT = person;
}
}
diff --git a/web/src/stream_edit.ts b/web/src/stream_edit.ts
index 5bc0fc661ac..0b1b46f39d0 100644
--- a/web/src/stream_edit.ts
+++ b/web/src/stream_edit.ts
@@ -1,6 +1,7 @@
import ClipboardJS from "clipboard";
import $ from "jquery";
import assert from "minimalistic-assert";
+import type * as tippy from "tippy.js";
import {z} from "zod";
import render_settings_deactivation_stream_modal from "../templates/confirm_dialog/confirm_deactivate_stream.hbs";
@@ -12,20 +13,25 @@ import render_stream_description from "../templates/stream_settings/stream_descr
import render_stream_settings from "../templates/stream_settings/stream_settings.hbs";
import * as blueslip from "./blueslip.ts";
+import type {Bot} from "./bot_data.ts";
import * as browser_history from "./browser_history.ts";
import * as channel from "./channel.ts";
import * as confirm_dialog from "./confirm_dialog.ts";
import {show_copied_confirmation} from "./copied_tooltip.ts";
import * as dialog_widget from "./dialog_widget.ts";
+import * as dropdown_widget from "./dropdown_widget.ts";
import {$t, $t_html} from "./i18n.ts";
import * as keydown_util from "./keydown_util.ts";
import * as narrow_state from "./narrow_state.ts";
+import type {User} from "./people.ts";
+import * as people from "./people.ts";
import * as popovers from "./popovers.ts";
import {postprocess_content} from "./postprocess_content.ts";
import * as scroll_util from "./scroll_util.ts";
import * as settings_components from "./settings_components.ts";
import * as settings_config from "./settings_config.ts";
import * as settings_org from "./settings_org.ts";
+import type {CurrentUser} from "./state_data.ts";
import {current_user, realm} from "./state_data.ts";
import * as stream_data from "./stream_data.ts";
import * as stream_edit_subscribers from "./stream_edit_subscribers.ts";
@@ -370,7 +376,7 @@ export function get_stream_email_address(flags: string[], address: string): stri
return clean_address.replace("@", flag_string + "@");
}
-function show_stream_email_address_modal(address: string): void {
+function show_stream_email_address_modal(address: string, sub: StreamSubscription): void {
const copy_email_address_modal_html = render_copy_email_address_modal({
email_address: address,
tags: [
@@ -397,45 +403,122 @@ function show_stream_email_address_modal(address: string): void {
],
});
+ function get_checked_tags(): string[] {
+ const flags: string[] = [];
+ const $checked_checkboxes = $(".copy-email-modal").find("input:checked");
+ $($checked_checkboxes).each(function () {
+ flags.push($(this).attr("id")!);
+ });
+ return flags;
+ }
+
+ function enable_tag_checkbox_change_handler(): void {
+ // Since channel email addresses encode a sender, we can only
+ // continuously update the email address when we have a current
+ // sender.
+ $("#copy_email_address_modal .tag-checkbox").on("change", () => {
+ const flags = get_checked_tags();
+ address = get_stream_email_address(flags, address);
+ $(".email-address").text(address);
+ });
+ }
+
+ function generate_email_modal_post_render(): void {
+ function update_option_label(sender: User | CurrentUser | Bot): string {
+ if (sender.user_id === people.EMAIL_GATEWAY_BOT.user_id) {
+ return "Email Gateway bot";
+ } else if (sender.user_id === current_user.user_id) {
+ return $t({defaultMessage: "You"});
+ }
+ return sender.full_name;
+ }
+
+ function get_options(): {
+ name: string;
+ unique_id: number;
+ }[] {
+ const senders: (User | CurrentUser | Bot)[] = [people.EMAIL_GATEWAY_BOT];
+ senders.push(
+ ...stream_data.get_current_user_and_their_bots_with_post_messages_permission(sub),
+ );
+ return senders.map((sender) => ({
+ name: update_option_label(sender),
+ unique_id: sender.user_id,
+ }));
+ }
+
+ function item_click_callback(event: JQuery.ClickEvent, dropdown: tippy.Instance): void {
+ sender_dropdown_widget.render();
+ $(sender_dropdown_widget.widget_selector).trigger("input");
+ dropdown.hide();
+
+ // Since channel email addresses encode a sender, we can
+ // only continuously update the email address when we have
+ // a current sender.
+ $("#copy_email_address_modal .tag-checkbox").off("change");
+ $(".stream-email").children().css("visibility", "hidden");
+ $("#copy_email_address_modal .dialog_submit_button").trigger("focus");
+
+ event.stopPropagation();
+ event.preventDefault();
+ }
+
+ const sender_dropdown_widget = new dropdown_widget.DropdownWidget({
+ widget_name: "sender_channel_email_address",
+ get_options,
+ item_click_callback,
+ $events_container: $("#copy_email_address_modal"),
+ default_id: people.EMAIL_GATEWAY_BOT.user_id,
+ unique_id_type: dropdown_widget.DataTypes.NUMBER,
+ hide_search_box: true,
+ });
+ sender_dropdown_widget.setup();
+ }
+
+ function generate_email_address(): void {
+ const close_on_success = false;
+ dialog_widget.submit_api_request(
+ channel.get,
+ "/json/streams/" + sub.stream_id + "/email_address",
+ {},
+ {
+ success_continuation(response_data) {
+ const email = z.object({email: z.string()}).parse(response_data).email;
+ const flags = get_checked_tags();
+ address = get_stream_email_address(flags, email);
+ $(".email-address").text(address);
+ $(".stream-email").children().css("visibility", "visible");
+ enable_tag_checkbox_change_handler();
+ },
+ },
+ close_on_success,
+ );
+ }
+
dialog_widget.launch({
html_heading: $t_html({defaultMessage: "Generate channel email address"}),
html_body: copy_email_address_modal_html,
id: "copy_email_address_modal",
- html_submit_button: $t_html({defaultMessage: "Copy address"}),
+ html_submit_button: $t_html({defaultMessage: "Generate email address"}),
html_exit_button: $t_html({defaultMessage: "Close"}),
help_link: "/help/message-a-channel-by-email#configuration-options",
- on_click() {
- // This is handled by the ClipboardJS object below.
- },
+ post_render: generate_email_modal_post_render,
+ on_click: generate_email_address,
close_on_submit: false,
});
$("#show-sender").prop("checked", true);
- const submit_button = util.the($("#copy_email_address_modal .dialog_submit_button"));
- const clipboard = new ClipboardJS(submit_button, {
- text() {
- return address;
- },
- });
-
- // Show a tippy tooltip when the stream email address copied
- clipboard.on("success", () => {
- show_copied_confirmation(submit_button);
- });
-
- $("#copy_email_address_modal .tag-checkbox").on("change", () => {
- const $checked_checkboxes = $(".copy-email-modal").find("input:checked");
-
- const flags: string[] = [];
-
- $($checked_checkboxes).each(function () {
- flags.push($(this).attr("id")!);
+ const email_address_clipboard = new ClipboardJS(
+ "#copy_email_address_modal .copy-email-address",
+ );
+ email_address_clipboard.on("success", (e) => {
+ assert(e.trigger instanceof HTMLElement);
+ show_copied_confirmation(e.trigger, {
+ show_check_icon: true,
});
-
- address = get_stream_email_address(flags, address);
-
- $(".email-address").text(address);
});
+
+ enable_tag_checkbox_change_handler();
}
export function initialize(): void {
@@ -560,7 +643,9 @@ export function initialize(): void {
url: "/json/streams/" + stream_id + "/email_address",
success(data) {
const address = z.object({email: z.string()}).parse(data).email;
- show_stream_email_address_modal(address);
+ const sub = sub_store.get(stream_id);
+ assert(sub !== undefined);
+ show_stream_email_address_modal(address, sub);
},
error(xhr) {
ui_report.error(
diff --git a/web/styles/subscriptions.css b/web/styles/subscriptions.css
index 0ab2bfcac19..8857625569f 100644
--- a/web/styles/subscriptions.css
+++ b/web/styles/subscriptions.css
@@ -21,6 +21,9 @@
}
.stream-email {
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
font-family: "Source Code Pro", monospace;
padding: 10px;
font-size: 0.85rem;
@@ -31,12 +34,16 @@
.stream-email .email-address {
display: block;
- margin: auto;
+ flex: 0 0 90%;
word-wrap: break-word;
word-break: break-all;
white-space: normal;
}
+.copy-email-address {
+ font-size: 16px;
+}
+
.sub_setting_checkbox {
display: flex;
diff --git a/web/templates/stream_settings/copy_email_address_modal.hbs b/web/templates/stream_settings/copy_email_address_modal.hbs
index a9cd35ba25b..5c7131e07db 100644
--- a/web/templates/stream_settings/copy_email_address_modal.hbs
+++ b/web/templates/stream_settings/copy_email_address_modal.hbs
@@ -1,6 +1,9 @@
<div class="copy-email-modal">
+ {{> ../dropdown_widget_with_label
+ widget_name="sender_channel_email_address"
+ label=(t 'Who should be the sender of the Zulip messages for this email address?')}}
<p class="question-which-parts">
- {{t "Which parts of the email should be included in the Zulip message sent to this channel?"}}
+ {{t "Which parts of the emails should be included in the Zulip messages?"}}
</p>
{{#each tags}}
{{#if (eq this.name "prefer-html") }}
@@ -20,5 +23,8 @@
</p>
<div class="stream-email">
<div class="email-address">{{email_address}}</div>
+ <span class="copy-button tippy-zulip-tooltip copy-email-address" data-tippy-content="{{t 'Copy email address' }}" data-clipboard-text="{{email_address}}">
+ <i class="zulip-icon zulip-icon-copy"></i>
+ </span>
</div>
</div>
diff --git a/web/tests/people.test.cjs b/web/tests/people.test.cjs
index c13009e5508..f2a2d905cd2 100644
--- a/web/tests/people.test.cjs
+++ b/web/tests/people.test.cjs
@@ -38,6 +38,14 @@ const welcome_bot = {
// cross realm bots have no owner
};
+const email_gateway_bot = {
+ email: "[email protected]",
+ user_id: 5,
+ full_name: "Email Gateway",
+ is_bot: true,
+ // cross realm bots have no owner
+};
+
const me = {
user_id: 30,
@@ -378,9 +386,12 @@ test_people("basics", ({override}) => {
// Add our cross-realm bot. It won't add to our human
// count, and it has no owner.
people.add_cross_realm_user(welcome_bot);
+ people.add_cross_realm_user(email_gateway_bot);
assert.equal(people.get_bot_owner_user(welcome_bot), undefined);
+ assert.equal(people.get_bot_owner_user(email_gateway_bot), undefined);
assert.equal(people.get_active_human_count(), 1);
assert.equal(people.get_by_email(welcome_bot.email).full_name, "Welcome Bot");
+ assert.equal(people.get_by_email(email_gateway_bot.email).full_name, "Email Gateway");
override(settings_data, "user_can_access_all_other_users", () => false);
assert.equal(
@@ -400,7 +411,11 @@ test_people("basics", ({override}) => {
// get_bot_ids() includes all bot users.
bot_user_ids = people.get_bot_ids();
- assert.deepEqual(bot_user_ids, [bot_botson.user_id, welcome_bot.user_id]);
+ assert.deepEqual(bot_user_ids, [
+ bot_botson.user_id,
+ welcome_bot.user_id,
+ email_gateway_bot.user_id,
+ ]);
// The bot doesn't add to our human count.
assert.equal(people.get_active_human_count(), 1);