2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 21:29:50 +01:00
|
|
|
import * as input_pill from "./input_pill";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 00:38:33 +01:00
|
|
|
import * as people from "./people";
|
2022-02-05 19:15:30 +01:00
|
|
|
import * as user_status from "./user_status";
|
2020-08-20 21:24:06 +02:00
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
// This will be used for pills for things like composing PMs
|
|
|
|
// or adding users to a stream/group.
|
|
|
|
|
2021-02-28 00:38:33 +01:00
|
|
|
export function create_item_from_email(email, current_items) {
|
2018-03-06 15:03:20 +01:00
|
|
|
// For normal Zulip use, we need to validate the email for our realm.
|
2019-11-02 00:06:25 +01:00
|
|
|
const user = people.get_by_email(email);
|
2018-03-06 15:03:20 +01:00
|
|
|
|
|
|
|
if (!user) {
|
2018-03-07 13:34:05 +01:00
|
|
|
if (page_params.realm_is_zephyr_mirror_realm) {
|
2020-07-02 01:39:34 +02:00
|
|
|
const existing_emails = current_items.map((item) => item.email);
|
2018-03-07 13:34:05 +01:00
|
|
|
|
js: Convert a.indexOf(…) !== -1 to a.includes(…).
Babel polyfills this for us for Internet Explorer.
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 K from "ast-types/gen/kinds";
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);
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;
recast.visit(ast, {
visitBinaryExpression(path) {
const { operator, left, right } = path.node;
if (
n.CallExpression.check(left) &&
n.MemberExpression.check(left.callee) &&
!left.callee.computed &&
n.Identifier.check(left.callee.property) &&
left.callee.property.name === "indexOf" &&
left.arguments.length === 1 &&
checkExpression(left.arguments[0]) &&
((["===", "!==", "==", "!=", ">", "<="].includes(operator) &&
n.UnaryExpression.check(right) &&
right.operator == "-" &&
n.Literal.check(right.argument) &&
right.argument.value === 1) ||
([">=", "<"].includes(operator) &&
n.Literal.check(right) &&
right.value === 0))
) {
const test = b.callExpression(
b.memberExpression(left.callee.object, b.identifier("includes")),
[left.arguments[0]]
);
path.replace(
["!==", "!=", ">", ">="].includes(operator)
? test
: b.unaryExpression("!", test)
);
changed = true;
}
this.traverse(path);
},
});
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-08 04:55:06 +01:00
|
|
|
if (existing_emails.includes(email)) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-03-07 13:34:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// For Zephyr we can't assume any emails are invalid,
|
|
|
|
// so we just create a pill where the display value
|
|
|
|
// is the email itself.
|
|
|
|
return {
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "user",
|
2018-03-07 13:34:05 +01:00
|
|
|
display_value: email,
|
2020-07-20 22:18:43 +02:00
|
|
|
email,
|
2018-03-07 13:34:05 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// The email is not allowed, so return.
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-03-06 15:03:20 +01:00
|
|
|
}
|
|
|
|
|
2020-07-02 01:39:34 +02:00
|
|
|
const existing_ids = current_items.map((item) => item.user_id);
|
2018-03-06 15:03:20 +01:00
|
|
|
|
js: Convert a.indexOf(…) !== -1 to a.includes(…).
Babel polyfills this for us for Internet Explorer.
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 K from "ast-types/gen/kinds";
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);
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;
recast.visit(ast, {
visitBinaryExpression(path) {
const { operator, left, right } = path.node;
if (
n.CallExpression.check(left) &&
n.MemberExpression.check(left.callee) &&
!left.callee.computed &&
n.Identifier.check(left.callee.property) &&
left.callee.property.name === "indexOf" &&
left.arguments.length === 1 &&
checkExpression(left.arguments[0]) &&
((["===", "!==", "==", "!=", ">", "<="].includes(operator) &&
n.UnaryExpression.check(right) &&
right.operator == "-" &&
n.Literal.check(right.argument) &&
right.argument.value === 1) ||
([">=", "<"].includes(operator) &&
n.Literal.check(right) &&
right.value === 0))
) {
const test = b.callExpression(
b.memberExpression(left.callee.object, b.identifier("includes")),
[left.arguments[0]]
);
path.replace(
["!==", "!=", ">", ">="].includes(operator)
? test
: b.unaryExpression("!", test)
);
changed = true;
}
this.traverse(path);
},
});
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-08 04:55:06 +01:00
|
|
|
if (existing_ids.includes(user.user_id)) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-03-06 15:03:20 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const avatar_url = people.small_avatar_url_for_person(user);
|
2018-06-27 20:55:56 +02:00
|
|
|
|
2022-02-05 19:15:30 +01:00
|
|
|
const status_emoji_info = user_status.get_status_emoji(user.user_id);
|
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
// We must supply display_value for the widget to work. Everything
|
|
|
|
// else is for our own use in callbacks.
|
2019-11-02 00:06:25 +01:00
|
|
|
const item = {
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "user",
|
2018-03-06 15:03:20 +01:00
|
|
|
display_value: user.full_name,
|
|
|
|
user_id: user.user_id,
|
|
|
|
email: user.email,
|
2018-06-27 20:55:56 +02:00
|
|
|
img_src: avatar_url,
|
2020-07-29 03:27:36 +02:00
|
|
|
deactivated: false,
|
2022-02-05 19:15:30 +01:00
|
|
|
status_emoji_info,
|
2018-03-06 15:03:20 +01:00
|
|
|
};
|
|
|
|
|
2020-07-29 03:27:36 +02:00
|
|
|
// We pass deactivated true for a deactivated user
|
|
|
|
if (!people.is_person_active(user.user_id)) {
|
|
|
|
item.deactivated = true;
|
|
|
|
item.display_value = user.full_name + " (deactivated)";
|
|
|
|
}
|
|
|
|
|
2018-03-06 15:03:20 +01:00
|
|
|
return item;
|
2021-02-28 00:38:33 +01:00
|
|
|
}
|
2018-03-06 15:03:20 +01:00
|
|
|
|
2021-02-28 00:38:33 +01:00
|
|
|
export function get_email_from_item(item) {
|
2018-05-06 21:43:17 +02:00
|
|
|
return item.email;
|
2021-02-28 00:38:33 +01:00
|
|
|
}
|
2018-03-06 15:03:20 +01:00
|
|
|
|
2021-02-28 00:38:33 +01:00
|
|
|
export function append_person(opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const person = opts.person;
|
|
|
|
const pill_widget = opts.pill_widget;
|
|
|
|
const avatar_url = people.small_avatar_url_for_person(person);
|
2022-02-05 19:15:30 +01:00
|
|
|
const status_emoji_info = user_status.get_status_emoji(opts.person.user_id);
|
2018-03-06 15:03:20 +01:00
|
|
|
|
2022-02-05 19:15:30 +01:00
|
|
|
const pill_data = {
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "user",
|
2018-03-06 15:03:20 +01:00
|
|
|
display_value: person.full_name,
|
|
|
|
user_id: person.user_id,
|
|
|
|
email: person.email,
|
2018-06-27 20:55:56 +02:00
|
|
|
img_src: avatar_url,
|
2022-02-05 19:15:30 +01:00
|
|
|
status_emoji_info,
|
|
|
|
};
|
|
|
|
|
|
|
|
pill_widget.appendValidatedData(pill_data);
|
2018-08-26 21:41:49 +02:00
|
|
|
pill_widget.clear_text();
|
2021-02-28 00:38:33 +01:00
|
|
|
}
|
2018-03-06 15:03:20 +01:00
|
|
|
|
2021-02-28 00:38:33 +01:00
|
|
|
export function get_user_ids(pill_widget) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const items = pill_widget.items();
|
2020-07-02 01:39:34 +02:00
|
|
|
let user_ids = items.map((item) => item.user_id);
|
2020-02-08 03:33:46 +01:00
|
|
|
user_ids = user_ids.filter(Boolean); // be defensive about undefined users
|
2018-03-06 15:03:20 +01:00
|
|
|
|
|
|
|
return user_ids;
|
2021-02-28 00:38:33 +01:00
|
|
|
}
|
2018-03-06 15:03:20 +01:00
|
|
|
|
2021-02-28 00:38:33 +01:00
|
|
|
export function has_unconverted_data(pill_widget) {
|
2018-09-18 01:40:27 +02:00
|
|
|
// This returns true if we either have text that hasn't been
|
|
|
|
// turned into pills or email-only pills (for Zephyr).
|
|
|
|
if (pill_widget.is_pending()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const items = pill_widget.items();
|
2020-07-02 01:39:34 +02:00
|
|
|
const has_unknown_items = items.some((item) => item.user_id === undefined);
|
2018-09-18 01:40:27 +02:00
|
|
|
|
|
|
|
return has_unknown_items;
|
2021-02-28 00:38:33 +01:00
|
|
|
}
|
2018-09-18 01:40:27 +02:00
|
|
|
|
2021-02-28 00:38:33 +01:00
|
|
|
export function typeahead_source(pill_widget) {
|
2020-03-21 19:53:16 +01:00
|
|
|
const persons = people.get_realm_users();
|
2021-02-28 00:38:33 +01:00
|
|
|
return filter_taken_users(persons, pill_widget);
|
|
|
|
}
|
2020-01-08 15:51:04 +01:00
|
|
|
|
2021-02-28 00:38:33 +01:00
|
|
|
export function filter_taken_users(items, pill_widget) {
|
|
|
|
const taken_user_ids = get_user_ids(pill_widget);
|
2020-07-02 01:39:34 +02:00
|
|
|
items = items.filter((item) => !taken_user_ids.includes(item.user_id));
|
2018-03-06 15:03:20 +01:00
|
|
|
return items;
|
2021-02-28 00:38:33 +01:00
|
|
|
}
|
2018-03-06 15:03:20 +01:00
|
|
|
|
2021-02-28 00:38:33 +01:00
|
|
|
export function append_user(user, pills) {
|
2018-05-09 13:42:10 +02:00
|
|
|
if (user) {
|
2021-02-28 00:38:33 +01:00
|
|
|
append_person({
|
2018-05-09 13:42:10 +02:00
|
|
|
pill_widget: pills,
|
|
|
|
person: user,
|
|
|
|
});
|
|
|
|
} else {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.warn("Undefined user in function append_user");
|
2018-05-09 13:42:10 +02:00
|
|
|
}
|
2021-02-28 00:38:33 +01:00
|
|
|
}
|
2018-05-09 13:42:10 +02:00
|
|
|
|
2022-02-05 19:15:30 +01:00
|
|
|
export function create_pills(pill_container, pill_config) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const pills = input_pill.create({
|
2018-05-09 13:42:10 +02:00
|
|
|
container: pill_container,
|
2022-02-05 19:15:30 +01:00
|
|
|
pill_config,
|
2021-02-28 00:38:33 +01:00
|
|
|
create_item_from_text: create_item_from_email,
|
|
|
|
get_text_from_item: get_email_from_item,
|
2018-05-09 13:42:10 +02:00
|
|
|
});
|
|
|
|
return pills;
|
2021-02-28 00:38:33 +01:00
|
|
|
}
|