Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

feat(engine): add baileys-group-mapper (GroupMetadata -> neutral)

rmyndharisa715b03cf7fa
diff --git a/src/engine/adapters/baileys-group-mapper.spec.ts b/src/engine/adapters/baileys-group-mapper.spec.ts
new file mode 100644
index 000000000..be3cbe737
--- /dev/null
+++ b/src/engine/adapters/baileys-group-mapper.spec.ts
@@ -0,0 +1,57 @@
+import type { GroupMetadata } from '@whiskeysockets/baileys';
+import { mapBaileysGroup, mapBaileysGroupInfo } from './baileys-group-mapper';
+
+const meta = (over: Partial<GroupMetadata> = {}): GroupMetadata =>
+ ({
+ subject: 'My Group',
+ owner: '[email protected]',
+ desc: 'a description',
+ creation: 1700000000,
+ announce: false,
+ participants: [
+ { id: '[email protected]', admin: 'superadmin' },
+ { id: '[email protected]', admin: null },
+ { id: '[email protected]', admin: 'admin' },
+ ],
+ ...over,
+ }) as GroupMetadata;
+
+describe('mapBaileysGroup', () => {
+ it('maps the summary shape and flags self-admin', () => {
+ const g = mapBaileysGroup(meta(), '628999:[email protected]');
+ expect(g).toEqual({
+ name: 'My Group',
+ participantsCount: 3,
+ isAdmin: true, // self is superadmin
+ linkedParentJID: null,
+ });
+ });
+
+ it('isAdmin is false when self is a non-admin member', () => {
+ expect(mapBaileysGroup(meta(), '[email protected]').isAdmin).toBe(false);
+ });
+
+ it('carries the linked community parent when present', () => {
+ expect(mapBaileysGroup(meta({ linkedParent: '[email protected]' }), '[email protected]').linkedParentJID).toBe('[email protected]');
+ });
+});
+
+describe('mapBaileysGroupInfo', () => {
+ it('maps full info incl. participants admin/superadmin', () => {
+ const info = mapBaileysGroupInfo(meta({ announce: true }), '[email protected]');
+ expect(info.id).toBe('[email protected]');
+ expect(info.name).toBe('My Group');
+ expect(info.description).toBe('a description');
+ expect(info.owner).toBe('[email protected]');
+ expect(info.createdAt).toBe(1700000000);
+ expect(info.isAnnounce).toBe(true);
+ expect(info.isReadOnly).toBe(true);
+ expect(info.participants).toEqual([
+ { id: '[email protected]', number: '628999', name: undefined, isAdmin: true, isSuperAdmin: true },
+ { id: '[email protected]', number: '628111', name: undefined, isAdmin: false, isSuperAdmin: false },
+ { id: '[email protected]', number: '628222', name: undefined, isAdmin: true, isSuperAdmin: false },
+ ]);
+ });
+});
diff --git a/src/engine/adapters/baileys-group-mapper.ts b/src/engine/adapters/baileys-group-mapper.ts
new file mode 100644
index 000000000..53227661b
--- /dev/null
+++ b/src/engine/adapters/baileys-group-mapper.ts
@@ -0,0 +1,47 @@
+import type { GroupMetadata } from '@whiskeysockets/baileys';
+import { Group, GroupInfo, GroupParticipant } from '../interfaces/whatsapp-engine.interface';
+
+/** `628xxx:[email protected]` / `628xxx@lid` -> `628xxx` (the user part, device + scheme stripped). */
+function userPart(jid: string): string {
+ return jid.split('@')[0].split(':')[0];
+}
+
+function isSelfAdmin(metadata: GroupMetadata, selfJid: string): boolean {
+ const self = userPart(selfJid);
+ return metadata.participants.some(p => userPart(p.id) === self && (p.admin === 'admin' || p.admin === 'superadmin'));
+}
+
+/** Map a Baileys GroupMetadata to the neutral summary {@link Group}. `selfJid` flags whether WE are an admin. */
+export function mapBaileysGroup(metadata: GroupMetadata, selfJid: string): Group {
+ return {
+ id: metadata.id,
+ name: metadata.subject,
+ participantsCount: metadata.participants.length,
+ isAdmin: isSelfAdmin(metadata, selfJid),
+ linkedParentJID: metadata.linkedParent ?? null,
+ };
+}
+
+/** Map a Baileys GroupMetadata to the neutral {@link GroupInfo} (full participant list). */
+// eslint-disable-next-line @typescript-eslint/no-unused-vars
+export function mapBaileysGroupInfo(metadata: GroupMetadata, selfJid: string): GroupInfo {
+ const participants: GroupParticipant[] = metadata.participants.map(p => ({
+ id: p.id,
+ number: userPart(p.id),
+ name: p.name ?? undefined,
+ isAdmin: p.admin === 'admin' || p.admin === 'superadmin',
+ isSuperAdmin: p.admin === 'superadmin',
+ }));
+ return {
+ id: metadata.id,
+ name: metadata.subject,
+ description: metadata.desc,
+ owner: metadata.owner,
+ createdAt: metadata.creation,
+ participants,
+ // WhatsApp "announce" = only admins can post; surface as both isAnnounce and (members') isReadOnly (best-effort).
+ isAnnounce: metadata.announce,
+ isReadOnly: metadata.announce,
+ linkedParentJID: metadata.linkedParent ?? null,
+ };
+}