mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 08:41:23 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -15,14 +15,18 @@ function unwrapMessage(message: proto.IMessage | undefined): proto.IMessage | un
|
||||
}
|
||||
|
||||
function extractContextInfo(message: proto.IMessage | undefined): proto.IContextInfo | undefined {
|
||||
if (!message) return undefined;
|
||||
if (!message) {
|
||||
return undefined;
|
||||
}
|
||||
const contentType = getContentType(message);
|
||||
const candidate = contentType ? (message as Record<string, unknown>)[contentType] : undefined;
|
||||
const contextInfo =
|
||||
candidate && typeof candidate === "object" && "contextInfo" in candidate
|
||||
? (candidate as { contextInfo?: proto.IContextInfo }).contextInfo
|
||||
: undefined;
|
||||
if (contextInfo) return contextInfo;
|
||||
if (contextInfo) {
|
||||
return contextInfo;
|
||||
}
|
||||
const fallback =
|
||||
message.extendedTextMessage?.contextInfo ??
|
||||
message.imageMessage?.contextInfo ??
|
||||
@@ -36,19 +40,29 @@ function extractContextInfo(message: proto.IMessage | undefined): proto.IContext
|
||||
message.interactiveResponseMessage?.contextInfo ??
|
||||
message.buttonsMessage?.contextInfo ??
|
||||
message.listMessage?.contextInfo;
|
||||
if (fallback) return fallback;
|
||||
if (fallback) {
|
||||
return fallback;
|
||||
}
|
||||
for (const value of Object.values(message)) {
|
||||
if (!value || typeof value !== "object") continue;
|
||||
if (!("contextInfo" in value)) continue;
|
||||
if (!value || typeof value !== "object") {
|
||||
continue;
|
||||
}
|
||||
if (!("contextInfo" in value)) {
|
||||
continue;
|
||||
}
|
||||
const candidateContext = (value as { contextInfo?: proto.IContextInfo }).contextInfo;
|
||||
if (candidateContext) return candidateContext;
|
||||
if (candidateContext) {
|
||||
return candidateContext;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function extractMentionedJids(rawMessage: proto.IMessage | undefined): string[] | undefined {
|
||||
const message = unwrapMessage(rawMessage);
|
||||
if (!message) return undefined;
|
||||
if (!message) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const candidates: Array<string[] | null | undefined> = [
|
||||
message.extendedTextMessage?.contextInfo?.mentionedJid,
|
||||
@@ -64,34 +78,46 @@ export function extractMentionedJids(rawMessage: proto.IMessage | undefined): st
|
||||
];
|
||||
|
||||
const flattened = candidates.flatMap((arr) => arr ?? []).filter(Boolean);
|
||||
if (flattened.length === 0) return undefined;
|
||||
if (flattened.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
return Array.from(new Set(flattened));
|
||||
}
|
||||
|
||||
export function extractText(rawMessage: proto.IMessage | undefined): string | undefined {
|
||||
const message = unwrapMessage(rawMessage);
|
||||
if (!message) return undefined;
|
||||
if (!message) {
|
||||
return undefined;
|
||||
}
|
||||
const extracted = extractMessageContent(message);
|
||||
const candidates = [message, extracted && extracted !== message ? extracted : undefined];
|
||||
for (const candidate of candidates) {
|
||||
if (!candidate) continue;
|
||||
if (!candidate) {
|
||||
continue;
|
||||
}
|
||||
if (typeof candidate.conversation === "string" && candidate.conversation.trim()) {
|
||||
return candidate.conversation.trim();
|
||||
}
|
||||
const extended = candidate.extendedTextMessage?.text;
|
||||
if (extended?.trim()) return extended.trim();
|
||||
if (extended?.trim()) {
|
||||
return extended.trim();
|
||||
}
|
||||
const caption =
|
||||
candidate.imageMessage?.caption ??
|
||||
candidate.videoMessage?.caption ??
|
||||
candidate.documentMessage?.caption;
|
||||
if (caption?.trim()) return caption.trim();
|
||||
if (caption?.trim()) {
|
||||
return caption.trim();
|
||||
}
|
||||
}
|
||||
const contactPlaceholder =
|
||||
extractContactPlaceholder(message) ??
|
||||
(extracted && extracted !== message
|
||||
? extractContactPlaceholder(extracted as proto.IMessage | undefined)
|
||||
: undefined);
|
||||
if (contactPlaceholder) return contactPlaceholder;
|
||||
if (contactPlaceholder) {
|
||||
return contactPlaceholder;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -99,18 +125,32 @@ export function extractMediaPlaceholder(
|
||||
rawMessage: proto.IMessage | undefined,
|
||||
): string | undefined {
|
||||
const message = unwrapMessage(rawMessage);
|
||||
if (!message) return undefined;
|
||||
if (message.imageMessage) return "<media:image>";
|
||||
if (message.videoMessage) return "<media:video>";
|
||||
if (message.audioMessage) return "<media:audio>";
|
||||
if (message.documentMessage) return "<media:document>";
|
||||
if (message.stickerMessage) return "<media:sticker>";
|
||||
if (!message) {
|
||||
return undefined;
|
||||
}
|
||||
if (message.imageMessage) {
|
||||
return "<media:image>";
|
||||
}
|
||||
if (message.videoMessage) {
|
||||
return "<media:video>";
|
||||
}
|
||||
if (message.audioMessage) {
|
||||
return "<media:audio>";
|
||||
}
|
||||
if (message.documentMessage) {
|
||||
return "<media:document>";
|
||||
}
|
||||
if (message.stickerMessage) {
|
||||
return "<media:sticker>";
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function extractContactPlaceholder(rawMessage: proto.IMessage | undefined): string | undefined {
|
||||
const message = unwrapMessage(rawMessage);
|
||||
if (!message) return undefined;
|
||||
if (!message) {
|
||||
return undefined;
|
||||
}
|
||||
const contact = message.contactMessage ?? undefined;
|
||||
if (contact) {
|
||||
const { name, phones } = describeContact({
|
||||
@@ -120,7 +160,9 @@ function extractContactPlaceholder(rawMessage: proto.IMessage | undefined): stri
|
||||
return formatContactPlaceholder(name, phones);
|
||||
}
|
||||
const contactsArray = message.contactsArrayMessage?.contacts ?? undefined;
|
||||
if (!contactsArray || contactsArray.length === 0) return undefined;
|
||||
if (!contactsArray || contactsArray.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
const labels = contactsArray
|
||||
.map((entry) => describeContact({ displayName: entry.displayName, vcard: entry.vcard }))
|
||||
.map((entry) => formatContactLabel(entry.name, entry.phones))
|
||||
@@ -140,7 +182,9 @@ function describeContact(input: { displayName?: string | null; vcard?: string |
|
||||
|
||||
function formatContactPlaceholder(name?: string, phones?: string[]): string {
|
||||
const label = formatContactLabel(name, phones);
|
||||
if (!label) return "<contact>";
|
||||
if (!label) {
|
||||
return "<contact>";
|
||||
}
|
||||
return `<contact: ${label}>`;
|
||||
}
|
||||
|
||||
@@ -158,17 +202,25 @@ function formatContactsPlaceholder(labels: string[], total: number): string {
|
||||
function formatContactLabel(name?: string, phones?: string[]): string | undefined {
|
||||
const phoneLabel = formatPhoneList(phones);
|
||||
const parts = [name, phoneLabel].filter((value): value is string => Boolean(value));
|
||||
if (parts.length === 0) return undefined;
|
||||
if (parts.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
return parts.join(", ");
|
||||
}
|
||||
|
||||
function formatPhoneList(phones?: string[]): string | undefined {
|
||||
const cleaned = phones?.map((phone) => phone.trim()).filter(Boolean) ?? [];
|
||||
if (cleaned.length === 0) return undefined;
|
||||
if (cleaned.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
const { shown, remaining } = summarizeList(cleaned, cleaned.length, 1);
|
||||
const [primary] = shown;
|
||||
if (!primary) return undefined;
|
||||
if (remaining === 0) return primary;
|
||||
if (!primary) {
|
||||
return undefined;
|
||||
}
|
||||
if (remaining === 0) {
|
||||
return primary;
|
||||
}
|
||||
return `${primary} (+${remaining} more)`;
|
||||
}
|
||||
|
||||
@@ -186,7 +238,9 @@ export function extractLocationData(
|
||||
rawMessage: proto.IMessage | undefined,
|
||||
): NormalizedLocation | null {
|
||||
const message = unwrapMessage(rawMessage);
|
||||
if (!message) return null;
|
||||
if (!message) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const live = message.liveLocationMessage ?? undefined;
|
||||
if (live) {
|
||||
@@ -242,15 +296,21 @@ export function describeReplyContext(rawMessage: proto.IMessage | undefined): {
|
||||
senderE164?: string;
|
||||
} | null {
|
||||
const message = unwrapMessage(rawMessage);
|
||||
if (!message) return null;
|
||||
if (!message) {
|
||||
return null;
|
||||
}
|
||||
const contextInfo = extractContextInfo(message);
|
||||
const quoted = normalizeMessageContent(contextInfo?.quotedMessage as proto.IMessage | undefined);
|
||||
if (!quoted) return null;
|
||||
if (!quoted) {
|
||||
return null;
|
||||
}
|
||||
const location = extractLocationData(quoted);
|
||||
const locationText = location ? formatLocationText(location) : undefined;
|
||||
const text = extractText(quoted);
|
||||
let body: string | undefined = [text, locationText].filter(Boolean).join("\n").trim();
|
||||
if (!body) body = extractMediaPlaceholder(quoted);
|
||||
if (!body) {
|
||||
body = extractMediaPlaceholder(quoted);
|
||||
}
|
||||
if (!body) {
|
||||
const quotedType = quoted ? getContentType(quoted) : undefined;
|
||||
logVerbose(
|
||||
|
||||
@@ -13,7 +13,9 @@ export async function downloadInboundMedia(
|
||||
sock: Awaited<ReturnType<typeof createWaSocket>>,
|
||||
): Promise<{ buffer: Buffer; mimetype?: string } | undefined> {
|
||||
const message = unwrapMessage(msg.message as proto.IMessage | undefined);
|
||||
if (!message) return undefined;
|
||||
if (!message) {
|
||||
return undefined;
|
||||
}
|
||||
const mimetype =
|
||||
message.imageMessage?.mimetype ??
|
||||
message.videoMessage?.mimetype ??
|
||||
|
||||
@@ -48,7 +48,9 @@ export async function monitorWebInbox(options: {
|
||||
onCloseResolve = resolve;
|
||||
});
|
||||
const resolveClose = (reason: WebListenerCloseReason) => {
|
||||
if (!onCloseResolve) return;
|
||||
if (!onCloseResolve) {
|
||||
return;
|
||||
}
|
||||
const resolver = onCloseResolve;
|
||||
onCloseResolve = null;
|
||||
resolver(reason);
|
||||
@@ -56,7 +58,9 @@ export async function monitorWebInbox(options: {
|
||||
|
||||
try {
|
||||
await sock.sendPresenceUpdate("available");
|
||||
if (shouldLogVerbose()) logVerbose("Sent global 'available' presence on connect");
|
||||
if (shouldLogVerbose()) {
|
||||
logVerbose("Sent global 'available' presence on connect");
|
||||
}
|
||||
} catch (err) {
|
||||
logVerbose(`Failed to send 'available' presence on connect: ${String(err)}`);
|
||||
}
|
||||
@@ -70,21 +74,27 @@ export async function monitorWebInbox(options: {
|
||||
msg.chatType === "group"
|
||||
? (msg.senderJid ?? msg.senderE164 ?? msg.senderName ?? msg.from)
|
||||
: msg.from;
|
||||
if (!senderKey) return null;
|
||||
if (!senderKey) {
|
||||
return null;
|
||||
}
|
||||
const conversationKey = msg.chatType === "group" ? msg.chatId : msg.from;
|
||||
return `${msg.accountId}:${conversationKey}:${senderKey}`;
|
||||
},
|
||||
shouldDebounce: options.shouldDebounce,
|
||||
onFlush: async (entries) => {
|
||||
const last = entries.at(-1);
|
||||
if (!last) return;
|
||||
if (!last) {
|
||||
return;
|
||||
}
|
||||
if (entries.length === 1) {
|
||||
await options.onMessage(last);
|
||||
return;
|
||||
}
|
||||
const mentioned = new Set<string>();
|
||||
for (const entry of entries) {
|
||||
for (const jid of entry.mentionedJids ?? []) mentioned.add(jid);
|
||||
for (const jid of entry.mentionedJids ?? []) {
|
||||
mentioned.add(jid);
|
||||
}
|
||||
}
|
||||
const combinedBody = entries
|
||||
.map((entry) => entry.body)
|
||||
@@ -114,7 +124,9 @@ export async function monitorWebInbox(options: {
|
||||
|
||||
const getGroupMeta = async (jid: string) => {
|
||||
const cached = groupMetaCache.get(jid);
|
||||
if (cached && cached.expires > Date.now()) return cached;
|
||||
if (cached && cached.expires > Date.now()) {
|
||||
return cached;
|
||||
}
|
||||
try {
|
||||
const meta = await sock.groupMetadata(jid);
|
||||
const participants =
|
||||
@@ -140,7 +152,9 @@ export async function monitorWebInbox(options: {
|
||||
};
|
||||
|
||||
const handleMessagesUpsert = async (upsert: { type?: string; messages?: Array<WAMessage> }) => {
|
||||
if (upsert.type !== "notify" && upsert.type !== "append") return;
|
||||
if (upsert.type !== "notify" && upsert.type !== "append") {
|
||||
return;
|
||||
}
|
||||
for (const msg of upsert.messages ?? []) {
|
||||
recordChannelActivity({
|
||||
channel: "whatsapp",
|
||||
@@ -149,17 +163,25 @@ export async function monitorWebInbox(options: {
|
||||
});
|
||||
const id = msg.key?.id ?? undefined;
|
||||
const remoteJid = msg.key?.remoteJid;
|
||||
if (!remoteJid) continue;
|
||||
if (remoteJid.endsWith("@status") || remoteJid.endsWith("@broadcast")) continue;
|
||||
if (!remoteJid) {
|
||||
continue;
|
||||
}
|
||||
if (remoteJid.endsWith("@status") || remoteJid.endsWith("@broadcast")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const group = isJidGroup(remoteJid) === true;
|
||||
if (id) {
|
||||
const dedupeKey = `${options.accountId}:${remoteJid}:${id}`;
|
||||
if (isRecentInboundMessage(dedupeKey)) continue;
|
||||
if (isRecentInboundMessage(dedupeKey)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
const participantJid = msg.key?.participant ?? undefined;
|
||||
const from = group ? remoteJid : await resolveInboundJid(remoteJid);
|
||||
if (!from) continue;
|
||||
if (!from) {
|
||||
continue;
|
||||
}
|
||||
const senderE164 = group
|
||||
? participantJid
|
||||
? await resolveInboundJid(participantJid)
|
||||
@@ -190,7 +212,9 @@ export async function monitorWebInbox(options: {
|
||||
sock: { sendMessage: (jid, content) => sock.sendMessage(jid, content) },
|
||||
remoteJid,
|
||||
});
|
||||
if (!access.allowed) continue;
|
||||
if (!access.allowed) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (id && !access.isSelfChat && options.sendReadReceipts !== false) {
|
||||
const participant = msg.key?.participant;
|
||||
@@ -209,7 +233,9 @@ export async function monitorWebInbox(options: {
|
||||
}
|
||||
|
||||
// If this is history/offline catch-up, mark read above but skip auto-reply.
|
||||
if (upsert.type === "append") continue;
|
||||
if (upsert.type === "append") {
|
||||
continue;
|
||||
}
|
||||
|
||||
const location = extractLocationData(msg.message ?? undefined);
|
||||
const locationText = location ? formatLocationText(location) : undefined;
|
||||
@@ -219,7 +245,9 @@ export async function monitorWebInbox(options: {
|
||||
}
|
||||
if (!body) {
|
||||
body = extractMediaPlaceholder(msg.message ?? undefined);
|
||||
if (!body) continue;
|
||||
if (!body) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
const replyContext = describeReplyContext(msg.message as proto.IMessage | undefined);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user