2017-03-19 00:43:14 +01:00
|
|
|
var hash_util = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
// Some browsers zealously URI-decode the contents of
|
|
|
|
// window.location.hash. So we hide our URI-encoding
|
|
|
|
// by replacing % with . (like MediaWiki).
|
|
|
|
exports.encodeHashComponent = function (str) {
|
|
|
|
return encodeURIComponent(str)
|
|
|
|
.replace(/\./g, '%2E')
|
2017-10-18 19:16:41 +02:00
|
|
|
.replace(/%/g, '.');
|
2017-03-19 00:43:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.encode_operand = function (operator, operand) {
|
2018-06-06 18:50:09 +02:00
|
|
|
if (operator === 'group-pm-with' || operator === 'pm-with' || operator === 'sender') {
|
2017-03-19 00:43:14 +01:00
|
|
|
var slug = people.emails_to_slug(operand);
|
|
|
|
if (slug) {
|
|
|
|
return slug;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 18:19:09 +02:00
|
|
|
if (operator === 'stream') {
|
2018-02-15 21:02:47 +01:00
|
|
|
return exports.encode_stream_name(operand);
|
|
|
|
}
|
|
|
|
|
|
|
|
return exports.encodeHashComponent(operand);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.encode_stream_name = function (operand) {
|
|
|
|
// stream_data prefixes the stream id, but it does not do the
|
|
|
|
// URI encoding piece
|
|
|
|
operand = stream_data.name_to_slug(operand);
|
|
|
|
|
2017-03-19 00:43:14 +01:00
|
|
|
return exports.encodeHashComponent(operand);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.decodeHashComponent = function (str) {
|
|
|
|
return decodeURIComponent(str.replace(/\./g, '%'));
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.decode_operand = function (operator, operand) {
|
2018-06-06 18:50:09 +02:00
|
|
|
if (operator === 'group-pm-with' || operator === 'pm-with' || operator === 'sender') {
|
2017-03-19 00:43:14 +01:00
|
|
|
var emails = people.slug_to_emails(operand);
|
|
|
|
if (emails) {
|
|
|
|
return emails;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:02:47 +01:00
|
|
|
operand = exports.decodeHashComponent(operand);
|
|
|
|
|
|
|
|
if (operator === 'stream') {
|
|
|
|
return stream_data.slug_to_name(operand);
|
|
|
|
}
|
|
|
|
|
|
|
|
return operand;
|
2017-03-19 00:43:14 +01:00
|
|
|
};
|
|
|
|
|
2018-08-04 16:19:38 +02:00
|
|
|
exports.by_stream_uri = function (stream) {
|
|
|
|
return "#narrow/stream/" + exports.encode_stream_name(stream);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.by_stream_subject_uri = function (stream, subject) {
|
|
|
|
return "#narrow/stream/" + exports.encode_stream_name(stream) +
|
|
|
|
"/subject/" + exports.encodeHashComponent(subject);
|
|
|
|
};
|
|
|
|
|
2018-08-04 16:33:28 +02:00
|
|
|
// Encodes an operator list into the
|
|
|
|
// corresponding hash: the # component
|
|
|
|
// of the narrow URL
|
|
|
|
exports.operators_to_hash = function (operators) {
|
|
|
|
var hash = '#';
|
|
|
|
|
|
|
|
if (operators !== undefined) {
|
|
|
|
hash = '#narrow';
|
|
|
|
_.each(operators, function (elem) {
|
|
|
|
// Support legacy tuples.
|
|
|
|
var operator = elem.operator;
|
|
|
|
var operand = elem.operand;
|
|
|
|
|
|
|
|
var sign = elem.negated ? '-' : '';
|
|
|
|
hash += '/' + sign + exports.encodeHashComponent(operator)
|
|
|
|
+ '/' + exports.encode_operand(operator, operand);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
};
|
|
|
|
|
2018-08-04 17:19:03 +02:00
|
|
|
exports.by_sender_uri = function (reply_to) {
|
|
|
|
return exports.operators_to_hash([
|
|
|
|
{operator: 'sender', operand: reply_to},
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
2018-08-04 16:46:17 +02:00
|
|
|
exports.pm_with_uri = function (reply_to) {
|
|
|
|
return exports.operators_to_hash([
|
|
|
|
{operator: 'pm-with', operand: reply_to},
|
|
|
|
]);
|
|
|
|
};
|
2018-08-04 16:19:38 +02:00
|
|
|
|
2018-08-04 16:52:37 +02:00
|
|
|
exports.huddle_with_uri = function (user_ids_string) {
|
|
|
|
// This method is convenient for callers
|
|
|
|
// that have already converted emails to a comma-delimited
|
|
|
|
// list of user_ids. We should be careful to keep this
|
|
|
|
// consistent with hash_util.decode_operand.
|
|
|
|
return "#narrow/pm-with/" + user_ids_string + '-group';
|
|
|
|
};
|
|
|
|
|
2018-08-04 17:25:50 +02:00
|
|
|
exports.by_conversation_and_time_uri = function (message, is_absolute_url) {
|
|
|
|
var absolute_url = "";
|
|
|
|
if (is_absolute_url) {
|
|
|
|
absolute_url = window.location.protocol + "//" +
|
|
|
|
window.location.host + "/" + window.location.pathname.split('/')[1];
|
|
|
|
}
|
|
|
|
if (message.type === "stream") {
|
|
|
|
return absolute_url + "#narrow/stream/" +
|
|
|
|
exports.encode_stream_name(message.stream) +
|
|
|
|
"/subject/" + exports.encodeHashComponent(message.subject) +
|
|
|
|
"/near/" + exports.encodeHashComponent(message.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include your own email in this URI if it's not there already
|
|
|
|
var all_emails = message.reply_to;
|
|
|
|
if (all_emails.indexOf(people.my_current_email()) === -1) {
|
|
|
|
all_emails += "," + people.my_current_email();
|
|
|
|
}
|
|
|
|
return absolute_url + "#narrow/pm-with/" +
|
|
|
|
exports.encodeHashComponent(all_emails) +
|
|
|
|
"/near/" + exports.encodeHashComponent(message.id);
|
|
|
|
};
|
|
|
|
|
2017-03-19 00:43:14 +01:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = hash_util;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.hash_util = hash_util;
|