2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-10 16:55:52 +01:00
|
|
|
import render_announce_stream_docs from "../templates/announce_stream_docs.hbs";
|
2021-06-21 09:47:45 +02:00
|
|
|
import render_subscription_invites_warning_modal from "../templates/confirm_dialog/confirm_subscription_invites_warning.hbs";
|
2021-04-22 23:40:09 +02:00
|
|
|
import render_new_stream_user from "../templates/new_stream_user.hbs";
|
2021-02-10 16:55:52 +01:00
|
|
|
import render_new_stream_users from "../templates/new_stream_users.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
import * as channel from "./channel";
|
2021-05-07 19:11:16 +02:00
|
|
|
import * as confirm_dialog from "./confirm_dialog";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t, $t_html} from "./i18n";
|
2021-04-22 23:40:09 +02:00
|
|
|
import * as ListWidget from "./list_widget";
|
2021-02-28 00:36:14 +01:00
|
|
|
import * as loading from "./loading";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-10 16:55:52 +01:00
|
|
|
import * as peer_data from "./peer_data";
|
|
|
|
import * as people from "./people";
|
2021-11-11 14:56:40 +01:00
|
|
|
import * as settings_data from "./settings_data";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-04-04 15:15:18 +02:00
|
|
|
import * as stream_settings_data from "./stream_settings_data";
|
2021-07-09 15:51:31 +02:00
|
|
|
import * as stream_settings_ui from "./stream_settings_ui";
|
2021-02-28 00:58:55 +01:00
|
|
|
import * as ui_report from "./ui_report";
|
2020-08-20 21:24:06 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let created_stream;
|
2021-04-22 23:40:09 +02:00
|
|
|
let all_users;
|
|
|
|
let all_users_list_widget;
|
2017-04-22 22:22:25 +02:00
|
|
|
|
2021-02-10 16:55:52 +01:00
|
|
|
export function reset_created_stream() {
|
2017-04-22 22:22:25 +02:00
|
|
|
created_stream = undefined;
|
2021-02-10 16:55:52 +01:00
|
|
|
}
|
2017-04-22 22:22:25 +02:00
|
|
|
|
2021-02-10 16:55:52 +01:00
|
|
|
export function set_name(stream) {
|
2017-04-22 22:22:25 +02:00
|
|
|
created_stream = stream;
|
2021-02-10 16:55:52 +01:00
|
|
|
}
|
2017-04-22 22:22:25 +02:00
|
|
|
|
2021-02-10 16:55:52 +01:00
|
|
|
export function get_name() {
|
2017-04-22 22:22:25 +02:00
|
|
|
return created_stream;
|
2021-02-10 16:55:52 +01:00
|
|
|
}
|
2017-04-22 22:22:25 +02:00
|
|
|
|
2020-07-23 02:37:12 +02:00
|
|
|
class StreamSubscriptionError {
|
|
|
|
report_no_subs_to_stream() {
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#stream_subscription_error").text(
|
2021-04-13 06:51:54 +02:00
|
|
|
$t({defaultMessage: "You cannot create a stream with no subscribers!"}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2018-01-21 09:34:50 +01:00
|
|
|
$("#stream_subscription_error").show();
|
2020-07-23 02:37:12 +02:00
|
|
|
}
|
2018-01-21 09:34:50 +01:00
|
|
|
|
2020-07-23 02:37:12 +02:00
|
|
|
cant_create_stream_without_susbscribing() {
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#stream_subscription_error").text(
|
2021-04-13 06:51:54 +02:00
|
|
|
$t({
|
|
|
|
defaultMessage:
|
|
|
|
"You must be an organization administrator to create a stream without subscribing.",
|
|
|
|
}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2018-02-03 09:04:45 +01:00
|
|
|
$("#stream_subscription_error").show();
|
2020-07-23 02:37:12 +02:00
|
|
|
}
|
2018-02-03 09:04:45 +01:00
|
|
|
|
2020-07-23 02:37:12 +02:00
|
|
|
clear_errors() {
|
2018-01-21 09:34:50 +01:00
|
|
|
$("#stream_subscription_error").hide();
|
2020-07-23 02:37:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const stream_subscription_error = new StreamSubscriptionError();
|
2018-01-21 09:34:50 +01:00
|
|
|
|
2020-07-23 02:39:41 +02:00
|
|
|
class StreamNameError {
|
|
|
|
report_already_exists() {
|
2021-04-13 06:51:54 +02:00
|
|
|
$("#stream_name_error").text(
|
|
|
|
$t({defaultMessage: "A stream with this name already exists"}),
|
|
|
|
);
|
2017-04-28 22:04:32 +02:00
|
|
|
$("#stream_name_error").show();
|
2020-07-23 02:39:41 +02:00
|
|
|
}
|
2017-04-28 22:04:32 +02:00
|
|
|
|
2020-07-23 02:39:41 +02:00
|
|
|
clear_errors() {
|
2017-04-28 22:04:32 +02:00
|
|
|
$("#stream_name_error").hide();
|
2020-07-23 02:39:41 +02:00
|
|
|
}
|
2017-04-28 22:04:32 +02:00
|
|
|
|
2020-07-23 02:39:41 +02:00
|
|
|
report_empty_stream() {
|
2021-04-13 06:51:54 +02:00
|
|
|
$("#stream_name_error").text($t({defaultMessage: "A stream needs to have a name"}));
|
2017-04-28 22:04:32 +02:00
|
|
|
$("#stream_name_error").show();
|
2020-07-23 02:39:41 +02:00
|
|
|
}
|
2017-04-28 22:04:32 +02:00
|
|
|
|
2020-07-23 02:39:41 +02:00
|
|
|
select() {
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#create_stream_name").trigger("focus").trigger("select");
|
2020-07-23 02:39:41 +02:00
|
|
|
}
|
2017-04-28 22:04:32 +02:00
|
|
|
|
2020-07-23 02:39:41 +02:00
|
|
|
pre_validate(stream_name) {
|
2017-04-28 22:04:32 +02:00
|
|
|
// Don't worry about empty strings...we just want to call this
|
|
|
|
// to warn users early before they start doing too much work
|
|
|
|
// after they make the effort to type in a stream name. (The
|
|
|
|
// use case here is that I go to create a stream, only to find
|
|
|
|
// out it already exists, and I was just too lazy to look at
|
|
|
|
// the public streams that I'm not subscribed to yet. Once I
|
|
|
|
// realize the stream already exists, I may want to cancel.)
|
|
|
|
if (stream_name && stream_data.get_sub(stream_name)) {
|
2020-07-23 02:39:41 +02:00
|
|
|
this.report_already_exists();
|
2017-04-28 22:04:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-23 02:39:41 +02:00
|
|
|
this.clear_errors();
|
|
|
|
}
|
2017-04-28 22:04:32 +02:00
|
|
|
|
2020-07-23 02:39:41 +02:00
|
|
|
validate_for_submit(stream_name) {
|
2017-04-28 22:04:32 +02:00
|
|
|
if (!stream_name) {
|
2020-07-23 02:39:41 +02:00
|
|
|
this.report_empty_stream();
|
|
|
|
this.select();
|
2017-04-28 22:04:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_data.get_sub(stream_name)) {
|
2020-07-23 02:39:41 +02:00
|
|
|
this.report_already_exists();
|
|
|
|
this.select();
|
2017-04-28 22:04:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, then we think we have a new unique stream
|
|
|
|
// name, so we'll submit to the server. (It's still plausible,
|
|
|
|
// however, that there's some invite-only stream that we don't
|
|
|
|
// know about locally that will cause a name collision.)
|
|
|
|
return true;
|
2020-07-23 02:39:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const stream_name_error = new StreamNameError();
|
2017-04-28 22:04:32 +02:00
|
|
|
|
2017-04-22 22:22:25 +02:00
|
|
|
// Within the new stream modal...
|
|
|
|
function update_announce_stream_state() {
|
2017-05-16 18:34:36 +02:00
|
|
|
// If there is no notifications_stream, we simply hide the widget.
|
2020-04-15 18:29:26 +02:00
|
|
|
if (!stream_data.realm_has_notifications_stream()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#announce-new-stream").hide();
|
2017-05-16 18:34:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-29 11:02:24 +01:00
|
|
|
// If the stream is invite only, disable the "Announce stream" option.
|
|
|
|
// Otherwise enable it.
|
2020-07-15 01:29:15 +02:00
|
|
|
const announce_stream_checkbox = $("#announce-new-stream input");
|
|
|
|
const announce_stream_label = $("#announce-new-stream");
|
2019-11-02 00:06:25 +01:00
|
|
|
let disable_it = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
const privacy_type = $("input:radio[name=privacy]:checked").val();
|
2020-07-15 00:34:28 +02:00
|
|
|
const is_invite_only =
|
|
|
|
privacy_type === "invite-only" || privacy_type === "invite-only-public-history";
|
2017-12-29 11:06:12 +01:00
|
|
|
announce_stream_label.removeClass("control-label-disabled");
|
2017-04-22 22:22:25 +02:00
|
|
|
|
|
|
|
if (is_invite_only) {
|
|
|
|
disable_it = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
announce_stream_checkbox.prop("checked", false);
|
2017-12-29 11:06:12 +01:00
|
|
|
announce_stream_label.addClass("control-label-disabled");
|
2017-04-22 22:22:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
announce_stream_checkbox.prop("disabled", disable_it);
|
|
|
|
$("#announce-new-stream").show();
|
2017-04-22 22:22:25 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 04:04:55 +02:00
|
|
|
function get_principals() {
|
2021-04-22 23:40:09 +02:00
|
|
|
// Return list of user ids which were selected by user.
|
|
|
|
return all_users.filter((user) => user.checked === true).map((user) => user.user_id);
|
2017-07-08 04:04:55 +02:00
|
|
|
}
|
|
|
|
|
2017-07-08 04:07:36 +02:00
|
|
|
function create_stream() {
|
2020-02-08 06:45:49 +01:00
|
|
|
const data = {};
|
2020-07-22 03:39:41 +02:00
|
|
|
const stream_name = $("#create_stream_name").val().trim();
|
|
|
|
const description = $("#create_stream_description").val().trim();
|
2020-02-08 06:45:49 +01:00
|
|
|
created_stream = stream_name;
|
|
|
|
|
|
|
|
// Even though we already check to make sure that while typing the user cannot enter
|
2020-08-11 02:09:14 +02:00
|
|
|
// newline characters (by pressing the Enter key) it would still be possible to copy
|
2020-02-08 06:45:49 +01:00
|
|
|
// and paste over a description with newline characters in it. Prevent that.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (description.includes("\n")) {
|
2021-01-27 08:16:28 +01:00
|
|
|
ui_report.client_error(
|
2021-04-13 05:18:25 +02:00
|
|
|
$t_html({defaultMessage: "The stream description cannot contain newline characters."}),
|
2020-07-15 00:34:28 +02:00
|
|
|
$(".stream_create_info"),
|
|
|
|
);
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-02-08 06:45:49 +01:00
|
|
|
}
|
2020-07-20 22:18:43 +02:00
|
|
|
data.subscriptions = JSON.stringify([{name: stream_name, description}]);
|
2017-07-08 04:07:36 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let invite_only;
|
|
|
|
let history_public_to_subscribers;
|
2020-11-10 15:51:21 +01:00
|
|
|
let is_web_public;
|
2020-07-15 01:29:15 +02:00
|
|
|
const privacy_setting = $("#stream_creation_form input[name=privacy]:checked").val();
|
2020-02-04 21:50:55 +01:00
|
|
|
|
2021-06-16 04:29:08 +02:00
|
|
|
switch (privacy_setting) {
|
|
|
|
case "invite-only": {
|
|
|
|
invite_only = true;
|
|
|
|
history_public_to_subscribers = false;
|
2020-11-10 15:51:21 +01:00
|
|
|
is_web_public = false;
|
2021-06-16 04:29:08 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "invite-only-public-history": {
|
|
|
|
invite_only = true;
|
|
|
|
history_public_to_subscribers = true;
|
2020-11-10 15:51:21 +01:00
|
|
|
is_web_public = false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "web-public": {
|
|
|
|
invite_only = false;
|
|
|
|
history_public_to_subscribers = true;
|
|
|
|
is_web_public = true;
|
2021-06-16 04:29:08 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
invite_only = false;
|
|
|
|
history_public_to_subscribers = true;
|
2020-11-10 15:51:21 +01:00
|
|
|
is_web_public = false;
|
2021-06-16 04:29:08 +02:00
|
|
|
}
|
2018-05-03 18:52:39 +02:00
|
|
|
}
|
2020-11-10 15:51:21 +01:00
|
|
|
|
|
|
|
data.is_web_public = JSON.stringify(is_web_public);
|
2020-02-08 06:45:49 +01:00
|
|
|
data.invite_only = JSON.stringify(invite_only);
|
|
|
|
data.history_public_to_subscribers = JSON.stringify(history_public_to_subscribers);
|
2018-05-03 18:52:39 +02:00
|
|
|
|
2020-10-07 09:17:30 +02:00
|
|
|
const stream_post_policy = Number.parseInt(
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#stream_creation_form input[name=stream-post-policy]:checked").val(),
|
|
|
|
10,
|
|
|
|
);
|
2020-02-08 06:45:49 +01:00
|
|
|
|
|
|
|
data.stream_post_policy = JSON.stringify(stream_post_policy);
|
2017-07-08 04:07:36 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
let message_retention_selection = $(
|
|
|
|
"#stream_creation_form select[name=stream_message_retention_setting]",
|
|
|
|
).val();
|
2020-06-15 17:00:00 +02:00
|
|
|
if (message_retention_selection === "retain_for_period") {
|
2020-10-07 09:17:30 +02:00
|
|
|
message_retention_selection = Number.parseInt(
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#stream_creation_form input[name=stream-message-retention-days]").val(),
|
|
|
|
10,
|
|
|
|
);
|
2020-06-15 17:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data.message_retention_days = JSON.stringify(message_retention_selection);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const announce =
|
|
|
|
stream_data.realm_has_notifications_stream() &&
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#announce-new-stream input").prop("checked");
|
2020-02-08 06:45:49 +01:00
|
|
|
data.announce = JSON.stringify(announce);
|
2017-07-08 04:07:36 +02:00
|
|
|
|
2020-02-08 06:45:49 +01:00
|
|
|
// TODO: We can eliminate the user_ids -> principals conversion
|
|
|
|
// once we upgrade the backend to accept user_ids.
|
|
|
|
const user_ids = get_principals();
|
2020-06-03 21:38:43 +02:00
|
|
|
data.principals = JSON.stringify(user_ids);
|
2019-02-20 18:36:46 +01:00
|
|
|
|
2021-04-13 06:51:54 +02:00
|
|
|
loading.make_indicator($("#stream_creating_indicator"), {
|
|
|
|
text: $t({defaultMessage: "Creating stream..."}),
|
|
|
|
});
|
2017-11-04 19:19:12 +01:00
|
|
|
|
2020-02-08 06:45:49 +01:00
|
|
|
// Subscribe yourself and possible other people to a new stream.
|
|
|
|
return channel.post({
|
|
|
|
url: "/json/users/me/subscriptions",
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
|
|
|
success() {
|
2020-02-08 06:45:49 +01:00
|
|
|
$("#create_stream_name").val("");
|
|
|
|
$("#create_stream_description").val("");
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.success(
|
|
|
|
$t_html({defaultMessage: "Stream successfully created!"}),
|
|
|
|
$(".stream_create_info"),
|
|
|
|
);
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#stream_creating_indicator"));
|
2020-02-08 06:45:49 +01:00
|
|
|
// The rest of the work is done via the subscribe event we will get
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2020-02-08 06:45:49 +01:00
|
|
|
const msg = JSON.parse(xhr.responseText).msg;
|
2020-07-15 01:29:15 +02:00
|
|
|
if (msg.includes("access")) {
|
2020-02-08 06:45:49 +01:00
|
|
|
// If we can't access the stream, we can safely assume it's
|
|
|
|
// a duplicate stream that we are not invited to.
|
|
|
|
//
|
|
|
|
// BUG: This check should be using error codes, not
|
|
|
|
// parsing the error string, so it works correctly
|
|
|
|
// with i18n. And likely we should be reporting the
|
|
|
|
// error text directly rather than turning it into
|
|
|
|
// "Error creating stream"?
|
|
|
|
stream_name_error.report_already_exists(stream_name);
|
2020-07-20 21:24:26 +02:00
|
|
|
stream_name_error.trigger("select");
|
2020-02-08 06:45:49 +01:00
|
|
|
}
|
|
|
|
|
2021-04-13 05:18:25 +02:00
|
|
|
ui_report.error(
|
|
|
|
$t_html({defaultMessage: "Error creating stream"}),
|
|
|
|
xhr,
|
|
|
|
$(".stream_create_info"),
|
|
|
|
);
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#stream_creating_indicator"));
|
2020-02-08 06:45:49 +01:00
|
|
|
},
|
|
|
|
});
|
2017-07-08 04:07:36 +02:00
|
|
|
}
|
2017-06-02 15:06:33 +02:00
|
|
|
|
2021-02-10 16:55:52 +01:00
|
|
|
export function new_stream_clicked(stream_name) {
|
2017-04-22 22:22:25 +02:00
|
|
|
// this changes the tab switcher (settings/preview) which isn't necessary
|
|
|
|
// to a add new stream title.
|
2021-07-09 15:51:31 +02:00
|
|
|
stream_settings_ui.show_subs_pane.create_stream();
|
2017-04-22 22:22:25 +02:00
|
|
|
$(".stream-row.active").removeClass("active");
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (stream_name !== "") {
|
|
|
|
$("#create_stream_name").val(stream_name);
|
2017-04-22 22:22:25 +02:00
|
|
|
}
|
2021-02-10 16:55:52 +01:00
|
|
|
show_new_stream_modal();
|
2020-11-01 04:05:19 +01:00
|
|
|
$("#create_stream_name").trigger("focus");
|
2021-02-10 16:55:52 +01:00
|
|
|
}
|
2017-04-22 22:22:25 +02:00
|
|
|
|
2018-03-10 03:14:48 +01:00
|
|
|
function clear_error_display() {
|
|
|
|
stream_name_error.clear_errors();
|
|
|
|
$(".stream_create_info").hide();
|
|
|
|
stream_subscription_error.clear_errors();
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:55:52 +01:00
|
|
|
export function show_new_stream_modal() {
|
2017-04-22 22:22:25 +02:00
|
|
|
$("#stream-creation").removeClass("hide");
|
|
|
|
$(".right .settings").hide();
|
2021-11-19 10:29:39 +01:00
|
|
|
stream_settings_ui.hide_or_disable_stream_privacy_options_if_required($("#stream-creation"));
|
2017-11-26 12:57:03 +01:00
|
|
|
|
2021-04-22 23:40:09 +02:00
|
|
|
const add_people_container = $("#people_to_add");
|
|
|
|
add_people_container.html(
|
|
|
|
render_new_stream_users({
|
2021-04-04 15:15:18 +02:00
|
|
|
streams: stream_settings_data.get_streams_for_settings_page(),
|
2021-04-22 23:40:09 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
all_users = people.get_people_for_stream_create();
|
|
|
|
// Add current user on top of list
|
|
|
|
const current_user = people.get_by_user_id(page_params.user_id);
|
|
|
|
all_users.unshift({
|
2021-11-11 14:56:40 +01:00
|
|
|
show_email: settings_data.show_email(),
|
|
|
|
email: people.get_visible_email(current_user),
|
2021-04-22 23:40:09 +02:00
|
|
|
user_id: current_user.user_id,
|
|
|
|
full_name: current_user.full_name,
|
|
|
|
checked: true,
|
|
|
|
disabled: !page_params.is_admin,
|
2018-03-25 18:11:23 +02:00
|
|
|
});
|
|
|
|
|
2021-04-22 23:40:09 +02:00
|
|
|
all_users_list_widget = ListWidget.create($("#user-checkboxes"), all_users, {
|
|
|
|
name: "new_stream_add_users",
|
|
|
|
parent_container: add_people_container,
|
|
|
|
modifier(item) {
|
|
|
|
return render_new_stream_user(item);
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: $("#people_to_add .add-user-list-filter"),
|
|
|
|
predicate(user, search_term) {
|
|
|
|
return people.build_person_matcher(search_term)(user);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
simplebar_container: $("#user-checkboxes-simplebar-wrapper"),
|
|
|
|
html_selector: (user) => $(`#${CSS.escape("user_checkbox_" + user.user_id)}`),
|
|
|
|
});
|
|
|
|
|
2017-04-22 22:22:25 +02:00
|
|
|
// Make the options default to the same each time:
|
|
|
|
// public, "announce stream" on.
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#make-invite-only input:radio[value=public]").prop("checked", true);
|
2020-06-15 17:00:00 +02:00
|
|
|
$("#stream_creation_form .stream-message-retention-days-input").hide();
|
|
|
|
$("#stream_creation_form select[name=stream_message_retention_setting]").val("realm_default");
|
2017-05-16 18:34:36 +02:00
|
|
|
|
2021-04-22 23:40:09 +02:00
|
|
|
update_announce_stream_state();
|
2020-04-15 18:29:26 +02:00
|
|
|
if (stream_data.realm_has_notifications_stream()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#announce-new-stream").show();
|
|
|
|
$("#announce-new-stream input").prop("disabled", false);
|
|
|
|
$("#announce-new-stream input").prop("checked", true);
|
2017-05-16 18:34:36 +02:00
|
|
|
} else {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#announce-new-stream").hide();
|
2017-05-16 18:34:36 +02:00
|
|
|
}
|
2018-03-10 03:14:48 +01:00
|
|
|
clear_error_display();
|
2021-02-10 16:55:52 +01:00
|
|
|
}
|
2017-04-22 22:22:25 +02:00
|
|
|
|
2021-07-15 19:23:21 +02:00
|
|
|
function create_handlers_for_users(container) {
|
2018-03-25 18:11:23 +02:00
|
|
|
// container should be $('#people_to_add')...see caller to verify
|
2021-04-22 23:40:09 +02:00
|
|
|
function update_checked_state_for_users(value, users) {
|
|
|
|
// Update the all_users backing data structure for
|
|
|
|
// which users will be submitted should the user click save,
|
|
|
|
// and also ensure that any visible checkboxes reflect
|
|
|
|
// the state of that data structure.
|
|
|
|
|
|
|
|
// If we have to rerender a very large number of users, it's
|
|
|
|
// eventually faster to just do a full redraw rather than
|
|
|
|
// many hundreds of single-item rerenders.
|
|
|
|
const full_redraw = !users || users.length > 250;
|
|
|
|
for (const user of all_users) {
|
|
|
|
// We don't want to uncheck the user creating the stream if it is not admin.
|
|
|
|
if (user.user_id === page_params.user_id && value === false && !page_params.is_admin) {
|
|
|
|
continue;
|
2017-04-22 22:22:25 +02:00
|
|
|
}
|
2021-04-22 23:40:09 +02:00
|
|
|
// We update for all users if `users` parameter is empty.
|
|
|
|
if (users === undefined || users.includes(user.user_id)) {
|
|
|
|
user.checked = value;
|
2017-04-22 22:22:25 +02:00
|
|
|
|
2021-04-22 23:40:09 +02:00
|
|
|
if (!full_redraw) {
|
|
|
|
all_users_list_widget.render_item(user);
|
2018-02-03 09:04:45 +01:00
|
|
|
}
|
2017-04-22 22:22:25 +02:00
|
|
|
}
|
2021-04-22 23:40:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (full_redraw) {
|
|
|
|
all_users_list_widget.hard_redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
container.on("change", "#user-checkboxes input", (e) => {
|
|
|
|
const elem = $(e.target);
|
|
|
|
const user_id = Number.parseInt(elem.attr("data-user-id"), 10);
|
|
|
|
const checked = elem.prop("checked");
|
|
|
|
update_checked_state_for_users(checked, [user_id]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// 'Check all' and 'Uncheck all' visible users
|
|
|
|
container.on("click", ".subs_set_all_users, .subs_unset_all_users", (e) => {
|
2017-04-22 22:22:25 +02:00
|
|
|
e.preventDefault();
|
2021-04-22 23:40:09 +02:00
|
|
|
// Only `check / uncheck` users who are displayed.
|
|
|
|
const mark_checked = e.target.classList.contains("subs_set_all_users");
|
|
|
|
const users_displayed = all_users_list_widget.get_current_list();
|
|
|
|
if (all_users.length !== users_displayed.length) {
|
|
|
|
update_checked_state_for_users(
|
|
|
|
mark_checked,
|
|
|
|
users_displayed.map((user) => user.user_id),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
update_checked_state_for_users(mark_checked);
|
|
|
|
}
|
2017-04-22 22:22:25 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
container.on("click", "#copy-from-stream-expand-collapse", (e) => {
|
2021-04-22 23:40:09 +02:00
|
|
|
e.preventDefault();
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#stream-checkboxes").toggle();
|
|
|
|
$("#copy-from-stream-expand-collapse .toggle").toggleClass("fa-caret-right fa-caret-down");
|
2017-04-22 22:22:25 +02:00
|
|
|
});
|
|
|
|
|
2021-07-15 18:38:44 +02:00
|
|
|
container.on("change", "#stream-checkboxes label.checkbox", (e) => {
|
2017-04-22 22:22:25 +02:00
|
|
|
e.preventDefault();
|
2021-04-22 23:40:09 +02:00
|
|
|
const elem = $(e.target).closest("[data-stream-id]");
|
|
|
|
const stream_id = Number.parseInt(elem.attr("data-stream-id"), 10);
|
|
|
|
const checked = elem.find("input").prop("checked");
|
|
|
|
const subscriber_ids = peer_data.get_subscribers(stream_id);
|
|
|
|
update_checked_state_for_users(checked, subscriber_ids);
|
2017-04-22 22:22:25 +02:00
|
|
|
});
|
2021-02-10 16:55:52 +01:00
|
|
|
}
|
2018-03-25 17:43:17 +02:00
|
|
|
|
2021-02-10 16:55:52 +01:00
|
|
|
export function set_up_handlers() {
|
2021-07-15 18:38:44 +02:00
|
|
|
// Sets up all the event handlers concerning the `People to add`
|
|
|
|
// section in Create stream UI.
|
|
|
|
const people_to_add_holder = $("#people_to_add").expectOne();
|
|
|
|
create_handlers_for_users(people_to_add_holder);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const container = $("#stream-creation").expectOne();
|
2017-04-22 22:22:25 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
container.on("change", "#make-invite-only input", update_announce_stream_state);
|
2018-03-25 18:46:10 +02:00
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
container.on("submit", "#stream_creation_form", (e) => {
|
2017-04-22 22:22:25 +02:00
|
|
|
e.preventDefault();
|
2018-03-10 03:14:48 +01:00
|
|
|
clear_error_display();
|
|
|
|
|
2020-07-22 03:39:41 +02:00
|
|
|
const stream_name = $("#create_stream_name").val().trim();
|
2019-11-02 00:06:25 +01:00
|
|
|
const name_ok = stream_name_error.validate_for_submit(stream_name);
|
2017-04-28 22:04:32 +02:00
|
|
|
|
|
|
|
if (!name_ok) {
|
|
|
|
return;
|
2017-04-22 22:22:25 +02:00
|
|
|
}
|
2017-06-02 15:06:33 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const principals = get_principals();
|
2018-01-21 09:34:50 +01:00
|
|
|
if (principals.length === 0) {
|
|
|
|
stream_subscription_error.report_no_subs_to_stream();
|
|
|
|
return;
|
|
|
|
}
|
js: Convert a.indexOf(…) !== -1 to a.includes(…).
Babel polyfills this for us for Internet Explorer.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import K from "ast-types/gen/kinds";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
recast.visit(ast, {
visitBinaryExpression(path) {
const { operator, left, right } = path.node;
if (
n.CallExpression.check(left) &&
n.MemberExpression.check(left.callee) &&
!left.callee.computed &&
n.Identifier.check(left.callee.property) &&
left.callee.property.name === "indexOf" &&
left.arguments.length === 1 &&
checkExpression(left.arguments[0]) &&
((["===", "!==", "==", "!=", ">", "<="].includes(operator) &&
n.UnaryExpression.check(right) &&
right.operator == "-" &&
n.Literal.check(right.argument) &&
right.argument.value === 1) ||
([">=", "<"].includes(operator) &&
n.Literal.check(right) &&
right.value === 0))
) {
const test = b.callExpression(
b.memberExpression(left.callee.object, b.identifier("includes")),
[left.arguments[0]]
);
path.replace(
["!==", "!=", ">", ">="].includes(operator)
? test
: b.unaryExpression("!", test)
);
changed = true;
}
this.traverse(path);
},
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-08 04:55:06 +01:00
|
|
|
if (!principals.includes(people.my_current_user_id()) && !page_params.is_admin) {
|
2018-02-03 09:04:45 +01:00
|
|
|
stream_subscription_error.cant_create_stream_without_susbscribing();
|
|
|
|
return;
|
|
|
|
}
|
2018-01-21 09:34:50 +01:00
|
|
|
|
2017-07-08 04:21:19 +02:00
|
|
|
if (principals.length >= 50) {
|
2021-05-07 19:11:16 +02:00
|
|
|
const html_body = render_subscription_invites_warning_modal({
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_name,
|
2019-07-09 21:24:00 +02:00
|
|
|
count: principals.length,
|
|
|
|
});
|
2021-05-07 19:11:16 +02:00
|
|
|
|
|
|
|
confirm_dialog.launch({
|
|
|
|
html_heading: $t_html({defaultMessage: "Large number of subscribers"}),
|
|
|
|
html_body,
|
|
|
|
on_click: () => {
|
|
|
|
create_stream();
|
|
|
|
},
|
|
|
|
});
|
2017-06-02 15:06:33 +02:00
|
|
|
} else {
|
|
|
|
create_stream();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
container.on("input", "#create_stream_name", () => {
|
2020-07-22 03:39:41 +02:00
|
|
|
const stream_name = $("#create_stream_name").val().trim();
|
2017-04-22 22:22:25 +02:00
|
|
|
|
2017-04-28 22:04:32 +02:00
|
|
|
// This is an inexpensive check.
|
2017-07-08 04:31:11 +02:00
|
|
|
stream_name_error.pre_validate(stream_name);
|
2017-04-22 22:22:25 +02:00
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
container.on("mouseover", "#announce-stream-docs", (e) => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const announce_stream_docs = $("#announce-stream-docs");
|
2018-05-06 21:43:17 +02:00
|
|
|
announce_stream_docs.popover({
|
|
|
|
placement: "right",
|
2019-07-09 21:24:00 +02:00
|
|
|
content: render_announce_stream_docs({
|
2020-04-14 12:55:18 +02:00
|
|
|
notifications_stream: stream_data.get_notifications_stream(),
|
|
|
|
}),
|
2019-09-17 23:54:52 +02:00
|
|
|
html: true,
|
2020-07-15 00:34:28 +02:00
|
|
|
trigger: "manual",
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
announce_stream_docs.popover("show");
|
|
|
|
announce_stream_docs.data("popover").tip().css("z-index", 2000);
|
2020-07-15 00:34:28 +02:00
|
|
|
announce_stream_docs
|
|
|
|
.data("popover")
|
|
|
|
.tip()
|
|
|
|
.find(".popover-content")
|
|
|
|
.css("margin", "9px 14px");
|
2017-04-22 22:22:25 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2020-07-02 01:45:54 +02:00
|
|
|
container.on("mouseout", "#announce-stream-docs", (e) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#announce-stream-docs").popover("hide");
|
2017-04-22 22:22:25 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
2019-02-20 18:36:46 +01:00
|
|
|
// Do not allow the user to enter newline characters while typing out the
|
|
|
|
// stream's description during it's creation.
|
2020-07-02 01:45:54 +02:00
|
|
|
container.on("keydown", "#create_stream_description", (e) => {
|
2021-05-29 22:16:37 +02:00
|
|
|
if (e.key === "Enter") {
|
2019-02-20 18:36:46 +01:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2021-02-10 16:55:52 +01:00
|
|
|
}
|