Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): report wwjs group createdAt and announce flags correctly

Yudhi Armyndharisdc25f2bdfd86
diff --git a/src/engine/adapters/wwebjs-group-info.spec.ts b/src/engine/adapters/wwebjs-group-info.spec.ts
new file mode 100644
index 000000000..2bfa4b1c6
--- /dev/null
+++ b/src/engine/adapters/wwebjs-group-info.spec.ts
@@ -0,0 +1,89 @@
+import type { Client } from 'whatsapp-web.js';
+import { WwebjsGroups } from './wwebjs-groups';
+import { createLogger } from '../../common/services/logger.service';
+import { type WwebjsEngineHost } from './wwebjs-host';
+
+/**
+ * getGroupInfo reads a whatsapp-web.js GroupChat, whose accessors do not match the neutral contract:
+ * `createdAt` is a getter returning a Date (GroupChat.js:36), there is no `isAnnounce` at all, and
+ * `isReadOnly` is overwritten with the group's announce setting (Injected/Utils.js:1005). These pin the
+ * fields to what the Baileys mapper reports for the same group.
+ */
+const logger = createLogger('wwebjs-group-info.spec');
+
+const SELF = '[email protected]';
+const OTHER = '[email protected]';
+
+function makeGroups(chat: Record<string, unknown>): WwebjsGroups {
+ const client = {
+ info: { wid: { _serialized: SELF } },
+ getChatById: jest
+ .fn()
+ .mockResolvedValue({ id: { _serialized: '[email protected]' }, name: 'Team', isGroup: true, ...chat }),
+ };
+ const host = {
+ ensureReady: jest.fn(),
+ getClient: () => client as unknown as Client,
+ logger,
+ } as unknown as WwebjsEngineHost;
+ return new WwebjsGroups(host);
+}
+
+const participant = (id: string, isAdmin: boolean): Record<string, unknown> => ({
+ id: { _serialized: id, user: id.split('@')[0] },
+ isAdmin,
+ isSuperAdmin: false,
+});
+
+describe('WwebjsGroups.getGroupInfo', () => {
+ it('reports createdAt in Unix seconds, not the Date the GroupChat getter builds', async () => {
+ const groupMetadata = { creation: 1718900000 };
+ const info = await makeGroups({
+ groupMetadata,
+ participants: [],
+ // The real getter: new Date(creation * 1000), which serialises as an ISO string.
+ get createdAt() {
+ return new Date(groupMetadata.creation * 1000);
+ },
+ }).getGroupInfo('[email protected]');
+
+ expect(info?.createdAt).toBe(1718900000);
+ });
+
+ it('omits createdAt when the metadata carries no creation time', async () => {
+ const info = await makeGroups({ groupMetadata: {}, participants: [] }).getGroupInfo('[email protected]');
+
+ expect(info?.createdAt).toBeUndefined();
+ });
+
+ // WA Web's own isReadOnly is the announce setting, so an admin of an announce-only group was told
+ // they could not post, while isAnnounce (absent on GroupChat) always read false.
+ it('lets an admin of an announce-only group post', async () => {
+ const info = await makeGroups({
+ groupMetadata: { announce: true },
+ isReadOnly: true,
+ participants: [participant(SELF, true), participant(OTHER, false)],
+ }).getGroupInfo('[email protected]');
+
+ expect(info).toMatchObject({ isAnnounce: true, isReadOnly: false, announce: true });
+ });
+
+ it('reports a non-admin of an announce-only group as read-only', async () => {
+ const info = await makeGroups({
+ groupMetadata: { announce: true },
+ isReadOnly: true,
+ participants: [participant(SELF, false), participant(OTHER, true)],
+ }).getGroupInfo('[email protected]');
+
+ expect(info).toMatchObject({ isAnnounce: true, isReadOnly: true });
+ });
+
+ it('reports an open group as writable for everyone', async () => {
+ const info = await makeGroups({
+ groupMetadata: { announce: false },
+ participants: [participant(SELF, false)],
+ }).getGroupInfo('[email protected]');
+
+ expect(info).toMatchObject({ isAnnounce: false, isReadOnly: false });
+ });
+});
diff --git a/src/engine/adapters/wwebjs-groups.ts b/src/engine/adapters/wwebjs-groups.ts
index ee9b7989b..4eda8899d 100644
--- a/src/engine/adapters/wwebjs-groups.ts
+++ b/src/engine/adapters/wwebjs-groups.ts
@@ -128,15 +128,25 @@ export class WwebjsGroups {
isSuperAdmin: Boolean(p.isSuperAdmin),
}));
+ // GroupChat.createdAt is a getter returning a Date (GroupChat.js:36), which serialises as an
+ // ISO string; the contract is Unix seconds, so read the raw metadata field instead.
+ const creation = (groupChat.groupMetadata as { creation?: unknown } | undefined)?.creation;
+ // GroupChat has no isAnnounce, and WA Web overwrites isReadOnly with the announce SETTING
+ // (Injected/Utils.js:1005), which tells an admin of an announce-only group they cannot post.
+ // Derive both from the metadata, as the Baileys mapper does.
+ const announce = Boolean(groupChat.groupMetadata?.announce);
+ const selfWid = readWid(this.client().info?.wid);
+ const selfIsAdmin = participants.some(p => p.isAdmin && selfWid !== undefined && p.id === selfWid);
+
return {
id: chat.id._serialized,
name: chat.name,
description: groupChat.description ? String(groupChat.description) : undefined,
owner: readWid(groupChat.owner),
- createdAt: groupChat.createdAt,
+ createdAt: typeof creation === 'number' ? creation : undefined,
participants,
- isReadOnly: Boolean(groupChat.isReadOnly),
- isAnnounce: Boolean(groupChat.isAnnounce),
+ isReadOnly: announce && !selfIsAdmin,
+ isAnnounce: announce,
announce: groupChat.groupMetadata?.announce,
locked: groupChat.groupMetadata?.restrict,
ephemeralSeconds: groupChat.groupMetadata?.ephemeralDuration,