stream_edit: Use user_ids for subscribing/unsubscribing users to a stream.

We now send user_ids to the backend API for subscribing/unsubscribing
users to a stream instead of emails.

This change is done now because we have just migrated the backend API to
support sending user_ids in 2187c84, so it wasn't possible before.

This change is helpful because sending user_ids is more robust, as those
are an immutable reference to a user, rather than something that can
change with time.
This commit is contained in:
sahil839 2020-06-04 01:08:43 +05:30 committed by Tim Abbott
parent 44995e36df
commit 48ac1082c1
4 changed files with 18 additions and 19 deletions

View File

@ -1261,10 +1261,16 @@ run_test('on_events', () => {
name: 'test',
subscribed: true,
};
const mentioned = {
full_name: 'Foo Barson',
email: 'foo@bar.com',
user_id: 34,
};
people.add_active_user(mentioned);
let invite_user_to_stream_called = false;
stream_edit.invite_user_to_stream = function (email, sub, success) {
stream_edit.invite_user_to_stream = function (user_ids, sub, success) {
invite_user_to_stream_called = true;
assert.deepEqual(email, ['foo@bar.com']);
assert.deepEqual(user_ids, [mentioned.user_id]);
assert.equal(sub, subscription);
success(); // This will check success callback path.
};
@ -1286,7 +1292,7 @@ run_test('on_events', () => {
$('#stream_message_recipient_stream').val('no-stream');
helper.container.data = function (field) {
assert.equal(field, 'useremail');
return 'foo@bar.com';
return mentioned.email;
};
$("#compose-textarea").select(noop);
helper.target.prop('disabled', false);

View File

@ -998,7 +998,7 @@ exports.initialize = function () {
if (email === undefined) {
return;
}
const user_id = people.get_user_id(email);
function success() {
const all_invites = $("#compose_invite_users");
invite_row.remove();
@ -1031,7 +1031,7 @@ exports.initialize = function () {
return;
}
stream_edit.invite_user_to_stream([email], sub, success, xhr_failure);
stream_edit.invite_user_to_stream([user_id], sub, success, xhr_failure);
});
$("#compose_invite_users").on('click', '.compose_invite_close', function (event) {

View File

@ -176,9 +176,7 @@ function create_stream() {
// TODO: We can eliminate the user_ids -> principals conversion
// once we upgrade the backend to accept user_ids.
const user_ids = get_principals();
const persons = user_ids.map(user_id => people.get_by_user_id(user_id)).filter(Boolean);
const principals = persons.map(person => person.email);
data.principals = JSON.stringify(principals);
data.principals = JSON.stringify(user_ids);
loading.make_indicator($('#stream_creating_indicator'), {text: i18n.t('Creating stream...')});

View File

@ -129,25 +129,25 @@ exports.update_stream_description = function (sub) {
);
};
exports.invite_user_to_stream = function (emails, sub, success, failure) {
exports.invite_user_to_stream = function (user_ids, sub, success, failure) {
// TODO: use stream_id when backend supports it
const stream_name = sub.name;
return channel.post({
url: "/json/users/me/subscriptions",
data: {subscriptions: JSON.stringify([{name: stream_name}]),
principals: JSON.stringify(emails)},
principals: JSON.stringify(user_ids)},
success: success,
error: failure,
});
};
exports.remove_user_from_stream = function (user_email, sub, success, failure) {
exports.remove_user_from_stream = function (user_id, sub, success, failure) {
// TODO: use stream_id when backend supports it
const stream_name = sub.name;
return channel.del({
url: "/json/users/me/subscriptions",
data: {subscriptions: JSON.stringify([stream_name]),
principals: JSON.stringify([user_email])},
principals: JSON.stringify([user_id])},
success: success,
error: failure,
});
@ -559,10 +559,6 @@ exports.initialize = function () {
}
const user_ids = user_pill.get_user_ids(exports.pill_widget);
const emails = user_ids.map(user_id => {
const person = people.get_by_user_id(user_id);
return person.email;
});
const stream_subscription_info_elem = $('.stream_subscription_info').expectOne();
function invite_success(data) {
@ -586,7 +582,7 @@ exports.initialize = function () {
.addClass("text-error").removeClass("text-success");
}
exports.invite_user_to_stream(emails, sub, invite_success, invite_failure);
exports.invite_user_to_stream(user_ids, sub, invite_success, invite_failure);
});
$("#subscriptions_table").on("submit", ".subscriber_list_remove form", function (e) {
@ -594,7 +590,6 @@ exports.initialize = function () {
const list_entry = $(e.target).closest("tr");
const target_user_id = parseInt(list_entry.attr("data-subscriber-id"), 10);
const principal = people.get_by_user_id(target_user_id).email;
const settings_row = $(e.target).closest('.subscription_settings');
const sub = get_sub_for_target(settings_row);
@ -622,7 +617,7 @@ exports.initialize = function () {
.addClass("text-error").removeClass("text-success");
}
exports.remove_user_from_stream(principal, sub, removal_success,
exports.remove_user_from_stream(target_user_id, sub, removal_success,
removal_failure);
});