var compose = (function () { var exports = {}; /* Track the state of the @all warning. The user must acknowledge that they are spamming the entire stream before the warning will go away. If they try to send before explicitly dismissing the warning, they will get an error message too. undefined: no @all/@everyone in message; false: user typed @all/@everyone; true: user clicked YES */ var user_acknowledged_all_everyone; exports.all_everyone_warn_threshold = 15; exports.uploads_domain = document.location.protocol + '//' + document.location.host; exports.uploads_path = '/user_uploads'; exports.uploads_re = new RegExp("\\]\\(" + exports.uploads_domain + "(" + exports.uploads_path + "[^\\)]+)\\)", 'g'); exports.clone_file_input = undefined; function make_upload_absolute(uri) { if (uri.indexOf(exports.uploads_path) === 0) { // Rewrite the URI to a usable link return exports.uploads_domain + uri; } return uri; } function make_uploads_relative(content) { // Rewrite uploads in markdown links back to domain-relative form return content.replace(exports.uploads_re, "]($1)"); } // This function resets an input type="file". Pass in the // jquery object. function clear_out_file_list(jq_file_list) { if (exports.clone_file_input !== undefined) { jq_file_list.replaceWith(exports.clone_file_input.clone(true)); } // Hack explanation: // IE won't let you do this (untested, but so says StackOverflow): // $("#file_input").val(""); } exports.uploadStarted = function () { $("#compose-send-button").attr("disabled", ""); $("#send-status").addClass("alert-info") .show(); $(".send-status-close").one('click', exports.abort_xhr); $("#error-msg").html( $("
").text(i18n.t("Uploading…")) .after('
The stream " + Handlebars.Utils.escapeExpression(stream_name) + " does not exist.
" + "Manage your subscriptions on your Streams page.
"; compose_error(response, $('#stream')); return false; case "error": compose_error(i18n.t("Error checking subscription"), $("#stream")); return false; case "not-subscribed": response = "You're not subscribed to the stream " + Handlebars.Utils.escapeExpression(stream_name) + ".
" + "Manage your subscriptions on your Streams page.
"; compose_error(response, $('#stream')); return false; } return true; }; function validate_stream_message() { var stream_name = compose_state.stream_name(); if (stream_name === "") { compose_error(i18n.t("Please specify a stream"), $("#stream")); return false; } if (page_params.realm_mandatory_topics) { var topic = compose_state.subject(); if (topic === "") { compose_error(i18n.t("Please specify a topic"), $("#subject")); return false; } } if (!exports.validate_stream_message_address_info(stream_name) || !validate_stream_message_mentions(stream_name)) { return false; } return true; } // The function checks whether the recipients are users of the realm or cross realm users (bots // for now) function validate_private_message() { if (compose_state.recipient() === "") { compose_error(i18n.t("Please specify at least one recipient"), $("#private_message_recipient")); return false; } else if (page_params.realm_is_zephyr_mirror_realm) { // For Zephyr mirroring realms, the frontend doesn't know which users exist return true; } var invalid_recipients = exports.get_invalid_recipient_emails(); var context = {}; if (invalid_recipients.length === 1) { context = {recipient: invalid_recipients.join()}; compose_error(i18n.t("The recipient __recipient__ is not valid", context), $("#private_message_recipient")); return false; } else if (invalid_recipients.length > 1) { context = {recipients: invalid_recipients.join()}; compose_error(i18n.t("The recipients __recipients__ are not valid", context), $("#private_message_recipient")); return false; } return true; } exports.validate = function () { $("#compose-send-button").attr('disabled', 'disabled').blur(); $("#sending-indicator").show(); if (/^\s*$/.test(compose_state.message_content())) { compose_error(i18n.t("You have nothing to send!"), $("#new_message_content")); return false; } if ($("#zephyr-mirror-error").is(":visible")) { compose_error(i18n.t("You need to be running Zephyr mirroring in order to send messages!")); return false; } if (compose_state.get_message_type() === 'private') { return validate_private_message(); } return validate_stream_message(); }; exports.initialize = function () { $('#stream,#subject,#private_message_recipient').on('keyup', update_fade); $('#stream,#subject,#private_message_recipient').on('change', update_fade); $("#compose form").on("submit", function (e) { e.preventDefault(); compose.finish(); }); resize.watch_manual_resize("#new_message_content"); // Run a feature test and decide whether to display // the "Attach files" button if (window.XMLHttpRequest && (new XMLHttpRequest()).upload) { $("#compose #attach_files").removeClass("notdisplayed"); } // Lazy load the Dropbox script, since it can slow our page load // otherwise, and isn't enabled for all users. Also, this Dropbox // script isn't under an open source license, so we can't (for legal // reasons) minify it with our own code. if (feature_flags.dropbox_integration) { LazyLoad.js('https://www.dropbox.com/static/api/1/dropins.js', function () { // Successful load. We should now have window.Dropbox. if (! _.has(window, 'Dropbox')) { blueslip.error('Dropbox script reports loading but window.Dropbox undefined'); } else if (Dropbox.isBrowserSupported()) { Dropbox.init({appKey: window.dropboxAppKey}); $("#compose #attach_dropbox_files").removeClass("notdisplayed"); } }); } // Show a warning if a user @-mentions someone who will not receive this message $(document).on('usermention_completed.zulip', function (event, data) { if (compose_state.get_message_type() !== 'stream') { return; } // Disable for Zephyr mirroring realms, since we never have subscriber lists there if (page_params.realm_is_zephyr_mirror_realm) { return; } if (data !== undefined && data.mentioned !== undefined) { var email = data.mentioned.email; // warn if @all or @everyone is mentioned if (data.mentioned.full_name === 'all' || data.mentioned.full_name === 'everyone') { return; // don't check if @all or @everyone is subscribed to a stream } if (compose_fade.would_receive_message(email) === false) { var error_area = $("#compose_invite_users"); var existing_invites_area = $('#compose_invite_users .compose_invite_user'); var existing_invites = _.map($(existing_invites_area), function (user_row) { return $(user_row).data('useremail'); }); if (existing_invites.indexOf(email) === -1) { var context = {email: email, name: data.mentioned.full_name}; var new_row = templates.render("compose-invite-users", context); error_area.append(new_row); } error_area.show(); } } // User group mentions will fall through here. In the future, // we may want to add some sort of similar warning for cases // where nobody in the group is subscribed, but that decision // can wait on user feedback. }); $("#compose-all-everyone").on('click', '.compose-all-everyone-confirm', function (event) { event.preventDefault(); $(event.target).parents('.compose-all-everyone').remove(); user_acknowledged_all_everyone = true; exports.clear_all_everyone_warnings(); compose.finish(); }); $("#compose_invite_users").on('click', '.compose_invite_link', function (event) { event.preventDefault(); var invite_row = $(event.target).parents('.compose_invite_user'); var email = $(invite_row).data('useremail'); if (email === undefined) { return; } function success() { var all_invites = $("#compose_invite_users"); invite_row.remove(); if (all_invites.children().length === 0) { all_invites.hide(); } } function failure() { var error_msg = invite_row.find('.compose_invite_user_error'); error_msg.show(); $(event.target).attr('disabled', true); } var stream_name = compose_state.stream_name(); var sub = stream_data.get_sub(stream_name); if (!sub) { // This should only happen if a stream rename occurs // before the user clicks. We could prevent this by // putting a stream id in the link. blueslip.warn('Stream no longer exists: ' + stream_name); failure(); return; } stream_edit.invite_user_to_stream(email, sub, success, failure); }); $("#compose_invite_users").on('click', '.compose_invite_close', function (event) { var invite_row = $(event.target).parents('.compose_invite_user'); var all_invites = $("#compose_invite_users"); invite_row.remove(); if (all_invites.children().length === 0) { all_invites.hide(); } }); // Show a warning if a private stream is linked $(document).on('streamname_completed.zulip', function (event, data) { // For PMs, we don't warn about links to private streams, since // you are often specifically encouraging somebody to subscribe // to the stream over PMs. if (compose_state.get_message_type() !== 'stream') { return; } if (data === undefined || data.stream === undefined) { blueslip.error('Invalid options passed into handler.'); return; } // data.stream refers to the stream we're linking to in // typeahead. If it's not invite-only, then it's public, and // there is no need to warn about it, since all users can already // see all the public streams. if (!data.stream.invite_only) { return; } var stream_name = data.stream.name; var warning_area = $("#compose_private_stream_alert"); var context = { stream_name: stream_name }; var new_row = templates.render("compose_private_stream_alert", context); warning_area.append(new_row); warning_area.show(); }); $("#compose_private_stream_alert").on('click', '.compose_private_stream_alert_close', function (event) { var stream_alert_row = $(event.target).parents('.compose_private_stream_alert'); var stream_alert = $("#compose_private_stream_alert"); stream_alert_row.remove(); if (stream_alert.children().length === 0) { stream_alert.hide(); } }); // Click event binding for "Attach files" button // Triggers a click on a hidden file input field $("#compose").on("click", "#attach_files", function (e) { e.preventDefault(); if (exports.clone_file_input === undefined) { exports.clone_file_input = $('#file_input').clone(true); } $("#compose #file_input").trigger("click"); }); function show_preview(rendered_content) { var preview_html; if (rendered_content.indexOf("/me ") === 0) { // Handle previews of /me messages preview_html = "" + page_params.full_name + " " + rendered_content.slice(4 + 3, -4); } else { preview_html = rendered_content; } $("#preview_content").html(preview_html); if (page_params.emoji_alt_code) { $("#preview_content").find(".emoji").replaceWith(function () { var text = $(this).attr("title"); return ":" + text + ":"; }); } } $('#compose').on('click', '#video_link', function (e) { e.preventDefault(); var video_call_id = util.random_int(100000000000000, 999999999999999); var video_call_link = 'https://meet.jit.si/' + video_call_id; var video_call_link_text = '[' + _('Click to join video call') + '](' + video_call_link + ')'; compose_ui.insert_syntax_and_focus(video_call_link_text); }); $("#compose").on("click", "#markdown_preview", function (e) { e.preventDefault(); var content = $("#new_message_content").val(); $("#new_message_content").hide(); $("#markdown_preview").hide(); $("#undo_markdown_preview").show(); $("#preview_message_area").show(); if (content.length === 0) { show_preview(i18n.t("Nothing to preview")); } else { if (markdown.contains_backend_only_syntax(content)) { var spinner = $("#markdown_preview_spinner").expectOne(); loading.make_indicator(spinner); } else { // For messages that don't appear to contain // bugdown-specific syntax not present in our // marked.js frontend processor, we render using the // frontend markdown processor message (but still // render server-side to ensure the preview is // accurate; if the `markdown.contains_backend_only_syntax` logic is // incorrect wrong, users will see a brief flicker). var message_obj = { raw_content: content, }; markdown.apply_markdown(message_obj); } channel.post({ url: '/json/messages/render', idempotent: true, data: {content: content}, success: function (response_data) { if (markdown.contains_backend_only_syntax(content)) { loading.destroy_indicator($("#markdown_preview_spinner")); } show_preview(response_data.rendered); }, error: function () { if (markdown.contains_backend_only_syntax(content)) { loading.destroy_indicator($("#markdown_preview_spinner")); } show_preview(i18n.t("Failed to generate preview")); }, }); } }); $("#compose").on("click", "#undo_markdown_preview", function (e) { e.preventDefault(); exports.clear_preview_area(); }); $("#compose").on("click", "#attach_dropbox_files", function (e) { e.preventDefault(); var options = { // Required. Called when a user selects an item in the Chooser. success: function (files) { var textbox = $("#new_message_content"); var links = _.map(files, function (file) { return '[' + file.name + '](' + file.link +')'; }) .join(' ') + ' '; textbox.val(textbox.val() + links); }, // Optional. A value of false (default) limits selection to a single file, while // true enables multiple file selection. multiselect: true, iframe: true, }; Dropbox.choose(options); }); $("#compose").filedrop({ url: "/json/user_uploads", fallback_id: "file_input", paramname: "file", maxfilesize: page_params.maxfilesize, data: { // the token isn't automatically included in filedrop's post csrfmiddlewaretoken: csrf_token, }, raw_droppable: ['text/uri-list', 'text/plain'], drop: exports.uploadStarted, progressUpdated: exports.progressUpdated, error: exports.uploadError, uploadFinished: exports.uploadFinished, rawDrop: function (contents) { var textbox = $("#new_message_content"); if (!compose_state.composing()) { compose_actions.start('stream'); } textbox.val(textbox.val() + contents); compose_ui.autosize_textarea(); }, }); if (page_params.narrow !== undefined) { if (page_params.narrow_topic !== undefined) { compose_actions.start("stream", {subject: page_params.narrow_topic}); } else { compose_actions.start("stream", {}); } } }; return exports; }()); if (typeof module !== 'undefined') { module.exports = compose; }