Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

profile: Add unsubscribe button in user profile modal.

m-e-l-u-h-a-nc6f8b9d807d1
diff --git a/static/js/user_profile.js b/static/js/user_profile.js
index 9857a64fb1e..cd111b38db9 100644
--- a/static/js/user_profile.js
+++ b/static/js/user_profile.js
@@ -5,10 +5,12 @@ import render_user_group_list_item from "../templates/user_group_list_item.hbs";
import render_user_profile_modal from "../templates/user_profile_modal.hbs";
import render_user_stream_list_item from "../templates/user_stream_list_item.hbs";
+import * as browser_history from "./browser_history";
import * as buddy_data from "./buddy_data";
+import * as channel from "./channel";
import * as components from "./components";
import * as hash_util from "./hash_util";
-import {$t} from "./i18n";
+import {$t, $t_html} from "./i18n";
import * as ListWidget from "./list_widget";
import * as overlays from "./overlays";
import {page_params} from "./page_params";
@@ -18,6 +20,9 @@ import * as settings_account from "./settings_account";
import * as settings_data from "./settings_data";
import * as settings_profile_fields from "./settings_profile_fields";
import * as stream_data from "./stream_data";
+import * as stream_edit from "./stream_edit";
+import * as sub_store from "./sub_store";
+import * as ui_report from "./ui_report";
import * as user_groups from "./user_groups";
import * as util from "./util";
@@ -25,13 +30,14 @@ function compare_by_name(a, b) {
return util.strcmp(a.name, b.name);
}
-function format_user_stream_list_item(stream) {
+function format_user_stream_list_item(stream, show_unsubscribe_button) {
return render_user_stream_list_item({
name: stream.name,
stream_id: stream.stream_id,
stream_color: stream.color,
invite_only: stream.invite_only,
is_web_public: stream.is_web_public,
+ show_unsubscribe_button,
stream_edit_url: hash_util.stream_edit_uri(stream),
});
}
@@ -47,10 +53,12 @@ function render_user_stream_list(streams, user) {
streams.sort(compare_by_name);
const container = $("#user-profile-modal .user-stream-list");
container.empty();
+ const show_unsubscribe_button =
+ people.is_my_user_id(user.user_id) || settings_data.user_can_unsubscribe_other_users();
ListWidget.create(container, streams, {
name: `user-${user.user_id}-stream-list`,
modifier(item) {
- return format_user_stream_list_item(item);
+ return format_user_stream_list_item(item, show_unsubscribe_button);
},
simplebar_container: $("#user-profile-modal .modal-body"),
});
@@ -183,6 +191,21 @@ export function show_user_profile(user) {
);
}
+function handle_remove_stream_subscription(target_user_id, sub, success, failure) {
+ if (people.is_my_user_id(target_user_id)) {
+ // Self unsubscribe.
+ channel.del({
+ url: "/json/users/me/subscriptions",
+ data: {subscriptions: JSON.stringify([sub.name])},
+ success,
+ error: failure,
+ });
+ } else {
+ // Unsubscribed by admin.
+ stream_edit.remove_user_from_stream(target_user_id, sub, success, failure);
+ }
+}
+
export function register_click_handlers() {
$("body").on("click", ".info_popover_actions .view_full_user_profile", (e) => {
const user_id = popovers.elem_to_user_id($(e.target).parents("ul"));
@@ -192,6 +215,66 @@ export function register_click_handlers() {
e.preventDefault();
});
+ $("body").on("click", "#user-profile-modal .remove-subscription-button", (e) => {
+ e.preventDefault();
+ const stream_row = $(e.currentTarget).closest("[data-stream-id]");
+ const stream_id = Number.parseInt(stream_row.attr("data-stream-id"), 10);
+ const sub = sub_store.get(stream_id);
+ const target_user_id = Number.parseInt(
+ stream_row.closest("#user-profile-modal").attr("data-user-id"),
+ 10,
+ );
+ const alert_box = $("#streams-tab .stream_list_info");
+
+ function removal_success(data) {
+ if (data.removed.length > 0) {
+ // Most of the work for handling the unsubscribe is done
+ // by the subscription -> remove event we will get.
+ // However, the user profile component has not yet
+ // implemented live update, so we do update its
+ // UI manually here by removing the stream from this list.
+ stream_row.remove();
+
+ ui_report.success(
+ $t_html({defaultMessage: "Unsubscribed successfully!"}),
+ alert_box,
+ 1200,
+ );
+ } else {
+ ui_report.client_error(
+ $t_html({defaultMessage: "Already not subscribed."}),
+ alert_box,
+ 1200,
+ );
+ }
+ }
+
+ function removal_failure() {
+ let error_message;
+ if (people.is_my_user_id(target_user_id)) {
+ error_message = $t(
+ {defaultMessage: "Error in unsubscribing from #{stream_name}"},
+ {stream_name: sub.name},
+ );
+ } else {
+ error_message = $t(
+ {defaultMessage: "Error removing user from #{stream_name}"},
+ {stream_name: sub.name},
+ );
+ }
+
+ ui_report.client_error(error_message, alert_box, 1200);
+ }
+
+ if (sub.invite_only && people.is_my_user_id(target_user_id)) {
+ const new_hash = hash_util.stream_edit_uri(sub);
+ hide_user_profile();
+ browser_history.go_to_location(new_hash);
+ return;
+ }
+ handle_remove_stream_subscription(target_user_id, sub, removal_success, removal_failure);
+ });
+
/* These click handlers are implemented as just deep links to the
* relevant part of the Zulip UI, so we don't want preventDefault,
* but we do want to close the modal when you click them. */
diff --git a/static/styles/popovers.css b/static/styles/popovers.css
index 09d0deaa678..279be2d1af1 100644
--- a/static/styles/popovers.css
+++ b/static/styles/popovers.css
@@ -363,6 +363,12 @@ ul {
margin-bottom: 20px;
}
+ #streams-tab {
+ .stream_list_info {
+ margin-bottom: 8px;
+ }
+ }
+
.subscription-group-list,
.subscription-stream-list {
position: relative;
@@ -392,6 +398,14 @@ ul {
&:first-of-type {
padding-left: 10px;
}
+
+ &:last-of-type {
+ padding-right: 10px;
+ }
+
+ &.remove_subscription {
+ text-align: right;
+ }
}
}
diff --git a/static/templates/user_profile_modal.hbs b/static/templates/user_profile_modal.hbs
index c4b767112ca..def103a97cb 100644
--- a/static/templates/user_profile_modal.hbs
+++ b/static/templates/user_profile_modal.hbs
@@ -65,6 +65,7 @@
</div>
<div class="tabcontent" id="streams-tab">
+ <div class="alert stream_list_info"></div>
<div class="subscription-stream-list">
<table class="user-stream-list" data-empty="{{t 'No stream subscriptions.'}}"></table>
</div>
diff --git a/static/templates/user_stream_list_item.hbs b/static/templates/user_stream_list_item.hbs
index 0d094c4899d..44da635b646 100644
--- a/static/templates/user_stream_list_item.hbs
+++ b/static/templates/user_stream_list_item.hbs
@@ -5,4 +5,13 @@
</span>
<a class = "stream_list_item" href="{{stream_edit_url}}">{{name}}</a>
</td>
+ {{#if show_unsubscribe_button}}
+ <td class="remove_subscription">
+ <div class="subscription_list_remove">
+ <button type="button" name="unsubscribe" class="remove-subscription-button button small rounded btn-danger">
+ {{t 'Unsubscribe' }}
+ </button>
+ </div>
+ </td>
+ {{/if}}
</tr>