mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-26 16:06:16 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
108
src/utils.ts
108
src/utils.ts
@@ -25,7 +25,9 @@ export function assertWebChannel(input: string): asserts input is WebChannel {
|
||||
}
|
||||
|
||||
export function normalizePath(p: string): string {
|
||||
if (!p.startsWith("/")) return `/${p}`;
|
||||
if (!p.startsWith("/")) {
|
||||
return `/${p}`;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -36,7 +38,9 @@ export function withWhatsAppPrefix(number: string): string {
|
||||
export function normalizeE164(number: string): string {
|
||||
const withoutPrefix = number.replace(/^whatsapp:/, "").trim();
|
||||
const digits = withoutPrefix.replace(/[^\d+]/g, "");
|
||||
if (digits.startsWith("+")) return `+${digits.slice(1)}`;
|
||||
if (digits.startsWith("+")) {
|
||||
return `+${digits.slice(1)}`;
|
||||
}
|
||||
return `+${digits}`;
|
||||
}
|
||||
|
||||
@@ -49,11 +53,17 @@ export function isSelfChatMode(
|
||||
selfE164: string | null | undefined,
|
||||
allowFrom?: Array<string | number> | null,
|
||||
): boolean {
|
||||
if (!selfE164) return false;
|
||||
if (!Array.isArray(allowFrom) || allowFrom.length === 0) return false;
|
||||
if (!selfE164) {
|
||||
return false;
|
||||
}
|
||||
if (!Array.isArray(allowFrom) || allowFrom.length === 0) {
|
||||
return false;
|
||||
}
|
||||
const normalizedSelf = normalizeE164(selfE164);
|
||||
return allowFrom.some((n) => {
|
||||
if (n === "*") return false;
|
||||
if (n === "*") {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return normalizeE164(String(n)) === normalizedSelf;
|
||||
} catch {
|
||||
@@ -64,7 +74,9 @@ export function isSelfChatMode(
|
||||
|
||||
export function toWhatsappJid(number: string): string {
|
||||
const withoutPrefix = number.replace(/^whatsapp:/, "").trim();
|
||||
if (withoutPrefix.includes("@")) return withoutPrefix;
|
||||
if (withoutPrefix.includes("@")) {
|
||||
return withoutPrefix;
|
||||
}
|
||||
const e164 = normalizeE164(withoutPrefix);
|
||||
const digits = e164.replace(/\D/g, "");
|
||||
return `${digits}@s.whatsapp.net`;
|
||||
@@ -83,11 +95,15 @@ type LidLookup = {
|
||||
function resolveLidMappingDirs(opts?: JidToE164Options): string[] {
|
||||
const dirs = new Set<string>();
|
||||
const addDir = (dir?: string | null) => {
|
||||
if (!dir) return;
|
||||
if (!dir) {
|
||||
return;
|
||||
}
|
||||
dirs.add(resolveUserPath(dir));
|
||||
};
|
||||
addDir(opts?.authDir);
|
||||
for (const dir of opts?.lidMappingDirs ?? []) addDir(dir);
|
||||
for (const dir of opts?.lidMappingDirs ?? []) {
|
||||
addDir(dir);
|
||||
}
|
||||
addDir(resolveOAuthDir());
|
||||
addDir(path.join(CONFIG_DIR, "credentials"));
|
||||
return [...dirs];
|
||||
@@ -101,7 +117,9 @@ function readLidReverseMapping(lid: string, opts?: JidToE164Options): string | n
|
||||
try {
|
||||
const data = fs.readFileSync(mappingPath, "utf8");
|
||||
const phone = JSON.parse(data) as string | number | null;
|
||||
if (phone === null || phone === undefined) continue;
|
||||
if (phone === null || phone === undefined) {
|
||||
continue;
|
||||
}
|
||||
return normalizeE164(String(phone));
|
||||
} catch {
|
||||
// Try the next location.
|
||||
@@ -123,7 +141,9 @@ export function jidToE164(jid: string, opts?: JidToE164Options): string | null {
|
||||
if (lidMatch) {
|
||||
const lid = lidMatch[1];
|
||||
const phone = readLidReverseMapping(lid, opts);
|
||||
if (phone) return phone;
|
||||
if (phone) {
|
||||
return phone;
|
||||
}
|
||||
const shouldLog = opts?.logMissing ?? shouldLogVerbose();
|
||||
if (shouldLog) {
|
||||
logVerbose(`LID mapping not found for ${lid}; skipping inbound message`);
|
||||
@@ -137,14 +157,24 @@ export async function resolveJidToE164(
|
||||
jid: string | null | undefined,
|
||||
opts?: JidToE164Options & { lidLookup?: LidLookup },
|
||||
): Promise<string | null> {
|
||||
if (!jid) return null;
|
||||
if (!jid) {
|
||||
return null;
|
||||
}
|
||||
const direct = jidToE164(jid, opts);
|
||||
if (direct) return direct;
|
||||
if (!/(@lid|@hosted\.lid)$/.test(jid)) return null;
|
||||
if (!opts?.lidLookup?.getPNForLID) return null;
|
||||
if (direct) {
|
||||
return direct;
|
||||
}
|
||||
if (!/(@lid|@hosted\.lid)$/.test(jid)) {
|
||||
return null;
|
||||
}
|
||||
if (!opts?.lidLookup?.getPNForLID) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const pnJid = await opts.lidLookup.getPNForLID(jid);
|
||||
if (!pnJid) return null;
|
||||
if (!pnJid) {
|
||||
return null;
|
||||
}
|
||||
return jidToE164(pnJid, opts);
|
||||
} catch (err) {
|
||||
if (shouldLogVerbose()) {
|
||||
@@ -197,13 +227,17 @@ export function sliceUtf16Safe(input: string, start: number, end?: number): stri
|
||||
|
||||
export function truncateUtf16Safe(input: string, maxLen: number): string {
|
||||
const limit = Math.max(0, Math.floor(maxLen));
|
||||
if (input.length <= limit) return input;
|
||||
if (input.length <= limit) {
|
||||
return input;
|
||||
}
|
||||
return sliceUtf16Safe(input, 0, limit);
|
||||
}
|
||||
|
||||
export function resolveUserPath(input: string): string {
|
||||
const trimmed = input.trim();
|
||||
if (!trimmed) return trimmed;
|
||||
if (!trimmed) {
|
||||
return trimmed;
|
||||
}
|
||||
if (trimmed.startsWith("~")) {
|
||||
const expanded = trimmed.replace(/^~(?=$|[\\/])/, os.homedir());
|
||||
return path.resolve(expanded);
|
||||
@@ -216,11 +250,15 @@ export function resolveConfigDir(
|
||||
homedir: () => string = os.homedir,
|
||||
): string {
|
||||
const override = env.OPENCLAW_STATE_DIR?.trim() || env.CLAWDBOT_STATE_DIR?.trim();
|
||||
if (override) return resolveUserPath(override);
|
||||
if (override) {
|
||||
return resolveUserPath(override);
|
||||
}
|
||||
const newDir = path.join(homedir(), ".openclaw");
|
||||
try {
|
||||
const hasNew = fs.existsSync(newDir);
|
||||
if (hasNew) return newDir;
|
||||
if (hasNew) {
|
||||
return newDir;
|
||||
}
|
||||
} catch {
|
||||
// best-effort
|
||||
}
|
||||
@@ -229,9 +267,13 @@ export function resolveConfigDir(
|
||||
|
||||
export function resolveHomeDir(): string | undefined {
|
||||
const envHome = process.env.HOME?.trim();
|
||||
if (envHome) return envHome;
|
||||
if (envHome) {
|
||||
return envHome;
|
||||
}
|
||||
const envProfile = process.env.USERPROFILE?.trim();
|
||||
if (envProfile) return envProfile;
|
||||
if (envProfile) {
|
||||
return envProfile;
|
||||
}
|
||||
try {
|
||||
const home = os.homedir();
|
||||
return home?.trim() ? home : undefined;
|
||||
@@ -241,18 +283,30 @@ export function resolveHomeDir(): string | undefined {
|
||||
}
|
||||
|
||||
export function shortenHomePath(input: string): string {
|
||||
if (!input) return input;
|
||||
if (!input) {
|
||||
return input;
|
||||
}
|
||||
const home = resolveHomeDir();
|
||||
if (!home) return input;
|
||||
if (input === home) return "~";
|
||||
if (input.startsWith(`${home}/`)) return `~${input.slice(home.length)}`;
|
||||
if (!home) {
|
||||
return input;
|
||||
}
|
||||
if (input === home) {
|
||||
return "~";
|
||||
}
|
||||
if (input.startsWith(`${home}/`)) {
|
||||
return `~${input.slice(home.length)}`;
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
export function shortenHomeInString(input: string): string {
|
||||
if (!input) return input;
|
||||
if (!input) {
|
||||
return input;
|
||||
}
|
||||
const home = resolveHomeDir();
|
||||
if (!home) return input;
|
||||
if (!home) {
|
||||
return input;
|
||||
}
|
||||
return input.split(home).join("~");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user