2014-01-31 16:27:24 +01:00
|
|
|
var message_store = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
2014-01-31 22:57:54 +01:00
|
|
|
var stored_messages = {};
|
2014-01-31 16:27:24 +01:00
|
|
|
|
2015-11-25 18:41:32 +01:00
|
|
|
exports.recent_private_messages = [];
|
2014-05-06 06:55:33 +02:00
|
|
|
|
2014-01-31 22:02:57 +01:00
|
|
|
exports.get = function get(message_id) {
|
2014-01-31 22:57:54 +01:00
|
|
|
return stored_messages[message_id];
|
2014-01-31 22:02:57 +01:00
|
|
|
};
|
|
|
|
|
2017-01-25 00:38:19 +01:00
|
|
|
exports.get_pm_emails = function (message) {
|
2016-12-02 17:09:31 +01:00
|
|
|
var recipient;
|
|
|
|
var i;
|
2016-05-25 13:53:23 +02:00
|
|
|
var other_recipients = _.filter(message.display_recipient,
|
|
|
|
function (element) {
|
2017-01-19 20:18:03 +01:00
|
|
|
return !people.is_current_user(element.email);
|
2016-05-25 13:53:23 +02:00
|
|
|
});
|
|
|
|
if (other_recipients.length === 0) {
|
|
|
|
// private message with oneself
|
2017-01-25 00:38:19 +01:00
|
|
|
return message.display_recipient[0].email;
|
2016-05-25 13:53:23 +02:00
|
|
|
}
|
|
|
|
|
2017-01-25 00:38:19 +01:00
|
|
|
recipient = other_recipients[0].email;
|
|
|
|
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i = 1; i < other_recipients.length; i += 1) {
|
2017-01-25 00:38:19 +01:00
|
|
|
var email = other_recipients[i].email;
|
|
|
|
recipient += ', ' + email;
|
2016-05-25 13:53:23 +02:00
|
|
|
}
|
|
|
|
return recipient;
|
|
|
|
};
|
|
|
|
|
2017-01-25 00:38:19 +01:00
|
|
|
exports.get_pm_full_names = function (message) {
|
|
|
|
function name(recip) {
|
|
|
|
if (recip.id) {
|
|
|
|
var person = people.get_person_from_user_id(recip.id);
|
|
|
|
if (person) {
|
|
|
|
return person.full_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return recip.full_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
var other_recipients = _.filter(message.display_recipient,
|
|
|
|
function (element) {
|
|
|
|
return !people.is_current_user(element.email);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (other_recipients.length === 0) {
|
|
|
|
// private message with oneself
|
|
|
|
return name(message.display_recipient[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var names = _.map(other_recipients, name).sort();
|
|
|
|
|
|
|
|
return names.join(', ');
|
|
|
|
};
|
|
|
|
|
2017-02-09 01:34:54 +01:00
|
|
|
exports.process_message_for_recent_private_messages = function (message) {
|
|
|
|
var user_ids = people.pm_with_user_ids(message);
|
|
|
|
if (!user_ids) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var user_ids_string = user_ids.join(',');
|
2016-11-18 15:48:53 +01:00
|
|
|
|
|
|
|
if (!user_ids_string) {
|
|
|
|
blueslip.warn('Unknown reply_to in message: ' + user_ids_string);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-13 20:34:21 +02:00
|
|
|
exports.insert_recent_private_message(user_ids_string, message.timestamp);
|
|
|
|
};
|
2016-11-18 15:48:53 +01:00
|
|
|
|
2017-04-13 20:57:06 +02:00
|
|
|
exports.insert_recent_private_message = (function () {
|
|
|
|
var recent_timestamps = new Dict({fold_case: true}); // key is user_ids_string
|
|
|
|
|
|
|
|
return function (user_ids_string, timestamp) {
|
|
|
|
var conversation = recent_timestamps.get(user_ids_string);
|
|
|
|
|
|
|
|
if (conversation === undefined) {
|
|
|
|
// This is a new user, so create a new object.
|
|
|
|
conversation = {
|
|
|
|
user_ids_string: user_ids_string,
|
|
|
|
timestamp: timestamp,
|
|
|
|
};
|
|
|
|
recent_timestamps.set(user_ids_string, conversation);
|
|
|
|
|
|
|
|
// Optimistically insert the new message at the front, since that
|
|
|
|
// is usually where it belongs, but we'll re-sort.
|
|
|
|
exports.recent_private_messages.unshift(conversation);
|
|
|
|
} else {
|
|
|
|
if (conversation.timestamp >= timestamp) {
|
|
|
|
return; // don't backdate our conversation
|
|
|
|
}
|
|
|
|
|
|
|
|
// update our timestamp
|
|
|
|
conversation.timestamp = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.recent_private_messages.sort(function (a, b) {
|
|
|
|
return b.timestamp - a.timestamp;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}());
|
2015-11-25 18:41:32 +01:00
|
|
|
|
2017-03-19 20:23:48 +01:00
|
|
|
exports.set_topic_edit_properties = function (message) {
|
2014-01-31 16:27:24 +01:00
|
|
|
message.always_visible_topic_edit = false;
|
|
|
|
message.on_hover_topic_edit = false;
|
2016-06-21 21:34:41 +02:00
|
|
|
if (!page_params.realm_allow_message_editing) {
|
2014-01-31 16:27:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Messages with no topics should always have an edit icon visible
|
|
|
|
// to encourage updating them. Admins can also edit any topic.
|
2016-08-27 04:47:53 +02:00
|
|
|
if (message.subject === compose.empty_topic_placeholder()) {
|
2014-01-31 16:27:24 +01:00
|
|
|
message.always_visible_topic_edit = true;
|
|
|
|
} else if (page_params.is_admin) {
|
|
|
|
message.on_hover_topic_edit = true;
|
|
|
|
}
|
2017-03-19 20:23:48 +01:00
|
|
|
};
|
2014-01-31 16:27:24 +01:00
|
|
|
|
2017-03-19 18:19:48 +01:00
|
|
|
exports.add_message_metadata = function (message) {
|
2014-02-03 19:31:18 +01:00
|
|
|
var cached_msg = stored_messages[message.id];
|
2014-01-31 16:27:24 +01:00
|
|
|
if (cached_msg !== undefined) {
|
|
|
|
// Copy the match subject and content over if they exist on
|
|
|
|
// the new message
|
|
|
|
if (message.match_subject !== undefined) {
|
|
|
|
cached_msg.match_subject = message.match_subject;
|
|
|
|
cached_msg.match_content = message.match_content;
|
|
|
|
}
|
|
|
|
return cached_msg;
|
|
|
|
}
|
|
|
|
|
2017-01-19 20:18:03 +01:00
|
|
|
message.sent_by_me = people.is_current_user(message.sender_email);
|
2014-01-31 16:27:24 +01:00
|
|
|
|
|
|
|
message.flags = message.flags || [];
|
|
|
|
message.historical = (message.flags !== undefined &&
|
|
|
|
message.flags.indexOf('historical') !== -1);
|
|
|
|
message.starred = message.flags.indexOf("starred") !== -1;
|
|
|
|
message.mentioned = message.flags.indexOf("mentioned") !== -1 ||
|
|
|
|
message.flags.indexOf("wildcard_mentioned") !== -1;
|
2016-08-30 10:51:50 +02:00
|
|
|
message.mentioned_me_directly = message.flags.indexOf("mentioned") !== -1;
|
2014-01-31 16:27:24 +01:00
|
|
|
message.collapsed = message.flags.indexOf("collapsed") !== -1;
|
|
|
|
message.alerted = message.flags.indexOf("has_alert_word") !== -1;
|
|
|
|
message.is_me_message = message.flags.indexOf("is_me_message") !== -1;
|
|
|
|
|
2016-12-15 23:33:36 +01:00
|
|
|
people.extract_people_from_message(message);
|
|
|
|
|
2017-01-24 23:10:01 +01:00
|
|
|
var sender = people.get_person_from_user_id(message.sender_id);
|
|
|
|
if (sender) {
|
|
|
|
message.sender_full_name = sender.full_name;
|
2017-02-04 19:12:25 +01:00
|
|
|
message.sender_email = sender.email;
|
2017-01-24 23:10:01 +01:00
|
|
|
}
|
|
|
|
|
2014-01-31 16:27:24 +01:00
|
|
|
switch (message.type) {
|
|
|
|
case 'stream':
|
|
|
|
message.is_stream = true;
|
|
|
|
message.stream = message.display_recipient;
|
|
|
|
composebox_typeahead.add_topic(message.stream, message.subject);
|
|
|
|
message.reply_to = message.sender_email;
|
|
|
|
|
2016-10-28 17:31:18 +02:00
|
|
|
stream_data.process_message_for_recent_topics(message);
|
2017-03-19 20:23:48 +01:00
|
|
|
exports.set_topic_edit_properties(message);
|
2014-01-31 16:27:24 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'private':
|
|
|
|
message.is_private = true;
|
|
|
|
message.reply_to = util.normalize_recipients(
|
2017-01-25 00:38:19 +01:00
|
|
|
exports.get_pm_emails(message));
|
|
|
|
message.display_reply_to = exports.get_pm_full_names(message);
|
2017-02-06 20:48:01 +01:00
|
|
|
message.pm_with_url = people.pm_with_url(message);
|
2017-02-25 00:19:02 +01:00
|
|
|
message.to_user_ids = people.emails_strings_to_user_ids_string(
|
|
|
|
message.reply_to);
|
2014-01-31 16:27:24 +01:00
|
|
|
|
2015-11-25 18:41:32 +01:00
|
|
|
exports.process_message_for_recent_private_messages(message);
|
2014-01-31 16:27:24 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
alert_words.process_message(message);
|
2016-12-02 13:23:23 +01:00
|
|
|
if (!message.reactions) {
|
|
|
|
message.reactions = [];
|
|
|
|
}
|
2014-01-31 22:57:54 +01:00
|
|
|
stored_messages[message.id] = message;
|
2014-01-31 16:27:24 +01:00
|
|
|
return message;
|
2017-03-19 18:19:48 +01:00
|
|
|
};
|
2014-01-31 16:27:24 +01:00
|
|
|
|
2014-02-05 19:49:18 +01:00
|
|
|
exports.clear = function clear() {
|
|
|
|
this.stored_messages = {};
|
|
|
|
};
|
|
|
|
|
2014-03-07 00:44:15 +01:00
|
|
|
util.execute_early(function () {
|
2014-01-31 16:27:24 +01:00
|
|
|
$(document).on('message_id_changed', function (event) {
|
2016-12-02 17:09:31 +01:00
|
|
|
var old_id = event.old_id;
|
|
|
|
var new_id = event.new_id;
|
2016-04-12 17:38:47 +02:00
|
|
|
if (pointer.furthest_read === old_id) {
|
|
|
|
pointer.furthest_read = new_id;
|
2014-01-31 16:27:24 +01:00
|
|
|
}
|
2014-01-31 22:57:54 +01:00
|
|
|
if (stored_messages[old_id]) {
|
|
|
|
stored_messages[new_id] = stored_messages[old_id];
|
|
|
|
delete stored_messages[old_id];
|
2014-01-31 16:27:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This handler cannot be in the MessageList constructor, which is the logical place
|
|
|
|
// If it's there, the event handler creates a closure with a reference to the message
|
|
|
|
// list itself. When narrowing, the old narrow message list is discarded and a new one
|
|
|
|
// created, but due to the closure, the old list is not garbage collected. This also leads
|
|
|
|
// to the old list receiving the change id events, and throwing errors as it does not
|
|
|
|
// have the messages that you would expect in its internal data structures.
|
2016-04-25 23:45:25 +02:00
|
|
|
_.each([message_list.all, home_msg_list, message_list.narrowed], function (msg_list) {
|
2014-01-31 16:27:24 +01:00
|
|
|
if (msg_list !== undefined) {
|
|
|
|
msg_list.change_message_id(old_id, new_id);
|
|
|
|
|
|
|
|
if (msg_list.view !== undefined) {
|
|
|
|
msg_list.view.change_message_id(old_id, new_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = message_store;
|
|
|
|
}
|