Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI option to subscribe existing accounts during bulk invites. #16041

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
invite: Add UI to subscribe users with existing accounts.
When inviting existing accounts in the realm, the user is presented
with an option to subscribe existing users to the marked streams.
The error option displays which accounts are being subscribed
to which streams.

The actual stream subscription can result in three cases:
    1. All the users were newly subscribed to the selected streams.
    2. All the users were already part of selected streams.
    3. Some users got newly subscribed, while some others were
       already subscribed.
Appropriate error messages are show.

Fixes #15784.
  • Loading branch information
sumanthvrao committed Aug 25, 2020
commit 2e0bc935787baabc5c1734d2704e4a7b733bc782
68 changes: 66 additions & 2 deletions static/js/invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,54 @@ function beforeSend() {
return true;
}

function subscribe_existing_accounts(subscriber_stream_names, subscriber_emails) {
const invite_status = $("#invite_status");
const subscriber_user_ids = [];
subscriber_emails.forEach((email) => {
const person = people.get_by_email(email);
subscriber_user_ids.push(person.user_id);
});

function success(data) {
// At this point one of these three things could have happen.
// 1. All the users were newly subscribed to the selected streams.
// 2. All the users were already part of selected streams.
// 3. Some users got newly subscribed, while some others were already subscribed.
const all_newly_subscribed = !Object.entries(data.already_subscribed).length;
const all_already_subscribed = !Object.entries(data.subscribed).length;
if (all_newly_subscribed) {
ui_report.success(i18n.t("User(s) subscribed successfully."), invite_status);
} else if (all_already_subscribed) {
ui_report.success(
i18n.t("User(s) already subscribed to the selected stream(s)."),
invite_status,
);
} else {
ui_report.success(
i18n.t(
"Some of those addresses were already subscribed to the selected stream(s). We subscribed everyone else!",
),
invite_status,
);
}
}

function failure(xhr) {
// Some fatal error occured.
ui_report.error("", xhr, invite_status);
}

channel.post({
url: "/json/users/me/subscriptions",
data: {
subscriptions: JSON.stringify(subscriber_stream_names),
principals: JSON.stringify(subscriber_user_ids),
},
success,
error: failure,
});
}

function submit_invitation_form() {
const invite_status = $("#invite_status");
const invitee_emails = $("#invitee_emails");
Expand Down Expand Up @@ -72,29 +120,45 @@ function submit_invitation_form() {
} else {
// Some users were not invited.
const invitee_emails_errored = [];
const subscriber_emails_errored = [];
const subscriber_stream_names = [];
const subscriber_stream_ids = JSON.parse(data.stream_ids);
const error_list = [];
let is_invitee_deactivated = false;
arr.errors.forEach((value) => {
const [email, error_message, deactivated] = value;
const [email, error_message, deactivated, maybe_anonymous_email] = value;
error_list.push(`${email}: ${error_message}`);
if (deactivated) {
is_invitee_deactivated = true;
} else {
// If they aren't deactivated, they can still be subscribed.
subscriber_emails_errored.push(maybe_anonymous_email);
}
invitee_emails_errored.push(email);
});

subscriber_stream_ids.forEach((stream_id) => {
subscriber_stream_names.push({name: stream_data.get_sub_by_id(stream_id).name});
});
const error_response = render_invitation_failed_error({
error_message: arr.msg,
error_list,
is_admin: page_params.is_admin,
is_invitee_deactivated,
show_subscription:
arr.show_subscription && subscriber_emails_errored.length > 0,
subscriber_emails_errored,
subscriber_stream_names,
});
ui_report.message(error_response, invite_status, "alert-warning");
invitee_emails_group.addClass("warning");

if (arr.sent_invitations) {
invitee_emails.val(invitee_emails_errored.join("\n"));
}

$("#subscribe_existing_accounts").on("click", () => {
subscribe_existing_accounts(subscriber_stream_names, subscriber_emails_errored);
});
}
},
complete() {
Expand Down
11 changes: 11 additions & 0 deletions static/templates/invitation_failed_error.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@
<p id="invitation_non_admin_message">{{t "Organization administrators can reactivate deactivated users." }}</p>
{{/if}}
{{/if}}
{{#if show_subscription}}
<p id="subscribe_anyway_message">{{t "To subscribe the existing user(s) to the following stream(s), click on
the button below." }}</p>
<ul>
{{#each subscriber_stream_names}}
<li>#{{this.name}}</li>
{{/each}}
</ul>
<button id="subscribe_existing_accounts" class="button small round" type="button">
{{t "Subscribe existing accounts" }}</button>
{{/if}}