2021-12-18 18:47:21 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
|
|
|
import render_unsubscribe_private_stream_modal from "../templates/confirm_dialog/confirm_unsubscribe_private_stream.hbs";
|
|
|
|
import render_stream_member_list_entry from "../templates/stream_settings/stream_member_list_entry.hbs";
|
|
|
|
import render_stream_subscription_request_result from "../templates/stream_settings/stream_subscription_request_result.hbs";
|
|
|
|
|
2022-02-20 13:39:38 +01:00
|
|
|
import * as add_subscribers_pill from "./add_subscribers_pill";
|
2021-12-18 18:47:21 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
|
|
|
import * as confirm_dialog from "./confirm_dialog";
|
2021-12-20 15:34:57 +01:00
|
|
|
import * as hash_util from "./hash_util";
|
2021-12-18 18:47:21 +01:00
|
|
|
import {$t, $t_html} from "./i18n";
|
|
|
|
import * as ListWidget from "./list_widget";
|
|
|
|
import {page_params} from "./page_params";
|
|
|
|
import * as peer_data from "./peer_data";
|
|
|
|
import * as people from "./people";
|
|
|
|
import * as settings_data from "./settings_data";
|
2021-12-20 14:31:25 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-12-18 18:47:21 +01:00
|
|
|
import * as sub_store from "./sub_store";
|
2022-02-21 15:28:56 +01:00
|
|
|
import * as subscriber_api from "./subscriber_api";
|
2021-12-18 18:47:21 +01:00
|
|
|
import * as ui from "./ui";
|
|
|
|
|
|
|
|
export let pill_widget;
|
2021-12-20 15:34:57 +01:00
|
|
|
let current_stream_id;
|
2021-12-20 15:54:10 +01:00
|
|
|
let subscribers_list_widget;
|
2021-12-18 18:47:21 +01:00
|
|
|
|
|
|
|
function format_member_list_elem(person) {
|
|
|
|
return render_stream_member_list_entry({
|
|
|
|
name: person.full_name,
|
|
|
|
user_id: person.user_id,
|
2022-03-04 11:14:01 +01:00
|
|
|
is_current_user: person.user_id === page_params.user_id,
|
2021-12-18 18:47:21 +01:00
|
|
|
email: settings_data.email_for_user_settings(person),
|
|
|
|
displaying_for_admin: page_params.is_admin,
|
|
|
|
show_email: settings_data.show_email(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-19 14:10:03 +01:00
|
|
|
function get_sub(stream_id) {
|
2021-12-18 18:47:21 +01:00
|
|
|
const sub = sub_store.get(stream_id);
|
|
|
|
if (!sub) {
|
2021-12-19 14:10:03 +01:00
|
|
|
blueslip.error("get_sub() failed id lookup: " + stream_id);
|
2021-12-18 18:47:21 +01:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
function show_stream_subscription_request_result({
|
|
|
|
message,
|
|
|
|
add_class,
|
|
|
|
remove_class,
|
|
|
|
subscribed_users,
|
|
|
|
already_subscribed_users,
|
|
|
|
ignored_deactivated_users,
|
|
|
|
}) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $stream_subscription_req_result_elem = $(
|
2021-12-18 18:47:21 +01:00
|
|
|
".stream_subscription_request_result",
|
|
|
|
).expectOne();
|
|
|
|
const html = render_stream_subscription_request_result({
|
|
|
|
message,
|
|
|
|
subscribed_users,
|
|
|
|
already_subscribed_users,
|
|
|
|
ignored_deactivated_users,
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
ui.get_content_element($stream_subscription_req_result_elem).html(html);
|
2021-12-18 18:47:21 +01:00
|
|
|
if (add_class) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$stream_subscription_req_result_elem.addClass(add_class);
|
2021-12-18 18:47:21 +01:00
|
|
|
}
|
|
|
|
if (remove_class) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$stream_subscription_req_result_elem.removeClass(remove_class);
|
2021-12-18 18:47:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function enable_subscriber_management({sub, $parent_container}) {
|
2021-12-18 18:47:21 +01:00
|
|
|
const stream_id = sub.stream_id;
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $pill_container = $parent_container.find(".pill-container");
|
2021-12-18 18:47:21 +01:00
|
|
|
|
2021-12-20 15:34:57 +01:00
|
|
|
// current_stream_id and pill_widget are module-level variables
|
|
|
|
current_stream_id = stream_id;
|
|
|
|
|
2022-02-20 13:39:38 +01:00
|
|
|
function get_potential_subscribers() {
|
|
|
|
return peer_data.potential_subscribers(stream_id);
|
2021-12-18 18:47:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-20 13:39:38 +01:00
|
|
|
pill_widget = add_subscribers_pill.create({
|
2022-01-25 11:36:19 +01:00
|
|
|
$pill_container,
|
2022-02-20 13:39:38 +01:00
|
|
|
get_potential_subscribers,
|
|
|
|
});
|
2021-12-21 16:31:07 +01:00
|
|
|
|
|
|
|
const user_ids = peer_data.get_subscribers(stream_id);
|
2021-12-21 17:00:35 +01:00
|
|
|
|
|
|
|
// We track a single subscribers_list_widget for this module, since we
|
|
|
|
// only ever have one list of subscribers visible at a time.
|
|
|
|
subscribers_list_widget = make_list_widget({
|
2022-01-25 11:36:19 +01:00
|
|
|
$parent_container,
|
2021-12-21 17:00:35 +01:00
|
|
|
name: "stream_subscribers",
|
|
|
|
user_ids,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
function make_list_widget({$parent_container, name, user_ids}) {
|
2021-12-21 16:31:07 +01:00
|
|
|
const users = people.get_users_from_ids(user_ids);
|
|
|
|
people.sort_but_pin_current_user_on_top(users);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $list_container = $parent_container.find(".subscriber_table");
|
|
|
|
$list_container.empty();
|
2021-12-18 18:47:21 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $simplebar_container = $parent_container.find(".subscriber_list_container");
|
2021-12-18 18:47:21 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
return ListWidget.create($list_container, users, {
|
2021-12-21 17:00:35 +01:00
|
|
|
name,
|
2021-12-18 18:47:21 +01:00
|
|
|
modifier(item) {
|
|
|
|
return format_member_list_elem(item);
|
|
|
|
},
|
|
|
|
filter: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $parent_container.find(".search"),
|
2021-12-18 18:47:21 +01:00
|
|
|
predicate(person, value) {
|
|
|
|
const matcher = people.build_person_matcher(value);
|
|
|
|
const match = matcher(person);
|
|
|
|
|
|
|
|
return match;
|
|
|
|
},
|
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container,
|
2021-12-18 18:47:21 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-20 15:20:34 +01:00
|
|
|
function subscribe_new_users({pill_user_ids}) {
|
|
|
|
const sub = get_sub(current_stream_id);
|
2021-12-18 18:47:21 +01:00
|
|
|
if (!sub) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const deactivated_users = new Set();
|
2022-02-08 17:46:14 +01:00
|
|
|
const active_user_ids = pill_user_ids.filter((user_id) => {
|
2021-12-18 18:47:21 +01:00
|
|
|
if (!people.is_person_active(user_id)) {
|
|
|
|
deactivated_users.add(user_id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2022-02-08 17:46:14 +01:00
|
|
|
const user_id_set = new Set(active_user_ids);
|
2021-12-18 18:47:21 +01:00
|
|
|
|
2022-02-08 17:46:14 +01:00
|
|
|
if (user_id_set.has(page_params.user_id) && sub.subscribed) {
|
2021-12-18 18:47:21 +01:00
|
|
|
// We don't want to send a request to subscribe ourselves
|
|
|
|
// if we are already subscribed to this stream. This
|
|
|
|
// case occurs when creating user pills from a stream.
|
2022-02-08 17:46:14 +01:00
|
|
|
user_id_set.delete(page_params.user_id);
|
2021-12-18 18:47:21 +01:00
|
|
|
}
|
|
|
|
let ignored_deactivated_users;
|
|
|
|
if (deactivated_users.size > 0) {
|
|
|
|
ignored_deactivated_users = Array.from(deactivated_users);
|
|
|
|
ignored_deactivated_users = ignored_deactivated_users.map((user_id) =>
|
|
|
|
people.get_by_user_id(user_id),
|
|
|
|
);
|
|
|
|
}
|
2022-02-08 17:46:14 +01:00
|
|
|
if (user_id_set.size === 0) {
|
2021-12-18 18:47:21 +01:00
|
|
|
show_stream_subscription_request_result({
|
|
|
|
message: $t({defaultMessage: "No user to subscribe."}),
|
|
|
|
add_class: "text-error",
|
|
|
|
remove_class: "text-success",
|
|
|
|
ignored_deactivated_users,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2022-02-08 17:46:14 +01:00
|
|
|
|
|
|
|
const user_ids = Array.from(user_id_set);
|
2021-12-18 18:47:21 +01:00
|
|
|
|
|
|
|
function invite_success(data) {
|
|
|
|
pill_widget.clear();
|
|
|
|
const subscribed_users = Object.keys(data.subscribed).map((email) =>
|
|
|
|
people.get_by_email(email),
|
|
|
|
);
|
|
|
|
const already_subscribed_users = Object.keys(data.already_subscribed).map((email) =>
|
|
|
|
people.get_by_email(email),
|
|
|
|
);
|
|
|
|
|
|
|
|
show_stream_subscription_request_result({
|
|
|
|
add_class: "text-success",
|
|
|
|
remove_class: "text-error",
|
|
|
|
subscribed_users,
|
|
|
|
already_subscribed_users,
|
|
|
|
ignored_deactivated_users,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function invite_failure(xhr) {
|
|
|
|
const error = JSON.parse(xhr.responseText);
|
|
|
|
show_stream_subscription_request_result({
|
|
|
|
message: error.msg,
|
|
|
|
add_class: "text-error",
|
|
|
|
remove_class: "text-success",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-21 15:28:56 +01:00
|
|
|
subscriber_api.add_user_ids_to_stream(user_ids, sub, invite_success, invite_failure);
|
2021-12-18 18:47:21 +01:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
function remove_subscriber({stream_id, target_user_id, $list_entry}) {
|
2021-12-19 14:48:01 +01:00
|
|
|
const sub = get_sub(stream_id);
|
|
|
|
if (!sub) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function removal_success(data) {
|
2021-12-19 14:50:18 +01:00
|
|
|
let message;
|
|
|
|
|
2021-12-20 15:34:57 +01:00
|
|
|
if (stream_id !== current_stream_id) {
|
|
|
|
blueslip.info("Response for subscription removal came too late.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-19 14:48:01 +01:00
|
|
|
if (data.removed.length > 0) {
|
|
|
|
// Remove the user from the subscriber list.
|
2022-01-25 11:36:19 +01:00
|
|
|
$list_entry.remove();
|
2021-12-19 14:48:01 +01:00
|
|
|
message = $t({defaultMessage: "Unsubscribed successfully!"});
|
|
|
|
// The rest of the work is done via the subscription -> remove event we will get
|
|
|
|
} else {
|
|
|
|
message = $t({defaultMessage: "User is already not subscribed."});
|
|
|
|
}
|
|
|
|
show_stream_subscription_request_result({
|
|
|
|
message,
|
|
|
|
add_class: "text-success",
|
|
|
|
remove_class: "text-remove",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function removal_failure() {
|
|
|
|
show_stream_subscription_request_result({
|
|
|
|
message: $t({defaultMessage: "Error removing user from this stream."}),
|
|
|
|
add_class: "text-error",
|
|
|
|
remove_class: "text-success",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove_user_from_private_stream() {
|
2022-02-21 15:28:56 +01:00
|
|
|
subscriber_api.remove_user_id_from_stream(
|
|
|
|
target_user_id,
|
|
|
|
sub,
|
|
|
|
removal_success,
|
|
|
|
removal_failure,
|
|
|
|
);
|
2021-12-19 14:48:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sub.invite_only && people.is_my_user_id(target_user_id)) {
|
|
|
|
const html_body = render_unsubscribe_private_stream_modal();
|
|
|
|
|
|
|
|
confirm_dialog.launch({
|
|
|
|
html_heading: $t_html(
|
|
|
|
{defaultMessage: "Unsubscribe from {stream_name}"},
|
|
|
|
{stream_name: sub.name},
|
|
|
|
),
|
|
|
|
html_body,
|
|
|
|
on_click: remove_user_from_private_stream,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-21 15:28:56 +01:00
|
|
|
subscriber_api.remove_user_id_from_stream(
|
|
|
|
target_user_id,
|
|
|
|
sub,
|
|
|
|
removal_success,
|
|
|
|
removal_failure,
|
|
|
|
);
|
2021-12-19 14:48:01 +01:00
|
|
|
}
|
|
|
|
|
2021-12-20 14:31:25 +01:00
|
|
|
export function update_subscribers_list(sub) {
|
2022-03-04 11:49:47 +01:00
|
|
|
// This is for the "Subscribers" tab of the right panel.
|
2021-12-20 14:31:25 +01:00
|
|
|
// Render subscriptions only if stream settings is open
|
2021-12-20 15:34:57 +01:00
|
|
|
if (!hash_util.is_editing_stream(sub.stream_id)) {
|
|
|
|
blueslip.info("ignoring subscription for stream that is no longer being edited");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sub.stream_id !== current_stream_id) {
|
|
|
|
// This should never happen if the prior check works correctly.
|
|
|
|
blueslip.error("current_stream_id does not match sub.stream_id for some reason");
|
2021-12-20 14:31:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stream_data.can_view_subscribers(sub)) {
|
|
|
|
$(".subscriber_list_settings_container").hide();
|
|
|
|
} else {
|
2021-12-20 15:54:10 +01:00
|
|
|
// Re-render the whole list when we add new users. This is
|
|
|
|
// inefficient for the single-user case, but using the big-hammer
|
|
|
|
// approach is superior when you do things like add subscribers
|
|
|
|
// from an existing stream or a user group.
|
2021-12-20 14:31:25 +01:00
|
|
|
const subscriber_ids = peer_data.get_subscribers(sub.stream_id);
|
2021-12-20 23:42:29 +01:00
|
|
|
update_subscribers_list_widget(subscriber_ids);
|
2021-12-20 14:31:25 +01:00
|
|
|
$(".subscriber_list_settings_container").show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 23:42:29 +01:00
|
|
|
function update_subscribers_list_widget(subscriber_ids) {
|
|
|
|
// This re-renders the subscribers_list_widget with a new
|
|
|
|
// list of subscriber_ids.
|
|
|
|
const users = people.get_users_from_ids(subscriber_ids);
|
|
|
|
people.sort_but_pin_current_user_on_top(users);
|
|
|
|
subscribers_list_widget.replace_list_data(users);
|
|
|
|
}
|
|
|
|
|
2021-12-18 18:47:21 +01:00
|
|
|
export function initialize() {
|
2022-02-20 15:20:34 +01:00
|
|
|
add_subscribers_pill.set_up_handlers({
|
|
|
|
get_pill_widget: () => pill_widget,
|
2022-01-25 11:36:19 +01:00
|
|
|
$parent_container: $("#manage_streams_container"),
|
2022-02-20 15:20:34 +01:00
|
|
|
pill_selector: ".edit_subscribers_for_stream .pill-container",
|
|
|
|
button_selector: ".edit_subscribers_for_stream .add-subscriber-button",
|
|
|
|
action: subscribe_new_users,
|
|
|
|
});
|
2021-12-18 18:47:21 +01:00
|
|
|
|
2021-12-21 19:41:13 +01:00
|
|
|
$("#manage_streams_container").on(
|
2021-12-21 17:39:05 +01:00
|
|
|
"submit",
|
|
|
|
".edit_subscribers_for_stream .subscriber_list_remove form",
|
|
|
|
(e) => {
|
|
|
|
e.preventDefault();
|
2021-12-18 18:47:21 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $list_entry = $(e.target).closest("tr");
|
|
|
|
const target_user_id = Number.parseInt($list_entry.attr("data-subscriber-id"), 10);
|
2021-12-21 17:39:05 +01:00
|
|
|
const stream_id = current_stream_id;
|
2021-12-18 18:47:21 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
remove_subscriber({stream_id, target_user_id, $list_entry});
|
2021-12-21 17:39:05 +01:00
|
|
|
},
|
|
|
|
);
|
2021-12-18 18:47:21 +01:00
|
|
|
}
|