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
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2019-02-25 19:01:23 +01:00
|
|
|
exports.each = function (f) {
|
|
|
|
_.each(stored_messages, function (message) {
|
|
|
|
f(message);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-01-25 00:38:19 +01:00
|
|
|
exports.get_pm_emails = function (message) {
|
2017-04-17 23:28:59 +02:00
|
|
|
|
|
|
|
function email(user_id) {
|
|
|
|
var person = people.get_person_from_user_id(user_id);
|
|
|
|
if (!person) {
|
|
|
|
blueslip.error('Unknown user id ' + user_id);
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
return person.email;
|
2016-05-25 13:53:23 +02:00
|
|
|
}
|
|
|
|
|
2017-04-17 23:28:59 +02:00
|
|
|
var user_ids = people.pm_with_user_ids(message);
|
|
|
|
var emails = _.map(user_ids, email).sort();
|
2017-01-25 00:38:19 +01:00
|
|
|
|
2017-04-17 23:28:59 +02:00
|
|
|
return emails.join(', ');
|
2016-05-25 13:53:23 +02:00
|
|
|
};
|
|
|
|
|
2017-01-25 00:38:19 +01:00
|
|
|
exports.get_pm_full_names = function (message) {
|
|
|
|
|
2017-04-17 23:08:38 +02:00
|
|
|
function name(user_id) {
|
|
|
|
var person = people.get_person_from_user_id(user_id);
|
|
|
|
if (!person) {
|
|
|
|
blueslip.error('Unknown user id ' + user_id);
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
return person.full_name;
|
2017-01-25 00:38:19 +01:00
|
|
|
}
|
|
|
|
|
2017-04-17 23:08:38 +02:00
|
|
|
var user_ids = people.pm_with_user_ids(message);
|
|
|
|
var names = _.map(user_ids, name).sort();
|
2017-01-25 00:38:19 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-01 07:46:23 +02:00
|
|
|
_.each(user_ids, function (user_id) {
|
|
|
|
pm_conversations.set_partner(user_id);
|
|
|
|
});
|
|
|
|
|
2017-02-09 01:34:54 +01:00
|
|
|
var user_ids_string = user_ids.join(',');
|
2016-11-18 15:48:53 +01:00
|
|
|
|
2018-02-09 18:10:39 +01:00
|
|
|
pm_conversations.recent.insert(user_ids_string, message.timestamp);
|
2017-04-13 20:34:21 +02:00
|
|
|
};
|
2016-11-18 15:48:53 +01:00
|
|
|
|
2017-12-16 22:40:43 +01:00
|
|
|
exports.set_message_booleans = function (message) {
|
2017-12-21 16:08:16 +01:00
|
|
|
var flags = message.flags || [];
|
2017-12-16 22:40:43 +01:00
|
|
|
|
2017-08-04 14:14:09 +02:00
|
|
|
function convert_flag(flag_name) {
|
|
|
|
return flags.indexOf(flag_name) >= 0;
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:12:57 +02:00
|
|
|
message.unread = !convert_flag('read');
|
2017-08-04 14:14:09 +02:00
|
|
|
message.historical = convert_flag('historical');
|
|
|
|
message.starred = convert_flag('starred');
|
|
|
|
message.mentioned = convert_flag('mentioned') || convert_flag('wildcard_mentioned');
|
|
|
|
message.mentioned_me_directly = convert_flag('mentioned');
|
|
|
|
message.collapsed = convert_flag('collapsed');
|
|
|
|
message.alerted = convert_flag('has_alert_word');
|
2017-12-21 16:08:16 +01:00
|
|
|
|
|
|
|
// Once we have set boolean flags here, the `flags` attribute is
|
|
|
|
// just a distraction, so we delete it. (All the downstream code
|
|
|
|
// uses booleans.)
|
|
|
|
delete message.flags;
|
|
|
|
|
2017-08-04 14:14:09 +02:00
|
|
|
};
|
|
|
|
|
2017-12-16 23:25:31 +01:00
|
|
|
exports.init_booleans = function (message) {
|
|
|
|
// This initializes booleans for the local-echo path where
|
|
|
|
// we don't have flags from the server yet. (We want to
|
|
|
|
// explicitly set flags to false to be consistent with other
|
|
|
|
// codepaths.)
|
|
|
|
message.unread = false;
|
|
|
|
message.historical = false;
|
|
|
|
message.starred = false;
|
|
|
|
message.mentioned = false;
|
|
|
|
message.mentioned_me_directly = false;
|
|
|
|
message.collapsed = false;
|
|
|
|
message.alerted = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.update_booleans = function (message, flags) {
|
|
|
|
// When we get server flags for local echo or message edits,
|
|
|
|
// we are vulnerable to race conditions, so only update flags
|
|
|
|
// that are driven by message content.
|
|
|
|
function convert_flag(flag_name) {
|
|
|
|
return flags.indexOf(flag_name) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
message.mentioned = convert_flag('mentioned') || convert_flag('wildcard_mentioned');
|
|
|
|
message.mentioned_me_directly = convert_flag('mentioned');
|
|
|
|
message.alerted = convert_flag('has_alert_word');
|
|
|
|
};
|
|
|
|
|
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) {
|
2018-12-22 15:22:07 +01:00
|
|
|
// Copy the match topic and content over if they exist on
|
2014-01-31 16:27:24 +01:00
|
|
|
// the new message
|
2018-11-15 16:59:41 +01:00
|
|
|
if (util.get_match_topic(message) !== undefined) {
|
|
|
|
util.set_match_data(cached_msg, message);
|
2014-01-31 16:27:24 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2016-12-15 23:33:36 +01:00
|
|
|
people.extract_people_from_message(message);
|
2017-11-06 15:48:44 +01:00
|
|
|
people.maybe_incr_recipient_count(message);
|
2016-12-15 23:33:36 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-12-23 16:49:14 +01:00
|
|
|
// Convert topic even for PMs, as legacy code
|
|
|
|
// wants the empty field.
|
|
|
|
util.convert_message_topic(message);
|
|
|
|
|
2014-01-31 16:27:24 +01:00
|
|
|
switch (message.type) {
|
|
|
|
case 'stream':
|
|
|
|
message.is_stream = true;
|
|
|
|
message.stream = message.display_recipient;
|
|
|
|
message.reply_to = message.sender_email;
|
|
|
|
|
2017-07-26 14:05:25 +02:00
|
|
|
topic_data.add_message({
|
|
|
|
stream_id: message.stream_id,
|
2018-12-23 16:01:30 +01:00
|
|
|
topic_name: util.get_message_topic(message),
|
2017-07-26 14:05:25 +02:00
|
|
|
message_id: message.id,
|
|
|
|
});
|
|
|
|
|
2017-06-01 07:42:57 +02:00
|
|
|
recent_senders.process_message_for_senders(message);
|
2014-01-31 16:27:24 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'private':
|
|
|
|
message.is_private = true;
|
|
|
|
message.reply_to = util.normalize_recipients(
|
2018-05-07 01:38:14 +02:00
|
|
|
exports.get_pm_emails(message));
|
2017-01-25 00:38:19 +01:00
|
|
|
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-04-17 22:50:54 +02:00
|
|
|
message.to_user_ids = people.pm_reply_user_string(message);
|
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
|
|
|
|
2017-07-19 12:49:49 +02:00
|
|
|
exports.reify_message_id = function (opts) {
|
|
|
|
var old_id = opts.old_id;
|
|
|
|
var new_id = opts.new_id;
|
|
|
|
if (pointer.furthest_read === old_id) {
|
2018-08-04 08:37:23 +02:00
|
|
|
pointer.set_furthest_read(new_id);
|
2017-07-19 12:49:49 +02: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
|
|
|
|
2017-07-19 12:49:49 +02:00
|
|
|
_.each([message_list.all, home_msg_list, message_list.narrowed], function (msg_list) {
|
|
|
|
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);
|
2014-01-31 16:27:24 +01:00
|
|
|
}
|
2017-07-19 12:49:49 +02:00
|
|
|
}
|
2014-01-31 16:27:24 +01:00
|
|
|
});
|
2017-07-04 02:09:30 +02:00
|
|
|
};
|
2014-01-31 16:27:24 +01:00
|
|
|
|
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = message_store;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.message_store = message_store;
|