2019-07-09 21:24:00 +02:00
|
|
|
var render_sidebar_private_message_list = require('../templates/sidebar_private_message_list.hbs');
|
|
|
|
|
2016-11-11 12:33:51 +01:00
|
|
|
var private_messages_open = false;
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
var count_span = get_filter_li().find('.count');
|
2016-11-14 17:14:59 +01:00
|
|
|
var value_span = count_span.find('.value');
|
|
|
|
update_count_in_dom(count_span, value_span, count);
|
|
|
|
}
|
|
|
|
|
2016-11-15 16:48:09 +01:00
|
|
|
exports.get_conversation_li = function (conversation) {
|
|
|
|
// conversation is something like "foo@example.com,bar@example.com"
|
2018-05-30 13:05:27 +02:00
|
|
|
var user_ids_string = people.reply_to_to_user_ids_string(conversation);
|
2016-11-18 15:48:53 +01:00
|
|
|
if (!user_ids_string) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return exports.get_li_for_user_ids_string(user_ids_string);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_li_for_user_ids_string = function (user_ids_string) {
|
2016-11-11 12:33:51 +01:00
|
|
|
var pm_li = get_filter_li();
|
2016-11-18 15:48:53 +01:00
|
|
|
var 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-10-25 09:45:13 +02:00
|
|
|
var pm_li = exports.get_li_for_user_ids_string(user_ids_string);
|
2016-11-14 17:14:59 +01:00
|
|
|
var count_span = pm_li.find('.private_message_count');
|
|
|
|
var value_span = count_span.find('.value');
|
|
|
|
|
|
|
|
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();
|
|
|
|
};
|
|
|
|
|
2019-04-02 19:48:47 +02:00
|
|
|
exports._build_private_messages_list = function (active_conversation) {
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2018-02-09 18:10:39 +01:00
|
|
|
var private_messages = pm_conversations.recent.get();
|
2016-11-11 12:33:51 +01:00
|
|
|
var display_messages = [];
|
|
|
|
|
2016-11-18 15:48:53 +01:00
|
|
|
// SHIM
|
|
|
|
if (active_conversation) {
|
|
|
|
active_conversation = people.emails_strings_to_user_ids_string(active_conversation);
|
|
|
|
}
|
|
|
|
|
2019-04-02 19:48:47 +02:00
|
|
|
_.each(private_messages, function (private_message_obj) {
|
2016-11-18 15:48:53 +01:00
|
|
|
var user_ids_string = private_message_obj.user_ids_string;
|
|
|
|
var reply_to = people.user_ids_string_to_emails_string(user_ids_string);
|
2017-01-25 16:23:22 +01:00
|
|
|
var recipients_string = people.get_recipients(user_ids_string);
|
2016-11-18 15:48:53 +01:00
|
|
|
|
2016-11-18 17:02:06 +01:00
|
|
|
var num_unread = unread.num_unread_for_person(user_ids_string);
|
2016-11-11 12:33:51 +01:00
|
|
|
|
2019-02-18 16:32:27 +01:00
|
|
|
var is_group = user_ids_string.indexOf(',') >= 0;
|
|
|
|
|
|
|
|
var user_circle_class = buddy_data.get_user_circle_class(user_ids_string);
|
|
|
|
|
|
|
|
var fraction_present;
|
|
|
|
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 {
|
|
|
|
var recipient_user_obj = people.get_person_from_user_id(user_ids_string);
|
|
|
|
|
|
|
|
if (recipient_user_obj.is_bot) {
|
|
|
|
user_circle_class = 'user_circle_green';
|
|
|
|
}
|
2019-02-18 16:32:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-11 12:33:51 +01:00
|
|
|
var display_message = {
|
|
|
|
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,
|
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-07-09 21:24:00 +02:00
|
|
|
var recipients_dom = render_sidebar_private_message_list({
|
|
|
|
messages: display_messages,
|
|
|
|
});
|
2016-11-11 12:33:51 +01:00
|
|
|
return recipients_dom;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.rebuild_recent = function (active_conversation) {
|
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) {
|
2019-05-03 03:16:09 +02:00
|
|
|
var rendered_pm_list = exports._build_private_messages_list(
|
2019-04-02 19:48:47 +02:00
|
|
|
active_conversation);
|
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
|
|
|
if (active_conversation) {
|
2017-06-15 21:52:42 +02:00
|
|
|
var active_li = exports.get_conversation_li(active_conversation);
|
|
|
|
if (active_li) {
|
|
|
|
active_li.addClass('active-sub-filter');
|
|
|
|
}
|
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-04-02 20:37:14 +02:00
|
|
|
var is_pm_filter = false;
|
|
|
|
var pm_with = '';
|
|
|
|
var filter = narrow_state.filter();
|
|
|
|
|
|
|
|
if (filter) {
|
|
|
|
var conversation = filter.operands('pm-with');
|
|
|
|
if (conversation.length === 1) {
|
|
|
|
pm_with = conversation[0];
|
|
|
|
}
|
|
|
|
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).
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.rebuild_recent(pm_with);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.expand = function (op_pm) {
|
|
|
|
private_messages_open = true;
|
|
|
|
if (op_pm.length === 1) {
|
|
|
|
exports.rebuild_recent(op_pm[0]);
|
|
|
|
} else if (op_pm.length !== 0) {
|
|
|
|
// TODO: Should pass the reply-to of the thread
|
|
|
|
exports.rebuild_recent("");
|
|
|
|
} else {
|
|
|
|
exports.rebuild_recent("");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|