Skip to content

Commit b3b2d24

Browse files
rootclaude
authored andcommitted
fix: replace as any LID casts with typed interfaces (LidMessageKey, LidContact)
Addresses Sourcery AI review feedback: introduces LidMessageKey and LidContact interfaces extending Baileys proto types with the LID-specific fields (remoteJidAlt, lidJidAlt), and adds resolveLidJid/resolveLidContact helpers to eliminate all `as any` casts for LID resolution across the codebase. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6275b10 commit b3b2d24

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import { PrismaRepository } from '@api/repository/repository.service';
5858
import { chatbotController, waMonitor } from '@api/server.module';
5959
import { CacheService } from '@api/services/cache.service';
6060
import { ChannelStartupService } from '@api/services/channel.service';
61-
import { Events, MessageSubtype, TypeMediaMessage, wa } from '@api/types/wa.types';
61+
import { Events, LidContact, LidMessageKey, MessageSubtype, resolveLidContact, resolveLidJid, TypeMediaMessage, wa } from '@api/types/wa.types';
6262
import { CacheEngine } from '@cache/cacheengine';
6363
import {
6464
CacheConf,
@@ -779,12 +779,10 @@ export class BaileysStartupService extends ChannelStartupService {
779779
};
780780

781781
private readonly contactHandle = {
782-
'contacts.upsert': async (contacts: Contact[]) => {
782+
'contacts.upsert': async (contacts: LidContact[]) => {
783783
try {
784784
const contactsRaw: any = contacts.map((contact) => ({
785-
remoteJid: contact.id.includes('@lid') && (contact as any)?.lidJidAlt
786-
? (contact as any).lidJidAlt
787-
: contact.id,
785+
remoteJid: resolveLidContact(contact),
788786
pushName: contact?.name || contact?.verifiedName || contact.id.split('@')[0],
789787
profilePicUrl: null,
790788
instanceId: this.instanceId,
@@ -822,10 +820,8 @@ export class BaileysStartupService extends ChannelStartupService {
822820
}
823821

824822
const updatedContacts = await Promise.all(
825-
contacts.map(async (contact) => ({
826-
remoteJid: contact.id.includes('@lid') && (contact as any)?.lidJidAlt
827-
? (contact as any).lidJidAlt
828-
: contact.id,
823+
contacts.map(async (contact: LidContact) => ({
824+
remoteJid: resolveLidContact(contact),
829825
pushName: contact?.name || contact?.verifiedName || contact.id.split('@')[0],
830826
profilePicUrl: (await this.profilePicture(contact.id)).profilePictureUrl,
831827
instanceId: this.instanceId,
@@ -1143,8 +1139,8 @@ export class BaileysStartupService extends ChannelStartupService {
11431139
}
11441140

11451141
// Resolve @lid to @s.whatsapp.net so all downstream uses (prepareMessage, chatbot, contact) get the correct JID
1146-
if (received.key.remoteJid?.includes('@lid') && (received.key as any)?.remoteJidAlt) {
1147-
received.key.remoteJid = (received.key as any).remoteJidAlt;
1142+
if (received.key.remoteJid?.includes('@lid') && (received.key as LidMessageKey)?.remoteJidAlt) {
1143+
received.key.remoteJid = (received.key as LidMessageKey).remoteJidAlt;
11481144
}
11491145

11501146
if (settings?.groupsIgnore && received.key.remoteJid.includes('@g.us')) {
@@ -1426,8 +1422,9 @@ export class BaileysStartupService extends ChannelStartupService {
14261422
}
14271423

14281424
// Resolve @lid to @s.whatsapp.net using the stored message key as source of truth
1425+
const lidKey = key as LidMessageKey;
14291426
const resolvedRemoteJid: string = key.remoteJid?.includes('@lid')
1430-
? ((key as any)?.remoteJidAlt ?? (findMessage.key as any)?.remoteJid ?? key.remoteJid)
1427+
? (lidKey.remoteJidAlt ?? (findMessage.key as LidMessageKey)?.remoteJid ?? key.remoteJid)
14311428
: key.remoteJid;
14321429

14331430
if (update.message === null && update.status === undefined) {
@@ -1676,9 +1673,7 @@ export class BaileysStartupService extends ChannelStartupService {
16761673
for (const event of payload) {
16771674
if (typeof event.key.remoteJid === 'string' && typeof event.receipt.readTimestamp === 'number') {
16781675
// Resolve @lid to @s.whatsapp.net so read receipts match stored messages
1679-
const jid = event.key.remoteJid.includes('@lid') && (event.key as any)?.remoteJidAlt
1680-
? (event.key as any).remoteJidAlt
1681-
: event.key.remoteJid;
1676+
const jid = resolveLidJid(event.key as LidMessageKey);
16821677
remotesJidMap[jid] = event.receipt.readTimestamp;
16831678
}
16841679
}

src/api/types/wa.types.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
/* eslint-disable @typescript-eslint/no-namespace */
22
import { JsonValue } from '@prisma/client/runtime/library';
3-
import { AuthenticationState, WAConnectionState } from 'baileys';
3+
import { AuthenticationState, Contact, proto, WAConnectionState } from 'baileys';
4+
5+
/**
6+
* LID (Linked Identity Device) extensions for Baileys types.
7+
* The EvolutionAPI Baileys fork adds `remoteJidAlt` to message keys and
8+
* `lidJidAlt` to contacts when the original JID uses the @lid suffix.
9+
* These interfaces provide type-safe access to those fields.
10+
*/
11+
export interface LidMessageKey extends proto.IMessageKey {
12+
remoteJidAlt?: string;
13+
}
14+
15+
export interface LidContact extends Contact {
16+
lidJidAlt?: string;
17+
}
418

519
export enum Events {
620
APPLICATION_STARTUP = 'application.startup',
@@ -131,6 +145,20 @@ export declare namespace wa {
131145
export type StatusMessage = 'ERROR' | 'PENDING' | 'SERVER_ACK' | 'DELIVERY_ACK' | 'READ' | 'DELETED' | 'PLAYED';
132146
}
133147

148+
/** Resolve a @lid JID to its @s.whatsapp.net alternative when available. */
149+
export function resolveLidJid(key: LidMessageKey): string {
150+
return key.remoteJid?.includes('@lid') && key.remoteJidAlt
151+
? key.remoteJidAlt
152+
: key.remoteJid;
153+
}
154+
155+
/** Resolve a @lid contact ID to its @s.whatsapp.net alternative when available. */
156+
export function resolveLidContact(contact: LidContact): string {
157+
return contact.id?.includes('@lid') && contact.lidJidAlt
158+
? contact.lidJidAlt
159+
: contact.id;
160+
}
161+
134162
export const TypeMediaMessage = ['imageMessage', 'documentMessage', 'audioMessage', 'videoMessage', 'stickerMessage', 'ptvMessage'];
135163

136164
export const MessageSubtype = [

0 commit comments

Comments
 (0)