2021-03-16 23:38:59 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2021-02-28 00:42:57 +01:00
|
|
|
import {FoldDict} from "./fold_dict";
|
2023-04-10 12:56:36 +02:00
|
|
|
import * as group_permission_settings from "./group_permission_settings";
|
2023-06-15 18:34:55 +02:00
|
|
|
import {page_params} from "./page_params";
|
2023-01-09 12:04:10 +01:00
|
|
|
import * as settings_config from "./settings_config";
|
2021-08-03 18:26:39 +02:00
|
|
|
import type {User, UserGroupUpdateEvent} from "./types";
|
2019-02-08 11:56:33 +01:00
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
type UserGroup = {
|
|
|
|
description: string;
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
members: Set<number>;
|
2021-09-16 20:54:12 +02:00
|
|
|
is_system_group: boolean;
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: Set<number>;
|
2023-06-15 18:23:25 +02:00
|
|
|
can_mention_group_id: number;
|
2021-08-03 18:26:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// The members field is a number array which we convert
|
|
|
|
// to a Set in the initialize function.
|
|
|
|
type UserGroupRaw = Omit<UserGroup, "members"> & {members: number[]};
|
|
|
|
|
2023-01-09 12:04:10 +01:00
|
|
|
type UserGroupForDropdownListWidget = {
|
|
|
|
name: string;
|
|
|
|
value: string;
|
|
|
|
};
|
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
let user_group_name_dict: FoldDict<UserGroup>;
|
|
|
|
let user_group_by_id_dict: Map<number, UserGroup>;
|
2017-11-07 08:58:46 +01:00
|
|
|
|
|
|
|
// We have an init() function so that our automated tests
|
|
|
|
// can easily clear data.
|
2021-08-03 18:26:39 +02:00
|
|
|
export function init(): void {
|
2019-12-26 15:34:17 +01:00
|
|
|
user_group_name_dict = new FoldDict();
|
2021-08-03 18:26:39 +02:00
|
|
|
user_group_by_id_dict = new Map<number, UserGroup>();
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2017-11-07 08:58:46 +01:00
|
|
|
|
|
|
|
// WE INITIALIZE DATA STRUCTURES HERE!
|
2021-02-28 00:42:57 +01:00
|
|
|
init();
|
2017-11-07 08:58:46 +01:00
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
export function add(user_group_raw: UserGroupRaw): void {
|
2020-01-16 20:12:06 +01:00
|
|
|
// Reformat the user group members structure to be a set.
|
2021-08-03 18:26:39 +02:00
|
|
|
const user_group = {
|
|
|
|
description: user_group_raw.description,
|
|
|
|
id: user_group_raw.id,
|
|
|
|
name: user_group_raw.name,
|
|
|
|
members: new Set(user_group_raw.members),
|
2021-09-16 20:54:12 +02:00
|
|
|
is_system_group: user_group_raw.is_system_group,
|
2022-05-16 17:02:44 +02:00
|
|
|
direct_subgroup_ids: new Set(user_group_raw.direct_subgroup_ids),
|
2023-06-15 18:23:25 +02:00
|
|
|
can_mention_group_id: user_group_raw.can_mention_group_id,
|
2021-08-03 18:26:39 +02:00
|
|
|
};
|
|
|
|
|
2017-11-07 08:58:46 +01:00
|
|
|
user_group_name_dict.set(user_group.name, user_group);
|
|
|
|
user_group_by_id_dict.set(user_group.id, user_group);
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2017-11-07 08:58:46 +01:00
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
export function remove(user_group: UserGroup): void {
|
2020-02-03 07:41:38 +01:00
|
|
|
user_group_name_dict.delete(user_group.name);
|
|
|
|
user_group_by_id_dict.delete(user_group.id);
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2017-12-06 20:00:23 +01:00
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
export function get_user_group_from_id(group_id: number): UserGroup {
|
2021-08-03 18:21:15 +02:00
|
|
|
const user_group = user_group_by_id_dict.get(group_id);
|
|
|
|
if (!user_group) {
|
2021-09-22 23:34:58 +02:00
|
|
|
throw new Error(`Unknown group_id in get_user_group_from_id: ${group_id}`);
|
2017-11-07 08:58:46 +01:00
|
|
|
}
|
2021-08-03 18:21:15 +02:00
|
|
|
return user_group;
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2017-11-07 08:58:46 +01:00
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
export function update(event: UserGroupUpdateEvent): void {
|
2021-02-28 00:42:57 +01:00
|
|
|
const group = get_user_group_from_id(event.group_id);
|
2018-03-14 17:52:38 +01:00
|
|
|
if (event.data.name !== undefined) {
|
|
|
|
group.name = event.data.name;
|
2020-02-03 07:41:38 +01:00
|
|
|
user_group_name_dict.delete(group.name);
|
2018-03-15 15:48:48 +01:00
|
|
|
user_group_name_dict.set(group.name, group);
|
2018-03-14 17:52:38 +01:00
|
|
|
}
|
|
|
|
if (event.data.description !== undefined) {
|
|
|
|
group.description = event.data.description;
|
2020-02-03 07:41:38 +01:00
|
|
|
user_group_name_dict.delete(group.name);
|
2018-03-15 15:48:48 +01:00
|
|
|
user_group_name_dict.set(group.name, group);
|
2018-03-14 17:52:38 +01:00
|
|
|
}
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2018-03-14 17:52:38 +01:00
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
export function get_user_group_from_name(name: string): UserGroup | undefined {
|
2017-11-07 08:58:46 +01:00
|
|
|
return user_group_name_dict.get(name);
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2017-11-07 08:58:46 +01:00
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
export function get_realm_user_groups(): UserGroup[] {
|
2023-03-02 01:58:25 +01:00
|
|
|
const user_groups = [...user_group_by_id_dict.values()].sort((a, b) => a.id - b.id);
|
2021-09-16 20:54:12 +02:00
|
|
|
return user_groups.filter((group) => !group.is_system_group);
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2017-11-07 08:58:46 +01:00
|
|
|
|
2023-06-15 18:34:55 +02:00
|
|
|
export function get_user_groups_allowed_to_mention(): UserGroup[] {
|
|
|
|
if (page_params.user_id === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const user_groups = get_realm_user_groups();
|
|
|
|
return user_groups.filter((group) => {
|
|
|
|
const can_mention_group_id = group.can_mention_group_id;
|
|
|
|
return (
|
|
|
|
page_params.user_id !== undefined &&
|
|
|
|
is_user_in_group(can_mention_group_id, page_params.user_id)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-05 10:04:57 +02:00
|
|
|
export function is_direct_member_of(user_id: number, user_group_id: number): boolean {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_group = user_group_by_id_dict.get(user_group_id);
|
2019-01-15 00:39:03 +01:00
|
|
|
if (user_group === undefined) {
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.error("Could not find user group", {user_group_id});
|
2019-01-15 00:39:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-04 17:44:12 +01:00
|
|
|
return user_group.members.has(user_id);
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2017-11-23 07:32:30 +01:00
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
export function add_members(user_group_id: number, user_ids: number[]): void {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_group = user_group_by_id_dict.get(user_group_id);
|
2021-08-03 18:26:39 +02:00
|
|
|
if (user_group === undefined) {
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.error("Could not find user group", {user_group_id});
|
2021-08-03 18:26:39 +02:00
|
|
|
return;
|
|
|
|
}
|
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 user_id of user_ids) {
|
2020-01-16 20:12:06 +01:00
|
|
|
user_group.members.add(user_id);
|
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
|
|
|
}
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2018-03-04 17:39:54 +01:00
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
export function remove_members(user_group_id: number, user_ids: number[]): void {
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_group = user_group_by_id_dict.get(user_group_id);
|
2021-08-03 18:26:39 +02:00
|
|
|
if (user_group === undefined) {
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.error("Could not find user group", {user_group_id});
|
2021-08-03 18:26:39 +02:00
|
|
|
return;
|
|
|
|
}
|
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 user_id of user_ids) {
|
2020-01-16 20:12:06 +01:00
|
|
|
user_group.members.delete(user_id);
|
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
|
|
|
}
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2018-03-04 17:39:54 +01:00
|
|
|
|
2022-05-02 16:41:51 +02:00
|
|
|
export function add_subgroups(user_group_id: number, subgroup_ids: number[]): void {
|
|
|
|
const user_group = user_group_by_id_dict.get(user_group_id);
|
|
|
|
if (user_group === undefined) {
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.error("Could not find user group", {user_group_id});
|
2022-05-02 16:41:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const subgroup_id of subgroup_ids) {
|
2022-05-16 17:02:44 +02:00
|
|
|
user_group.direct_subgroup_ids.add(subgroup_id);
|
2022-05-02 16:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function remove_subgroups(user_group_id: number, subgroup_ids: number[]): void {
|
|
|
|
const user_group = user_group_by_id_dict.get(user_group_id);
|
|
|
|
if (user_group === undefined) {
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.error("Could not find user group", {user_group_id});
|
2022-05-02 16:41:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const subgroup_id of subgroup_ids) {
|
2022-05-16 17:02:44 +02:00
|
|
|
user_group.direct_subgroup_ids.delete(subgroup_id);
|
2022-05-02 16:41:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
export function initialize(params: {realm_user_groups: UserGroupRaw[]}): void {
|
2020-02-25 12:16:26 +01:00
|
|
|
for (const user_group of params.realm_user_groups) {
|
2021-02-28 00:42:57 +01:00
|
|
|
add(user_group);
|
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
|
|
|
}
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2017-11-07 08:58:46 +01:00
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
export function is_user_group(item: User | UserGroup): item is UserGroup {
|
2020-05-27 00:50:02 +02:00
|
|
|
return item.members !== undefined;
|
2021-02-28 00:42:57 +01:00
|
|
|
}
|
2021-06-01 14:49:13 +02:00
|
|
|
|
2021-08-03 18:26:39 +02:00
|
|
|
export function get_user_groups_of_user(user_id: number): UserGroup[] {
|
2021-06-01 14:49:13 +02:00
|
|
|
const user_groups_realm = get_realm_user_groups();
|
2022-05-04 13:34:25 +02:00
|
|
|
const groups_of_user = user_groups_realm.filter((group) =>
|
2022-05-05 10:04:57 +02:00
|
|
|
is_direct_member_of(user_id, group.id),
|
2022-05-04 13:34:25 +02:00
|
|
|
);
|
2021-06-01 14:49:13 +02:00
|
|
|
return groups_of_user;
|
|
|
|
}
|
2022-05-04 13:16:50 +02:00
|
|
|
|
2022-05-17 16:17:04 +02:00
|
|
|
export function get_recursive_subgroups(target_user_group: UserGroup): Set<number> | undefined {
|
2022-05-04 13:16:50 +02:00
|
|
|
// Correctness of this algorithm relying on the ES6 Set
|
|
|
|
// implementation having the property that a `for of` loop will
|
|
|
|
// visit all items that are added to the set during the loop.
|
2022-05-16 17:02:44 +02:00
|
|
|
const subgroup_ids = new Set(target_user_group.direct_subgroup_ids);
|
2022-05-04 13:16:50 +02:00
|
|
|
for (const subgroup_id of subgroup_ids) {
|
|
|
|
const subgroup = user_group_by_id_dict.get(subgroup_id);
|
|
|
|
if (subgroup === undefined) {
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.error("Could not find subgroup", {subgroup_id});
|
2022-05-04 13:16:50 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2022-05-16 17:02:44 +02:00
|
|
|
for (const direct_subgroup_id of subgroup.direct_subgroup_ids) {
|
2022-05-04 13:16:50 +02:00
|
|
|
subgroup_ids.add(direct_subgroup_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return subgroup_ids;
|
|
|
|
}
|
2022-05-02 11:55:30 +02:00
|
|
|
|
|
|
|
export function is_user_in_group(user_group_id: number, user_id: number): boolean {
|
|
|
|
const user_group = user_group_by_id_dict.get(user_group_id);
|
|
|
|
if (user_group === undefined) {
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.error("Could not find user group", {user_group_id});
|
2022-05-02 11:55:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (is_direct_member_of(user_id, user_group_id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:17:04 +02:00
|
|
|
const subgroup_ids = get_recursive_subgroups(user_group);
|
2022-05-02 11:55:30 +02:00
|
|
|
if (subgroup_ids === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const group_id of subgroup_ids) {
|
|
|
|
if (is_direct_member_of(user_id, group_id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-01-09 12:04:10 +01:00
|
|
|
|
|
|
|
export function get_realm_user_groups_for_dropdown_list_widget(
|
2023-04-10 12:56:36 +02:00
|
|
|
setting_name: string,
|
2023-01-09 12:04:10 +01:00
|
|
|
): UserGroupForDropdownListWidget[] {
|
2023-04-10 12:56:36 +02:00
|
|
|
const group_setting_config =
|
|
|
|
group_permission_settings.get_group_permission_setting_config(setting_name);
|
|
|
|
|
|
|
|
if (group_setting_config === undefined) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const {require_system_group, allow_internet_group, allow_owners_group, allow_nobody_group} =
|
|
|
|
group_setting_config;
|
|
|
|
|
2023-02-17 08:20:12 +01:00
|
|
|
const system_user_groups = settings_config.system_user_groups_list
|
2023-01-09 12:04:10 +01:00
|
|
|
.filter((group) => {
|
2023-07-03 09:18:44 +02:00
|
|
|
if (!allow_internet_group && group.name === "role:internet") {
|
2023-04-10 12:56:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-03 09:18:44 +02:00
|
|
|
if (!allow_owners_group && group.name === "role:owners") {
|
2023-01-09 12:04:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-03 09:18:44 +02:00
|
|
|
if (!allow_nobody_group && group.name === "role:nobody") {
|
2023-01-09 12:04:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.map((group) => {
|
|
|
|
const user_group = get_user_group_from_name(group.name);
|
|
|
|
if (!user_group) {
|
|
|
|
throw new Error(`Unknown group name: ${group.name}`);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
name: group.display_name,
|
|
|
|
value: user_group.id.toString(),
|
|
|
|
};
|
|
|
|
});
|
2023-02-17 08:20:12 +01:00
|
|
|
|
|
|
|
if (require_system_group) {
|
|
|
|
return system_user_groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
const user_groups_excluding_system_groups = get_realm_user_groups().map((group) => ({
|
|
|
|
name: group.name,
|
|
|
|
value: group.id.toString(),
|
|
|
|
}));
|
|
|
|
|
2023-03-02 01:58:25 +01:00
|
|
|
return [...system_user_groups, ...user_groups_excluding_system_groups];
|
2023-01-09 12:04:10 +01:00
|
|
|
}
|