Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
import type { GroupMetadata } from '@whiskeysockets/baileys';
import type { WASocket } from '@whiskeysockets/baileys';
import { BaileysGroups, BaileysGroupsHost, collectGroupLidTwins } from './baileys-groups';
/**
* The group roster carries each member's phone twin inline (`participant.phoneNumber`, `metadata.ownerPn`),
* but `resolvePhone` reads only the lid map that 1:1 traffic feeds. A member present solely in a roster
* resolved to `null` on `GET /contacts/{lid}/phone` (#1510). getGroupInfo now harvests the roster twins
* into that map, and only the @lid-addressed ones (a phone-addressed participant would key a bogus pair).
*/
const meta = (over: Partial<GroupMetadata> = {}): GroupMetadata =>
({
id: '[email protected]',
subject: 'Ops',
owner: '111222333@lid',
ownerPn: '[email protected]',
creation: 1,
participants: [
{ id: '111222333@lid', phoneNumber: '[email protected]', admin: 'superadmin' },
{ id: '444555666@lid', phoneNumber: '[email protected]', admin: null },
// @lid with no server-sent twin (non-contact privacy default): nothing to harvest.
{ id: '777888999@lid', phoneNumber: undefined, admin: null },
// already phone-addressed: no lid to key, must NOT become a phone->phone pair.
{ id: '[email protected]', phoneNumber: '[email protected]', admin: null },
],
...over,
}) as unknown as GroupMetadata;
describe('collectGroupLidTwins', () => {
it('harvests only @lid participants that carry a phone twin, plus the @lid owner', () => {
expect(collectGroupLidTwins(meta())).toEqual([
{ lid: '111222333@lid', pn: '[email protected]' },
{ lid: '444555666@lid', pn: '[email protected]' },
{ lid: '111222333@lid', pn: '[email protected]' },
]);
});
it('skips the owner when it is phone-addressed or has no twin', () => {
expect(collectGroupLidTwins(meta({ owner: '[email protected]', ownerPn: undefined }))).toEqual([
{ lid: '111222333@lid', pn: '[email protected]' },
{ lid: '444555666@lid', pn: '[email protected]' },
]);
expect(collectGroupLidTwins(meta({ owner: '111222333@lid', ownerPn: undefined }))).toEqual([
{ lid: '111222333@lid', pn: '[email protected]' },
{ lid: '444555666@lid', pn: '[email protected]' },
]);
});
it('returns nothing for an all-phone roster (no lids to key)', () => {
const phoneOnly = meta({
owner: '[email protected]',
ownerPn: undefined,
participants: [{ id: '[email protected]', phoneNumber: '[email protected]', admin: null }] as never,
});
expect(collectGroupLidTwins(phoneOnly)).toEqual([]);
});
});
describe('getGroupInfo harvests the roster twins into the lid map', () => {
const build = (metadata: GroupMetadata) => {
const addLidMappings = jest.fn();
const host = {
ensureReady: () => undefined,
getSocket: () => ({ groupMetadata: jest.fn().mockResolvedValue(metadata) }) as unknown as WASocket,
logger: { warn: jest.fn(), debug: jest.fn(), info: jest.fn(), error: jest.fn() },
toNeutralJid: (j: string) => j,
toEngineJid: (j: string) => j,
normalizedSelfJid: () => '[email protected]',
addLidMappings,
} as unknown as BaileysGroupsHost;
return { groups: new BaileysGroups(host, 500), addLidMappings };
};
it('feeds the collected @lid twins to addLidMappings on a successful read', async () => {
const { groups, addLidMappings } = build(meta());
await groups.getGroupInfo('[email protected]');
expect(addLidMappings).toHaveBeenCalledWith(collectGroupLidTwins(meta()));
});
});