Skip to content

Commit 185b9e3

Browse files
committed
fix: add lid to return
1 parent 65d865a commit 185b9e3

3 files changed

Lines changed: 42 additions & 10 deletions

File tree

src/whatsapp/controllers/instance.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ export class InstanceController {
191191
case 'close':
192192
await instance.setPhoneNumber(phoneNumber);
193193
await instance.connectToWhatsapp();
194-
this.eventEmitter.once('code.connection', (data: { code: string }) => {
195-
res.status(HttpStatus.OK).json(data);
194+
this.eventEmitter.once('qrcode.updated', (data: { code: string }) => {
195+
res.status(HttpStatus.OK).json({ code: data?.code });
196196
});
197197
break;
198198
case 'connecting':

src/whatsapp/dto/chat.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import { WAPresence } from '@whiskeysockets/baileys';
4141

4242
export class OnWhatsAppDto {
4343
constructor(
44-
public readonly jid: string,
4544
public readonly exists: boolean,
45+
public readonly jid: string,
4646
public readonly lid?: string,
4747
public readonly name?: string,
4848
) {}

src/whatsapp/services/whatsapp.service.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ import makeWASocket, {
6363
proto,
6464
useMultiFileAuthState,
6565
UserFacingSocketConfig,
66+
USyncQuery,
67+
USyncUser,
6668
WABrowserDescription,
6769
WACallEvent,
6870
WAConnectionState,
@@ -443,8 +445,8 @@ export class WAStartupService {
443445
return;
444446
}
445447

446-
this.instanceQr.base64 = base64;
447448
this.instanceQr.code = qr;
449+
this.instanceQr.base64 = base64;
448450

449451
this.ws.send(this.instance.name, 'qrcode.updated', this.instanceQr);
450452

@@ -561,7 +563,6 @@ export class WAStartupService {
561563
this.authState = await this.defineAuthState();
562564

563565
const { version } = fetchLatestBaileysVersionV2();
564-
console.log({ version });
565566
const session = this.configService.get<ConfigSessionPhone>('CONFIG_SESSION_PHONE');
566567
const browser: WABrowserDescription = !this.phoneNumber
567568
? [session.CLIENT, session.NAME, release()]
@@ -2008,28 +2009,59 @@ export class WAStartupService {
20082009
for await (const number of data.numbers) {
20092010
const jid = this.createJid(number);
20102011
if (isLidUser(jid)) {
2011-
onWhatsapp.push(new OnWhatsAppDto(jid, true, jid));
2012+
onWhatsapp.push(new OnWhatsAppDto(true, '', jid));
20122013
}
20132014
if (isJidGroup(jid)) {
20142015
const group = await this.findGroup({ groupJid: jid }, 'inner');
2015-
onWhatsapp.push(new OnWhatsAppDto(group.id, !!group?.id, '', group?.subject));
2016+
onWhatsapp.push(new OnWhatsAppDto(!!group?.id, group.id, '', group?.subject));
20162017
} else if (jid.includes('@broadcast')) {
2017-
onWhatsapp.push(new OnWhatsAppDto(jid, true));
2018+
onWhatsapp.push(new OnWhatsAppDto(true, jid));
20182019
} else {
20192020
try {
20202021
const result = (await this.client.onWhatsApp(jid))[0];
2022+
2023+
let lid: string | undefined;
2024+
if (result?.exists) {
2025+
const item = (await this.getLid(result.jid))[0]
2026+
lid = item?.lid
2027+
}
20212028
onWhatsapp.push(
2022-
new OnWhatsAppDto(result.jid, !!result.exists, result?.['lid'] as string),
2029+
new OnWhatsAppDto(!!result.exists, result.jid, lid),
20232030
);
20242031
} catch (error) {
2025-
onWhatsapp.push(new OnWhatsAppDto(number, false));
2032+
onWhatsapp.push(new OnWhatsAppDto(false, number));
20262033
}
20272034
}
20282035
}
20292036

20302037
return onWhatsapp;
20312038
}
20322039

2040+
public async getLid(...jids: string[]): Promise<{ id: string, lid: string }[]> {
2041+
const q = new USyncQuery().withLIDProtocol().withContext('background');
2042+
2043+
for (const jid of jids) {
2044+
if (isLidUser(jid)){
2045+
continue;
2046+
}
2047+
2048+
q.withUser(new USyncUser().withId(jid));
2049+
}
2050+
2051+
if (q.users.length === 0) {
2052+
return [];
2053+
}
2054+
2055+
const results = await this.client.executeUSyncQuery(q);
2056+
if (results) {
2057+
return results.list.
2058+
filter(i => !!i?.lid)
2059+
.map(({ lid, id }) => ({ lid, id }) as { id: string, lid: string });
2060+
}
2061+
2062+
return [];
2063+
}
2064+
20332065
/**
20342066
* @deprecated
20352067
*/

0 commit comments

Comments
 (0)