2023-02-22 23:03:47 +01:00
|
|
|
import * as internal_url from "../shared/src/internal_url";
|
2022-02-25 19:34:01 +01:00
|
|
|
|
2024-02-01 10:26:22 +01:00
|
|
|
import * as blueslip from "./blueslip";
|
2023-12-09 23:33:44 +01:00
|
|
|
import type {Message} from "./message_store";
|
2024-02-01 10:26:22 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 01:07:13 +01:00
|
|
|
import * as people from "./people";
|
2024-02-01 10:26:22 +01:00
|
|
|
import * as settings_data from "./settings_data";
|
2024-04-05 19:08:01 +02:00
|
|
|
import type {NarrowTerm} from "./state_data";
|
2021-02-28 01:07:13 +01:00
|
|
|
import * as stream_data from "./stream_data";
|
2023-05-18 15:53:21 +02:00
|
|
|
import * as sub_store from "./sub_store";
|
2023-08-15 04:19:07 +02:00
|
|
|
import type {StreamSubscription} from "./sub_store";
|
2024-02-09 14:34:09 +01:00
|
|
|
import * as user_groups from "./user_groups";
|
2023-08-15 04:19:07 +02:00
|
|
|
import type {UserGroup} from "./user_groups";
|
2024-04-05 19:41:15 +02:00
|
|
|
import * as util from "./util";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2023-08-15 04:19:07 +02:00
|
|
|
export function build_reload_url(): string {
|
2021-03-04 13:36:30 +01:00
|
|
|
let hash = window.location.hash;
|
2023-12-22 02:03:12 +01:00
|
|
|
if (hash.length !== 0 && hash.startsWith("#")) {
|
2021-03-04 13:36:30 +01:00
|
|
|
hash = hash.slice(1);
|
|
|
|
}
|
|
|
|
return "+oldhash=" + encodeURIComponent(hash);
|
|
|
|
}
|
|
|
|
|
2023-08-15 04:19:07 +02:00
|
|
|
export function encode_operand(operator: string, operand: string): string {
|
2023-04-11 21:04:33 +02:00
|
|
|
if (
|
|
|
|
operator === "group-pm-with" ||
|
2023-04-19 17:35:32 +02:00
|
|
|
operator === "dm-including" ||
|
2023-04-11 21:04:33 +02:00
|
|
|
operator === "dm" ||
|
|
|
|
operator === "sender" ||
|
|
|
|
operator === "pm-with"
|
|
|
|
) {
|
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") {
|
2021-02-28 01:07:13 +01:00
|
|
|
return encode_stream_name(operand);
|
2018-02-15 21:02:47 +01:00
|
|
|
}
|
|
|
|
|
2022-02-25 19:34:01 +01:00
|
|
|
return internal_url.encodeHashComponent(operand);
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2018-02-15 21:02:47 +01:00
|
|
|
|
2023-08-15 04:19:07 +02:00
|
|
|
export function encode_stream_name(operand: string): string {
|
2018-02-15 21:02:47 +01:00
|
|
|
// stream_data prefixes the stream id, but it does not do the
|
|
|
|
// URI encoding piece
|
|
|
|
operand = stream_data.name_to_slug(operand);
|
|
|
|
|
2022-02-25 19:34:01 +01:00
|
|
|
return internal_url.encodeHashComponent(operand);
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2017-03-19 00:43:14 +01:00
|
|
|
|
2023-08-15 04:19:07 +02:00
|
|
|
export function decode_operand(operator: string, operand: string): string {
|
2023-04-11 21:04:33 +02:00
|
|
|
if (
|
|
|
|
operator === "group-pm-with" ||
|
2023-04-19 17:35:32 +02:00
|
|
|
operator === "dm-including" ||
|
2023-04-11 21:04:33 +02:00
|
|
|
operator === "dm" ||
|
|
|
|
operator === "sender" ||
|
|
|
|
operator === "pm-with"
|
|
|
|
) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 19:34:01 +01:00
|
|
|
operand = internal_url.decodeHashComponent(operand);
|
2018-02-15 21:02:47 +01:00
|
|
|
|
2024-04-24 21:00:16 +02:00
|
|
|
if (util.canonicalize_stream_synonyms(operator) === "stream") {
|
2018-02-15 21:02:47 +01:00
|
|
|
return stream_data.slug_to_name(operand);
|
|
|
|
}
|
|
|
|
|
|
|
|
return operand;
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2017-03-19 00:43:14 +01:00
|
|
|
|
2023-08-15 04:19:07 +02:00
|
|
|
export function by_stream_url(stream_id: number): string {
|
2022-03-01 19:14:26 +01:00
|
|
|
// Wrapper for web use of internal_url.by_stream_url
|
2023-05-18 15:53:21 +02:00
|
|
|
return internal_url.by_stream_url(stream_id, sub_store.maybe_get_stream_name);
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2018-08-04 16:19:38 +02:00
|
|
|
|
2023-08-15 04:19:07 +02:00
|
|
|
export function by_stream_topic_url(stream_id: number, topic: string): string {
|
2022-03-01 19:14:26 +01:00
|
|
|
// Wrapper for web use of internal_url.by_stream_topic_url
|
2023-05-18 15:53:21 +02:00
|
|
|
return internal_url.by_stream_topic_url(stream_id, topic, sub_store.maybe_get_stream_name);
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2018-08-04 16:19:38 +02:00
|
|
|
|
2023-12-22 00:26:14 +01:00
|
|
|
// Encodes a term list into the
|
2018-08-04 16:33:28 +02:00
|
|
|
// corresponding hash: the # component
|
|
|
|
// of the narrow URL
|
2024-04-05 19:08:01 +02:00
|
|
|
export function search_terms_to_hash(terms?: NarrowTerm[]): string {
|
2024-04-27 08:21:32 +02:00
|
|
|
// Note: This does not return the correct hash for combined feed, recent and inbox view.
|
|
|
|
// These views can have multiple hashes that lead to them, so this function cannot support them.
|
2020-07-15 01:29:15 +02:00
|
|
|
let hash = "#";
|
2018-08-04 16:33:28 +02:00
|
|
|
|
2023-12-22 00:26:14 +01:00
|
|
|
if (terms !== 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
|
|
|
|
2023-12-22 00:26:14 +01:00
|
|
|
for (const term of terms) {
|
2018-08-04 16:33:28 +02:00
|
|
|
// Support legacy tuples.
|
2024-04-12 13:45:46 +02:00
|
|
|
const operator = util.canonicalize_stream_synonyms(term.operator);
|
2023-12-22 00:26:14 +01:00
|
|
|
const operand = term.operand;
|
2018-08-04 16:33:28 +02:00
|
|
|
|
2023-12-22 00:26:14 +01:00
|
|
|
const sign = term.negated ? "-" : "";
|
2020-07-15 00:34:28 +02:00
|
|
|
hash +=
|
|
|
|
"/" +
|
|
|
|
sign +
|
2022-02-25 19:34:01 +01:00
|
|
|
internal_url.encodeHashComponent(operator) +
|
2020-07-15 00:34:28 +02:00
|
|
|
"/" +
|
2021-02-28 01:07:13 +01:00
|
|
|
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;
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2018-08-04 16:33:28 +02:00
|
|
|
|
2023-08-15 04:19:07 +02:00
|
|
|
export function by_sender_url(reply_to: string): string {
|
2023-12-22 00:26:14 +01:00
|
|
|
return search_terms_to_hash([{operator: "sender", operand: reply_to}]);
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2018-08-04 17:19:03 +02:00
|
|
|
|
2023-08-15 04:19:07 +02:00
|
|
|
export function pm_with_url(reply_to: string): string {
|
2020-01-12 23:18:35 +01:00
|
|
|
const slug = people.emails_to_slug(reply_to);
|
2023-04-11 21:04:33 +02:00
|
|
|
return "#narrow/dm/" + slug;
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2018-08-04 16:19:38 +02:00
|
|
|
|
2023-08-15 04:19:07 +02:00
|
|
|
export function huddle_with_url(user_ids_string: string): string {
|
2018-08-04 16:52:37 +02:00
|
|
|
// 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.
|
2023-04-11 21:04:33 +02:00
|
|
|
return "#narrow/dm/" + user_ids_string + "-group";
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2018-08-04 16:52:37 +02:00
|
|
|
|
2023-08-15 04:19:07 +02:00
|
|
|
export function by_conversation_and_time_url(message: Message): string {
|
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
|
|
|
|
2023-08-15 04:19:07 +02:00
|
|
|
const suffix = "/near/" + internal_url.encodeHashComponent(message.id.toString());
|
2018-08-05 00:40:37 +02:00
|
|
|
|
2018-08-04 17:25:50 +02:00
|
|
|
if (message.type === "stream") {
|
2023-09-22 03:35:52 +02:00
|
|
|
return absolute_url + by_stream_topic_url(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;
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2018-08-04 17:25:50 +02:00
|
|
|
|
2024-01-30 15:26:56 +01:00
|
|
|
export function group_edit_url(group: UserGroup, right_side_tab: string): string {
|
|
|
|
const hash = `#groups/${group.id}/${internal_url.encodeHashComponent(group.name)}/${right_side_tab}`;
|
2022-08-21 17:18:00 +02:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2024-04-05 19:08:01 +02:00
|
|
|
export function search_public_streams_notice_url(terms: NarrowTerm[]): string {
|
2024-04-23 21:59:36 +02:00
|
|
|
const public_operator = {operator: "channels", operand: "public"};
|
2023-12-22 00:26:14 +01:00
|
|
|
return search_terms_to_hash([public_operator, ...terms]);
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2020-06-20 05:48:18 +02:00
|
|
|
|
2024-04-27 08:22:01 +02:00
|
|
|
export function parse_narrow(hash: string[]): NarrowTerm[] | undefined {
|
2022-02-25 19:27:25 +01:00
|
|
|
// This will throw an exception when passed an invalid hash
|
|
|
|
// at the decodeHashComponent call, handle appropriately.
|
2019-11-02 00:06:25 +01:00
|
|
|
let i;
|
2023-12-22 00:26:14 +01:00
|
|
|
const terms = [];
|
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.
|
2022-02-25 19:34:01 +01:00
|
|
|
let operator = internal_url.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;
|
2023-12-22 02:03:12 +01:00
|
|
|
if (operator.startsWith("-")) {
|
2018-12-04 23:29:43 +01:00
|
|
|
negated = true;
|
|
|
|
operator = operator.slice(1);
|
|
|
|
}
|
2020-03-20 19:56:01 +01:00
|
|
|
|
2021-02-28 01:07:13 +01:00
|
|
|
const operand = decode_operand(operator, raw_operand);
|
2023-12-22 00:26:14 +01:00
|
|
|
terms.push({negated, operator, operand});
|
2018-12-04 23:24:03 +01:00
|
|
|
}
|
2023-12-22 00:26:14 +01:00
|
|
|
return terms;
|
2021-02-28 01:07:13 +01:00
|
|
|
}
|
2024-02-01 10:26:22 +01:00
|
|
|
|
2024-04-30 14:46:11 +02:00
|
|
|
export function channels_settings_edit_url(
|
|
|
|
sub: StreamSubscription,
|
|
|
|
right_side_tab: string,
|
|
|
|
): string {
|
|
|
|
return `#channels/${sub.stream_id}/${internal_url.encodeHashComponent(
|
|
|
|
sub.name,
|
|
|
|
)}/${right_side_tab}`;
|
|
|
|
}
|
|
|
|
|
2024-04-30 13:30:24 +02:00
|
|
|
export function channels_settings_section_url(section = "subscribed"): string {
|
|
|
|
const valid_section_values = new Set(["new", "subscribed", "all"]);
|
|
|
|
if (!valid_section_values.has(section)) {
|
|
|
|
blueslip.warn("invalid section for channels settings: " + section);
|
|
|
|
return "#channels/subscribed";
|
|
|
|
}
|
|
|
|
return `#channels/${section}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validate_channels_settings_hash(hash: string): string {
|
2024-02-01 10:26:22 +01:00
|
|
|
const hash_components = hash.slice(1).split(/\//);
|
|
|
|
const section = hash_components[1];
|
|
|
|
|
|
|
|
const can_create_streams =
|
|
|
|
settings_data.user_can_create_public_streams() ||
|
|
|
|
settings_data.user_can_create_web_public_streams() ||
|
|
|
|
settings_data.user_can_create_private_streams();
|
|
|
|
if (section === "new" && !can_create_streams) {
|
2024-04-30 13:30:24 +02:00
|
|
|
return channels_settings_section_url();
|
2024-02-01 10:26:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (/\d+/.test(section)) {
|
|
|
|
const stream_id = Number.parseInt(section, 10);
|
|
|
|
const sub = sub_store.get(stream_id);
|
|
|
|
// There are a few situations where we can't display stream settings:
|
|
|
|
// 1. This is a stream that's been archived. (sub=undefined)
|
|
|
|
// 2. The stream ID is invalid. (sub=undefined)
|
|
|
|
// 3. The current user is a guest, and was unsubscribed from the stream
|
|
|
|
// stream in the current session. (In future sessions, the stream will
|
|
|
|
// not be in sub_store).
|
|
|
|
//
|
|
|
|
// In all these cases we redirect the user to 'subscribed' tab.
|
|
|
|
if (sub === undefined || (page_params.is_guest && !stream_data.is_subscribed(stream_id))) {
|
2024-04-30 13:30:24 +02:00
|
|
|
return channels_settings_section_url();
|
2024-02-01 10:26:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let right_side_tab = hash_components[3];
|
|
|
|
const valid_right_side_tab_values = new Set(["general", "personal", "subscribers"]);
|
|
|
|
if (!valid_right_side_tab_values.has(right_side_tab)) {
|
|
|
|
right_side_tab = "general";
|
|
|
|
}
|
2024-04-30 14:46:11 +02:00
|
|
|
return channels_settings_edit_url(sub, right_side_tab);
|
2024-02-01 10:26:22 +01:00
|
|
|
}
|
|
|
|
|
2024-04-30 13:30:24 +02:00
|
|
|
return channels_settings_section_url(section);
|
2024-02-01 10:26:22 +01:00
|
|
|
}
|
2024-02-09 14:34:09 +01:00
|
|
|
|
|
|
|
export function validate_group_settings_hash(hash: string): string {
|
|
|
|
const hash_components = hash.slice(1).split(/\//);
|
|
|
|
const section = hash_components[1];
|
|
|
|
|
2024-02-09 14:56:54 +01:00
|
|
|
const can_create_groups = settings_data.user_can_edit_user_groups();
|
|
|
|
if (section === "new" && !can_create_groups) {
|
|
|
|
return "#groups/your";
|
|
|
|
}
|
|
|
|
|
2024-02-09 14:34:09 +01:00
|
|
|
if (/\d+/.test(section)) {
|
|
|
|
const group_id = Number.parseInt(section, 10);
|
|
|
|
const group = user_groups.maybe_get_user_group_from_id(group_id);
|
|
|
|
if (!group) {
|
|
|
|
// Some users can type random url of the form
|
|
|
|
// /#groups/<random-group-id> we need to handle that.
|
|
|
|
return "#groups/your";
|
|
|
|
}
|
|
|
|
|
|
|
|
const group_name = hash_components[2];
|
|
|
|
let right_side_tab = hash_components[3];
|
|
|
|
const valid_right_side_tab_values = new Set(["general", "members"]);
|
|
|
|
if (group.name === group_name && valid_right_side_tab_values.has(right_side_tab)) {
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
if (!valid_right_side_tab_values.has(right_side_tab)) {
|
|
|
|
right_side_tab = "general";
|
|
|
|
}
|
|
|
|
return group_edit_url(group, right_side_tab);
|
|
|
|
}
|
|
|
|
|
|
|
|
const valid_section_values = ["new", "your", "all"];
|
|
|
|
if (!valid_section_values.includes(section)) {
|
|
|
|
blueslip.info("invalid section for groups: " + section);
|
|
|
|
return "#groups/your";
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|