2019-11-02 00:06:25 +01:00
|
|
|
const render_sidebar_private_message_list = require('../templates/sidebar_private_message_list.hbs');
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let private_messages_open = false;
|
2016-11-11 12:33:51 +01:00
|
|
|
|
|
|
|
// This module manages the "Private Messages" section in the upper
|
|
|
|
// left corner of the app. This was split out from stream_list.js.
|
|
|
|
|
|
|
|
function get_filter_li() {
|
2019-01-30 18:24:56 +01:00
|
|
|
return $(".top_left_private_messages");
|
2016-11-11 12:33:51 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 17:14:59 +01:00
|
|
|
function update_count_in_dom(count_span, value_span, count) {
|
|
|
|
if (count === 0) {
|
|
|
|
count_span.hide();
|
|
|
|
value_span.text('');
|
|
|
|
} else {
|
|
|
|
count_span.show();
|
|
|
|
value_span.text(count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 16:33:20 +02:00
|
|
|
function set_count(count) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const count_span = get_filter_li().find('.count');
|
|
|
|
const value_span = count_span.find('.value');
|
2016-11-14 17:14:59 +01:00
|
|
|
update_count_in_dom(count_span, value_span, count);
|
|
|
|
}
|
|
|
|
|
2016-11-18 15:48:53 +01:00
|
|
|
exports.get_li_for_user_ids_string = function (user_ids_string) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const pm_li = get_filter_li();
|
|
|
|
const convo_li = pm_li.find("li[data-user-ids-string='" + user_ids_string + "']");
|
2016-11-15 16:48:09 +01:00
|
|
|
return convo_li;
|
2016-11-11 12:33:51 +01:00
|
|
|
};
|
|
|
|
|
2017-08-01 19:22:40 +02:00
|
|
|
function set_pm_conversation_count(user_ids_string, count) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const pm_li = exports.get_li_for_user_ids_string(user_ids_string);
|
|
|
|
const count_span = pm_li.find('.private_message_count');
|
|
|
|
const value_span = count_span.find('.value');
|
2016-11-14 17:14:59 +01:00
|
|
|
|
|
|
|
if (count_span.length === 0 || value_span.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
count_span.removeClass("zero_count");
|
|
|
|
update_count_in_dom(count_span, value_span, count);
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:33:51 +01:00
|
|
|
function remove_expanded_private_messages() {
|
2017-03-05 17:28:40 +01:00
|
|
|
stream_popover.hide_topic_popover();
|
2019-05-03 03:16:09 +02:00
|
|
|
ui.get_content_element($("#private-container")).empty();
|
2016-11-11 12:33:51 +01:00
|
|
|
resize.resize_stream_filters_container();
|
|
|
|
}
|
|
|
|
|
2016-12-17 01:14:07 +01:00
|
|
|
exports.close = function () {
|
2016-11-11 12:33:51 +01:00
|
|
|
private_messages_open = false;
|
|
|
|
remove_expanded_private_messages();
|
|
|
|
};
|
|
|
|
|
2020-01-06 15:00:11 +01:00
|
|
|
exports.get_active_user_ids_string = function () {
|
|
|
|
const filter = narrow_state.filter();
|
|
|
|
|
|
|
|
if (!filter) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const emails = filter.operands('pm-with')[0];
|
|
|
|
|
|
|
|
if (!emails) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return people.emails_strings_to_user_ids_string(emails);
|
|
|
|
};
|
|
|
|
|
2020-01-06 13:07:00 +01:00
|
|
|
exports._build_private_messages_list = function () {
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const private_messages = pm_conversations.recent.get();
|
|
|
|
const display_messages = [];
|
2020-01-06 15:00:11 +01:00
|
|
|
const active_user_ids_string = exports.get_active_user_ids_string();
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2019-04-02 19:48:47 +02:00
|
|
|
_.each(private_messages, function (private_message_obj) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids_string = private_message_obj.user_ids_string;
|
|
|
|
const reply_to = people.user_ids_string_to_emails_string(user_ids_string);
|
|
|
|
const recipients_string = people.get_recipients(user_ids_string);
|
2016-11-18 15:48:53 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const num_unread = unread.num_unread_for_person(user_ids_string);
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const is_group = user_ids_string.indexOf(',') >= 0;
|
2019-02-18 16:32:27 +01:00
|
|
|
|
2020-01-06 15:00:11 +01:00
|
|
|
const is_active = user_ids_string === active_user_ids_string;
|
|
|
|
|
2019-12-31 14:02:30 +01:00
|
|
|
let user_circle_class;
|
2019-11-02 00:06:25 +01:00
|
|
|
let fraction_present;
|
2019-12-31 14:02:30 +01:00
|
|
|
|
2019-02-18 16:32:27 +01:00
|
|
|
if (is_group) {
|
|
|
|
user_circle_class = 'user_circle_fraction';
|
|
|
|
fraction_present = buddy_data.huddle_fraction_present(user_ids_string);
|
2019-09-16 23:57:18 +02:00
|
|
|
} else {
|
2019-12-29 15:07:05 +01:00
|
|
|
const user_id = parseInt(user_ids_string, 10);
|
2019-12-31 14:02:30 +01:00
|
|
|
user_circle_class = buddy_data.get_user_circle_class(user_id);
|
2019-12-29 15:07:05 +01:00
|
|
|
const recipient_user_obj = people.get_person_from_user_id(user_id);
|
2019-09-16 23:57:18 +02:00
|
|
|
|
|
|
|
if (recipient_user_obj.is_bot) {
|
|
|
|
user_circle_class = 'user_circle_green';
|
|
|
|
}
|
2019-02-18 16:32:27 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const display_message = {
|
2016-11-11 12:33:51 +01:00
|
|
|
recipients: recipients_string,
|
2016-11-18 15:48:53 +01:00
|
|
|
user_ids_string: user_ids_string,
|
2016-11-11 12:33:51 +01:00
|
|
|
unread: num_unread,
|
|
|
|
is_zero: num_unread === 0,
|
2020-01-06 15:00:11 +01:00
|
|
|
is_active: is_active,
|
2018-08-04 16:46:17 +02:00
|
|
|
url: hash_util.pm_with_uri(reply_to),
|
2019-02-18 16:32:27 +01:00
|
|
|
user_circle_class: user_circle_class,
|
|
|
|
fraction_present: fraction_present,
|
|
|
|
is_group: is_group,
|
2016-11-11 12:33:51 +01:00
|
|
|
};
|
|
|
|
display_messages.push(display_message);
|
|
|
|
});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const recipients_dom = render_sidebar_private_message_list({
|
2019-07-09 21:24:00 +02:00
|
|
|
messages: display_messages,
|
|
|
|
});
|
2016-11-11 12:33:51 +01:00
|
|
|
return recipients_dom;
|
|
|
|
};
|
|
|
|
|
2020-01-06 15:00:11 +01:00
|
|
|
exports.rebuild_recent = function () {
|
2019-03-27 18:04:51 +01:00
|
|
|
stream_popover.hide_topic_popover();
|
|
|
|
|
2016-12-01 13:30:30 +01:00
|
|
|
if (private_messages_open) {
|
2020-01-06 13:07:00 +01:00
|
|
|
const rendered_pm_list = exports._build_private_messages_list();
|
2019-05-03 03:16:09 +02:00
|
|
|
ui.get_content_element($("#private-container")).html(rendered_pm_list);
|
2016-11-11 12:33:51 +01:00
|
|
|
}
|
2019-05-03 03:16:09 +02:00
|
|
|
|
2016-11-11 12:33:51 +01:00
|
|
|
resize.resize_stream_filters_container();
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_private_messages = function () {
|
2018-06-04 21:09:11 +02:00
|
|
|
if (!narrow_state.active()) {
|
2016-11-11 12:33:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let is_pm_filter = false;
|
|
|
|
const filter = narrow_state.filter();
|
2019-04-02 20:37:14 +02:00
|
|
|
|
|
|
|
if (filter) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const conversation = filter.operands('pm-with');
|
2019-04-02 20:37:14 +02:00
|
|
|
if (conversation.length === 0) {
|
|
|
|
is_pm_filter = _.contains(filter.operands('is'), "private");
|
|
|
|
}
|
|
|
|
// We don't support having two pm-with: operands in a search
|
|
|
|
// (Group PMs are represented as a single pm-with operand
|
|
|
|
// containing a list).
|
|
|
|
}
|
|
|
|
|
2020-01-06 15:00:11 +01:00
|
|
|
exports.rebuild_recent();
|
2019-04-02 20:37:14 +02:00
|
|
|
|
|
|
|
if (is_pm_filter) {
|
2019-01-30 18:24:56 +01:00
|
|
|
$(".top_left_private_messages").addClass('active-filter');
|
2016-11-11 12:33:51 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-06 15:00:11 +01:00
|
|
|
exports.expand = function () {
|
2016-11-11 12:33:51 +01:00
|
|
|
private_messages_open = true;
|
2020-01-06 15:00:11 +01:00
|
|
|
exports.rebuild_recent();
|
2016-11-11 12:33:51 +01:00
|
|
|
};
|
|
|
|
|
2016-11-14 17:14:59 +01:00
|
|
|
exports.update_dom_with_unread_counts = function (counts) {
|
2017-08-22 16:33:20 +02:00
|
|
|
set_count(counts.private_message_count);
|
2016-11-16 00:09:09 +01:00
|
|
|
counts.pm_count.each(function (count, user_ids_string) {
|
|
|
|
// TODO: just use user_ids_string in our markup
|
2017-08-01 19:22:40 +02:00
|
|
|
set_pm_conversation_count(user_ids_string, count);
|
2016-11-14 17:14:59 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
unread_ui.set_count_toggle_button($("#userlist-toggle-unreadcount"),
|
|
|
|
counts.private_message_count);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-14 19:58:51 +02:00
|
|
|
exports.initialize = function () {
|
|
|
|
};
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.pm_list = exports;
|