2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = require("./people");
|
|
|
|
|
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) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return "";
|
2018-12-05 20:33:52 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const parts = hash.replace(/\/$/, "").split(/\//);
|
2018-12-05 20:33:52 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
return parts[1] || "";
|
2018-12-05 20:33:52 +01:00
|
|
|
};
|
|
|
|
|
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) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return encodeURIComponent(str).replace(/\./g, "%2E").replace(/%/g, ".");
|
2017-03-19 00:43:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.encode_operand = function (operator, operand) {
|
2020-07-15 01:29:15 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +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) {
|
2020-07-10 16:46:59 +02:00
|
|
|
try {
|
2020-10-23 02:43:28 +02:00
|
|
|
// This fails for URLs containing
|
2020-07-10 16:46:59 +02:00
|
|
|
// foo.foo or foo%foo due to our fault in special handling
|
|
|
|
// of such characters when encoding. This can also,
|
|
|
|
// fail independent of our fault, so just tell the user
|
2020-10-23 02:43:28 +02:00
|
|
|
// that the URL is invalid.
|
|
|
|
// TODO: Show possible valid URLs to the user.
|
2020-07-15 01:29:15 +02:00
|
|
|
return decodeURIComponent(str.replace(/\./g, "%"));
|
2020-10-07 10:18:48 +02:00
|
|
|
} catch {
|
2020-07-10 16:46:59 +02:00
|
|
|
ui_report.error(i18n.t("Invalid URL"), undefined, $("#home-error"), 2000);
|
|
|
|
return "";
|
|
|
|
}
|
2017-03-19 00:43:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.decode_operand = function (operator, operand) {
|
2020-07-15 01:29:15 +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);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (operator === "stream") {
|
2018-02-15 21:02:47 +01:00
|
|
|
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) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
|
|
|
"#narrow/stream/" +
|
|
|
|
exports.encode_stream_id(stream_id) +
|
|
|
|
"/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) {
|
2020-07-15 01:29:15 +02:00
|
|
|
let hash = "#";
|
2018-08-04 16:33:28 +02:00
|
|
|
|
|
|
|
if (operators !== undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
hash = "#narrow";
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
|
|
|
|
for (const elem of operators) {
|
2018-08-04 16:33:28 +02:00
|
|
|
// 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
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const sign = elem.negated ? "-" : "";
|
2020-07-15 00:34:28 +02:00
|
|
|
hash +=
|
|
|
|
"/" +
|
|
|
|
sign +
|
|
|
|
exports.encodeHashComponent(operator) +
|
|
|
|
"/" +
|
|
|
|
exports.encode_operand(operator, operand);
|
js: Automatically convert _.each to for…of.
This commit was automatically generated by the following script,
followed by lint --fix and a few small manual lint-related cleanups.
import * as babelParser from "recast/parsers/babel";
import * as recast from "recast";
import * as tsParser from "recast/parsers/typescript";
import { builders as b, namedTypes as n } from "ast-types";
import { Context } from "ast-types/lib/path-visitor";
import K from "ast-types/gen/kinds";
import { NodePath } from "ast-types/lib/node-path";
import assert from "assert";
import fs from "fs";
import path from "path";
import process from "process";
const checkExpression = (node: n.Node): node is K.ExpressionKind =>
n.Expression.check(node);
const checkStatement = (node: n.Node): node is K.StatementKind =>
n.Statement.check(node);
for (const file of process.argv.slice(2)) {
console.log("Parsing", file);
const ast = recast.parse(fs.readFileSync(file, { encoding: "utf8" }), {
parser: path.extname(file) === ".ts" ? tsParser : babelParser,
});
let changed = false;
let inLoop = false;
let replaceReturn = false;
const visitLoop = (...args: string[]) =>
function(this: Context, path: NodePath) {
for (const arg of args) {
this.visit(path.get(arg));
}
const old = { inLoop };
inLoop = true;
this.visit(path.get("body"));
inLoop = old.inLoop;
return false;
};
recast.visit(ast, {
visitDoWhileStatement: visitLoop("test"),
visitExpressionStatement(path) {
const { expression, comments } = path.node;
let valueOnly;
if (
n.CallExpression.check(expression) &&
n.MemberExpression.check(expression.callee) &&
!expression.callee.computed &&
n.Identifier.check(expression.callee.object) &&
expression.callee.object.name === "_" &&
n.Identifier.check(expression.callee.property) &&
["each", "forEach"].includes(expression.callee.property.name) &&
[2, 3].includes(expression.arguments.length) &&
checkExpression(expression.arguments[0]) &&
(n.FunctionExpression.check(expression.arguments[1]) ||
n.ArrowFunctionExpression.check(expression.arguments[1])) &&
[1, 2].includes(expression.arguments[1].params.length) &&
n.Identifier.check(expression.arguments[1].params[0]) &&
((valueOnly = expression.arguments[1].params[1] === undefined) ||
n.Identifier.check(expression.arguments[1].params[1])) &&
(expression.arguments[2] === undefined ||
n.ThisExpression.check(expression.arguments[2]))
) {
const old = { inLoop, replaceReturn };
inLoop = false;
replaceReturn = true;
this.visit(
path
.get("expression")
.get("arguments")
.get(1)
.get("body")
);
inLoop = old.inLoop;
replaceReturn = old.replaceReturn;
const [right, { body, params }] = expression.arguments;
const loop = b.forOfStatement(
b.variableDeclaration("let", [
b.variableDeclarator(
valueOnly ? params[0] : b.arrayPattern([params[1], params[0]])
),
]),
valueOnly
? right
: b.callExpression(
b.memberExpression(right, b.identifier("entries")),
[]
),
checkStatement(body) ? body : b.expressionStatement(body)
);
loop.comments = comments;
path.replace(loop);
changed = true;
}
this.traverse(path);
},
visitForStatement: visitLoop("init", "test", "update"),
visitForInStatement: visitLoop("left", "right"),
visitForOfStatement: visitLoop("left", "right"),
visitFunction(path) {
this.visit(path.get("params"));
const old = { replaceReturn };
replaceReturn = false;
this.visit(path.get("body"));
replaceReturn = old.replaceReturn;
return false;
},
visitReturnStatement(path) {
if (replaceReturn) {
assert(!inLoop); // could use labeled continue if this ever fires
const { argument, comments } = path.node;
if (argument === null) {
const s = b.continueStatement();
s.comments = comments;
path.replace(s);
} else {
const s = b.expressionStatement(argument);
s.comments = comments;
path.replace(s, b.continueStatement());
}
return false;
}
this.traverse(path);
},
visitWhileStatement: visitLoop("test"),
});
if (changed) {
console.log("Writing", file);
fs.writeFileSync(file, recast.print(ast).code, { encoding: "utf8" });
}
}
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-02-06 06:19:47 +01:00
|
|
|
}
|
2018-08-04 16:33:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
};
|
|
|
|
|
2018-08-04 17:19:03 +02:00
|
|
|
exports.by_sender_uri = function (reply_to) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return exports.operators_to_hash([{operator: "sender", operand: reply_to}]);
|
2018-08-04 17:19:03 +02:00
|
|
|
};
|
|
|
|
|
2018-08-04 16:46:17 +02:00
|
|
|
exports.pm_with_uri = function (reply_to) {
|
2020-01-12 23:18:35 +01:00
|
|
|
const slug = people.emails_to_slug(reply_to);
|
|
|
|
return "#narrow/pm-with/" + slug;
|
2018-08-04 16:46:17 +02:00
|
|
|
};
|
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.
|
2020-07-15 01:29:15 +02:00
|
|
|
return "#narrow/pm-with/" + user_ids_string + "-group";
|
2018-08-04 16:52:37 +02:00
|
|
|
};
|
|
|
|
|
2018-08-05 00:40:37 +02:00
|
|
|
exports.by_conversation_and_time_uri = function (message) {
|
2020-07-15 00:34:28 +02:00
|
|
|
const absolute_url =
|
|
|
|
window.location.protocol +
|
|
|
|
"//" +
|
|
|
|
window.location.host +
|
|
|
|
"/" +
|
2020-07-15 01:29:15 +02:00
|
|
|
window.location.pathname.split("/")[1];
|
2018-10-18 22:05:43 +02:00
|
|
|
|
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") {
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
|
|
|
absolute_url + exports.by_stream_topic_uri(message.stream_id, message.topic) + 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) {
|
2020-10-07 13:17:55 +02:00
|
|
|
const hash = `#streams/${sub.stream_id}/${exports.encodeHashComponent(sub.name)}`;
|
2018-12-02 19:42:34 +01:00
|
|
|
return hash;
|
|
|
|
};
|
|
|
|
|
2020-06-20 05:48:18 +02:00
|
|
|
exports.search_public_streams_notice_url = function () {
|
|
|
|
// Computes the URL of the current narrow if streams:public were added.
|
|
|
|
const operators = narrow_state.filter().operators();
|
2020-07-15 01:29:15 +02:00
|
|
|
const public_operator = {operator: "streams", operand: "public"};
|
2020-06-20 05:48:18 +02:00
|
|
|
return exports.operators_to_hash([public_operator].concat(operators));
|
|
|
|
};
|
|
|
|
|
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.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (operator === "") {
|
2018-12-04 23:29:43 +01:00
|
|
|
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) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-12-04 23:24:03 +01:00
|
|
|
}
|
2018-12-04 23:29:43 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let negated = false;
|
2020-07-15 01:29:15 +02:00
|
|
|
if (operator[0] === "-") {
|
2018-12-04 23:29:43 +01:00
|
|
|
negated = true;
|
|
|
|
operator = operator.slice(1);
|
|
|
|
}
|
2020-03-20 19:56:01 +01:00
|
|
|
|
2020-07-16 23:29:01 +02:00
|
|
|
const operand = exports.decode_operand(operator, raw_operand);
|
2020-07-20 22:18:43 +02:00
|
|
|
operators.push({negated, operator, operand});
|
2018-12-04 23:24:03 +01:00
|
|
|
}
|
|
|
|
return operators;
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.hash_util = exports;
|