2018-12-04 23:48:07 +01:00
|
|
|
exports.get_hash_category = function (hash) {
|
|
|
|
// given "#streams/subscribed", returns "streams"
|
|
|
|
return hash ? hash.replace(/^#/, "").split(/\//)[0] : "";
|
|
|
|
};
|
|
|
|
|
2018-12-05 20:33:52 +01:00
|
|
|
exports.get_hash_section = function (hash) {
|
|
|
|
// given "#settings/your-account", returns "your-account"
|
|
|
|
// given '#streams/5/social", returns "5"
|
|
|
|
if (!hash) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const parts = hash.replace(/\/$/, "").split(/\//);
|
2018-12-05 20:33:52 +01:00
|
|
|
|
|
|
|
return parts[1] || '';
|
|
|
|
};
|
|
|
|
|
2017-03-19 00:43:14 +01:00
|
|
|
// 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') {
|
2019-11-02 00:06:25 +01:00
|
|
|
const slug = people.emails_to_slug(operand);
|
2017-03-19 00:43:14 +01:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2018-12-14 19:02:26 +01:00
|
|
|
exports.encode_stream_id = function (stream_id) {
|
|
|
|
// stream_data appends the stream name, but it does not do the
|
|
|
|
// URI encoding piece
|
2019-11-02 00:06:25 +01:00
|
|
|
const slug = stream_data.id_to_slug(stream_id);
|
2018-12-14 19:02:26 +01:00
|
|
|
|
|
|
|
return exports.encodeHashComponent(slug);
|
|
|
|
};
|
|
|
|
|
2018-02-15 21:02:47 +01:00
|
|
|
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') {
|
2019-11-02 00:06:25 +01:00
|
|
|
const emails = people.slug_to_emails(operand);
|
2017-03-19 00:43:14 +01:00
|
|
|
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-12-14 19:18:24 +01:00
|
|
|
exports.by_stream_uri = function (stream_id) {
|
|
|
|
return "#narrow/stream/" + exports.encode_stream_id(stream_id);
|
2018-08-04 16:19:38 +02:00
|
|
|
};
|
|
|
|
|
2018-12-22 23:04:23 +01:00
|
|
|
exports.by_stream_topic_uri = function (stream_id, topic) {
|
2018-12-14 19:02:26 +01:00
|
|
|
return "#narrow/stream/" + exports.encode_stream_id(stream_id) +
|
2018-12-22 23:04:23 +01:00
|
|
|
"/topic/" + exports.encodeHashComponent(topic);
|
2018-08-04 16:19:38 +02:00
|
|
|
};
|
|
|
|
|
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) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let hash = '#';
|
2018-08-04 16:33:28 +02:00
|
|
|
|
|
|
|
if (operators !== undefined) {
|
|
|
|
hash = '#narrow';
|
|
|
|
_.each(operators, function (elem) {
|
|
|
|
// Support legacy tuples.
|
2019-11-02 00:06:25 +01:00
|
|
|
const operator = elem.operator;
|
|
|
|
const operand = elem.operand;
|
2018-08-04 16:33:28 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const sign = elem.negated ? '-' : '';
|
2018-08-04 16:33:28 +02:00
|
|
|
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-05 00:40:37 +02:00
|
|
|
exports.by_conversation_and_time_uri = function (message) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const absolute_url = window.location.protocol + "//" +
|
2018-10-18 22:05:43 +02:00
|
|
|
window.location.host + "/" +
|
|
|
|
window.location.pathname.split('/')[1];
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const suffix = "/near/" + exports.encodeHashComponent(message.id);
|
2018-08-05 00:40:37 +02:00
|
|
|
|
2018-08-04 17:25:50 +02:00
|
|
|
if (message.type === "stream") {
|
2018-10-18 22:05:43 +02:00
|
|
|
return absolute_url +
|
2018-12-22 23:04:23 +01:00
|
|
|
exports.by_stream_topic_uri(message.stream_id, util.get_message_topic(message)) +
|
2018-10-18 22:05:43 +02:00
|
|
|
suffix;
|
2018-08-04 17:25:50 +02:00
|
|
|
}
|
|
|
|
|
2018-10-18 22:05:43 +02:00
|
|
|
return absolute_url + people.pm_perma_link(message) + suffix;
|
2018-08-04 17:25:50 +02:00
|
|
|
};
|
|
|
|
|
2018-12-02 19:42:34 +01:00
|
|
|
exports.stream_edit_uri = function (sub) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const hash = "#streams" + "/" + sub.stream_id + "/" + exports.encodeHashComponent(sub.name);
|
2018-12-02 19:42:34 +01:00
|
|
|
return hash;
|
|
|
|
};
|
|
|
|
|
2018-12-04 23:24:03 +01:00
|
|
|
exports.parse_narrow = function (hash) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let i;
|
|
|
|
const operators = [];
|
2018-12-04 23:24:03 +01:00
|
|
|
for (i = 1; i < hash.length; i += 2) {
|
|
|
|
// We don't construct URLs with an odd number of components,
|
|
|
|
// but the user might write one.
|
2019-11-02 00:06:25 +01:00
|
|
|
let operator = exports.decodeHashComponent(hash[i]);
|
2018-12-04 23:29:43 +01:00
|
|
|
// Do not parse further if empty operator encountered.
|
|
|
|
if (operator === '') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const raw_operand = hash[i + 1];
|
2018-12-04 23:29:43 +01:00
|
|
|
|
|
|
|
if (!raw_operand) {
|
2018-12-04 23:24:03 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-12-04 23:29:43 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const operand = exports.decode_operand(operator, raw_operand);
|
|
|
|
let negated = false;
|
2018-12-04 23:29:43 +01:00
|
|
|
if (operator[0] === '-') {
|
|
|
|
negated = true;
|
|
|
|
operator = operator.slice(1);
|
|
|
|
}
|
|
|
|
operators.push({negated: negated, operator: operator, operand: operand});
|
2018-12-04 23:24:03 +01:00
|
|
|
}
|
|
|
|
return operators;
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.hash_util = exports;
|