2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-04-17 17:15:51 +02:00
|
|
|
import _ from "lodash";
|
2021-03-11 05:43:45 +01:00
|
|
|
|
2021-03-27 13:14:34 +01:00
|
|
|
import render_confirm_mute_user from "../templates/confirm_mute_user.hbs";
|
2021-02-28 00:41:32 +01:00
|
|
|
import render_topic_muted from "../templates/topic_muted.hbs";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-04-23 17:30:00 +02:00
|
|
|
import * as activity from "./activity";
|
2021-02-28 00:41:32 +01:00
|
|
|
import * as channel from "./channel";
|
2021-03-27 13:14:34 +01:00
|
|
|
import * as confirm_dialog from "./confirm_dialog";
|
2021-02-28 00:41:32 +01:00
|
|
|
import * as feedback_widget from "./feedback_widget";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-03-30 02:21:21 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-02-28 00:41:32 +01:00
|
|
|
import * as muting from "./muting";
|
2021-02-28 01:03:46 +01:00
|
|
|
import * as overlays from "./overlays";
|
2021-03-27 13:14:34 +01:00
|
|
|
import * as people from "./people";
|
|
|
|
import * as popovers from "./popovers";
|
2021-02-28 01:27:48 +01:00
|
|
|
import * as recent_topics from "./recent_topics";
|
2021-03-27 11:39:36 +01:00
|
|
|
import * as settings_muted_topics from "./settings_muted_topics";
|
2021-04-17 07:22:25 +02:00
|
|
|
import * as settings_muted_users from "./settings_muted_users";
|
2021-02-28 00:53:59 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2021-02-28 21:31:02 +01:00
|
|
|
import * as stream_list from "./stream_list";
|
2021-02-28 01:02:37 +01:00
|
|
|
import * as stream_popover from "./stream_popover";
|
2021-02-28 00:41:32 +01:00
|
|
|
import * as unread_ui from "./unread_ui";
|
2021-02-10 16:47:06 +01:00
|
|
|
|
2021-04-17 17:15:51 +02:00
|
|
|
export function rerender_for_muted_topic(old_muted_topics) {
|
2013-09-30 22:03:10 +02:00
|
|
|
stream_list.update_streams_sidebar();
|
2021-03-27 11:37:31 +01:00
|
|
|
message_lists.current.update_topic_muting_and_rerender();
|
2021-03-30 02:21:21 +02:00
|
|
|
if (message_lists.current !== message_lists.home) {
|
2021-03-27 11:37:31 +01:00
|
|
|
message_lists.home.update_topic_muting_and_rerender();
|
2013-09-27 16:26:04 +02:00
|
|
|
}
|
2021-03-27 11:39:36 +01:00
|
|
|
if (overlays.settings_open() && settings_muted_topics.loaded) {
|
2021-04-27 12:09:08 +02:00
|
|
|
settings_muted_topics.populate_list();
|
2020-07-12 00:13:47 +02:00
|
|
|
}
|
2021-04-17 17:15:51 +02:00
|
|
|
|
|
|
|
// We only update those topics which could have been affected, because
|
|
|
|
// we want to avoid doing a complete rerender of the recent topics view,
|
|
|
|
// because that can be expensive.
|
|
|
|
const current_muted_topics = muting.get_muted_topics();
|
|
|
|
const maybe_affected_topics = _.unionWith(old_muted_topics, current_muted_topics, _.isEqual);
|
|
|
|
|
|
|
|
for (const topic_data of maybe_affected_topics) {
|
|
|
|
recent_topics.update_topic_is_muted(topic_data.stream_id, topic_data.topic);
|
|
|
|
}
|
2021-02-28 00:41:32 +01:00
|
|
|
}
|
2013-09-27 16:26:04 +02:00
|
|
|
|
2021-04-17 17:15:51 +02:00
|
|
|
export function handle_topic_updates(muted_topics) {
|
|
|
|
const old_muted_topics = muting.get_muted_topics();
|
|
|
|
muting.set_muted_topics(muted_topics);
|
|
|
|
stream_popover.hide_topic_popover();
|
|
|
|
unread_ui.update_unread_counts();
|
|
|
|
rerender_for_muted_topic(old_muted_topics);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mute_topic(stream_id, topic) {
|
|
|
|
const stream_name = stream_data.maybe_get_stream_name(stream_id);
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2021-04-17 17:15:51 +02:00
|
|
|
topic,
|
2020-07-15 01:29:15 +02:00
|
|
|
op: "add",
|
2013-09-10 20:07:24 +02:00
|
|
|
};
|
2021-04-17 17:15:51 +02:00
|
|
|
|
2017-08-29 21:40:38 +02:00
|
|
|
channel.patch({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/users/me/subscriptions/muted_topics",
|
2017-08-29 21:40:38 +02:00
|
|
|
idempotent: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
2021-04-17 17:15:51 +02:00
|
|
|
success() {
|
|
|
|
feedback_widget.show({
|
|
|
|
populate(container) {
|
|
|
|
const rendered_html = render_topic_muted();
|
|
|
|
container.html(rendered_html);
|
|
|
|
container.find(".stream").text(stream_name);
|
|
|
|
container.find(".topic").text(topic);
|
|
|
|
},
|
|
|
|
on_undo() {
|
|
|
|
unmute_topic(stream_id, topic);
|
|
|
|
},
|
|
|
|
title_text: $t({defaultMessage: "Topic muted"}),
|
|
|
|
undo_button_text: $t({defaultMessage: "Unmute"}),
|
|
|
|
});
|
|
|
|
},
|
2017-08-29 21:40:38 +02:00
|
|
|
});
|
2021-02-28 00:41:32 +01:00
|
|
|
}
|
2017-08-29 21:40:38 +02:00
|
|
|
|
2021-04-17 17:15:51 +02:00
|
|
|
export function unmute_topic(stream_id, topic) {
|
|
|
|
// Accidentally unmuting a topic isn't as much an issue as accidentally muting
|
|
|
|
// a topic, so we don't show a popup after unmuting.
|
2019-11-02 00:06:25 +01:00
|
|
|
const data = {
|
2020-07-20 22:18:43 +02:00
|
|
|
stream_id,
|
2021-04-17 17:15:51 +02:00
|
|
|
topic,
|
2020-07-15 01:29:15 +02:00
|
|
|
op: "remove",
|
2017-08-29 21:40:38 +02:00
|
|
|
};
|
2021-04-17 17:15:51 +02:00
|
|
|
|
2017-08-29 21:40:38 +02:00
|
|
|
channel.patch({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/users/me/subscriptions/muted_topics",
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
2021-04-17 17:15:51 +02:00
|
|
|
success() {
|
|
|
|
feedback_widget.dismiss();
|
2018-12-20 18:04:46 +01:00
|
|
|
},
|
|
|
|
});
|
2021-02-28 00:41:32 +01:00
|
|
|
}
|
2017-03-23 06:57:36 +01:00
|
|
|
|
2021-02-28 00:41:32 +01:00
|
|
|
export function toggle_topic_mute(message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const stream_id = message.stream_id;
|
2020-02-19 00:04:12 +01:00
|
|
|
const topic = message.topic;
|
2018-12-22 19:18:59 +01:00
|
|
|
|
|
|
|
if (muting.is_topic_muted(stream_id, topic)) {
|
2021-02-28 00:41:32 +01:00
|
|
|
unmute_topic(stream_id, topic);
|
2020-07-15 01:29:15 +02:00
|
|
|
} else if (message.type === "stream") {
|
2021-02-28 00:41:32 +01:00
|
|
|
mute_topic(stream_id, topic);
|
2017-03-24 23:25:04 +01:00
|
|
|
}
|
2021-02-28 00:41:32 +01:00
|
|
|
}
|
2021-03-27 12:42:16 +01:00
|
|
|
|
2021-03-27 13:14:34 +01:00
|
|
|
export function mute_user(user_id) {
|
|
|
|
channel.post({
|
|
|
|
url: "/json/users/me/muted_users/" + user_id,
|
|
|
|
idempotent: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function confirm_mute_user(user_id) {
|
|
|
|
function on_click() {
|
|
|
|
mute_user(user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
const modal_parent = $(".mute-user-modal-holder");
|
|
|
|
const html_body = render_confirm_mute_user({
|
|
|
|
user_name: people.get_full_name(user_id),
|
|
|
|
});
|
|
|
|
|
|
|
|
confirm_dialog.launch({
|
|
|
|
parent: modal_parent,
|
|
|
|
html_heading: $t({defaultMessage: "Mute user"}),
|
|
|
|
html_body,
|
|
|
|
html_yes_button: $t({defaultMessage: "Confirm"}),
|
|
|
|
on_click,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function unmute_user(user_id) {
|
|
|
|
channel.del({
|
|
|
|
url: "/json/users/me/muted_users/" + user_id,
|
|
|
|
idempotent: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-17 07:22:25 +02:00
|
|
|
export function rerender_for_muted_user() {
|
|
|
|
if (overlays.settings_open() && settings_muted_users.loaded) {
|
|
|
|
settings_muted_users.populate_list();
|
|
|
|
}
|
2021-04-23 17:30:00 +02:00
|
|
|
|
|
|
|
activity.redraw();
|
2021-04-17 07:22:25 +02:00
|
|
|
}
|
|
|
|
|
2021-03-27 12:42:16 +01:00
|
|
|
export function handle_user_updates(muted_user_ids) {
|
2021-03-27 13:14:34 +01:00
|
|
|
popovers.hide_all();
|
2021-03-27 12:42:16 +01:00
|
|
|
muting.set_muted_users(muted_user_ids);
|
2021-04-17 07:22:25 +02:00
|
|
|
rerender_for_muted_user();
|
2021-03-27 12:42:16 +01:00
|
|
|
}
|