Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): answer three group and contact fields honestly

Yudhi Armyndharisb8964e9fd8d5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c0ed4ef04..9b1470ca3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- `isReadOnly` on a group answers for the calling account rather than repeating the group setting, so an admin of an announce-only group is no longer told they cannot post.
+- `isMyContact` reflects whether the contact is actually saved, instead of reporting `true` for every contact the Baileys engine has seen.
+- Listing membership requests for an id that is not a group is refused instead of answering an empty list, which read as a group with nothing pending.
- Three Baileys operations answer the refusal they were hiding: leaving a group and unsubscribing from a channel map WhatsApp's rejection like their sibling writes already did, and labelling a channel is refused outright instead of reporting success while nothing was labelled.
- A message's delivery status is announced, not only coloured. `delivered` and `read` render the same double check and differed only by a blue that measured 2.13:1 on the outgoing bubble, so the distinction reached neither screen readers nor colour-blind readers.
- Text rendered in a brand or status colour meets AA on the light theme. As foregrounds they measured 1.98:1 (brand), 2.15:1 (warning), 2.28:1 (success) and 3.76:1 (error); darkened `-text` twins now carry text and icons while the originals stay the fill colour. Each clears 4.5:1 against the tint its own badges paint behind it, not just against white. Dark theme is unchanged.
diff --git a/src/engine/adapters/baileys-group-mapper.spec.ts b/src/engine/adapters/baileys-group-mapper.spec.ts
index 8e8dd32ff..9d60de432 100644
--- a/src/engine/adapters/baileys-group-mapper.spec.ts
+++ b/src/engine/adapters/baileys-group-mapper.spec.ts
@@ -1,5 +1,6 @@
import type { GroupMetadata } from '@whiskeysockets/baileys';
import { mapBaileysGroup, mapBaileysGroupInfo } from './baileys-group-mapper';
+const identity = (jid: string): string => jid;
const meta = (over: Partial<GroupMetadata> = {}): GroupMetadata => ({
@@ -43,6 +44,24 @@ describe('mapBaileysGroup', () => {
});
describe('mapBaileysGroupInfo', () => {
+ /**
+ * isAnnounce is the group SETTING; isReadOnly is what that setting means for THIS account, which
+ * is the field a client uses to disable its composer. Copying announce into both told an admin of
+ * an announce-only group that they could not post, while whatsapp-web.js reads WA Web's own
+ * per-account flag and says they can.
+ */
+ it('reads isReadOnly against the calling account, not the group setting', () => {
+ const announceOnly = meta({ announce: true });
+ // 628999 is a superadmin and 628222 an admin in the fixture; 628111 is a plain member.
+ expect(mapBaileysGroupInfo(announceOnly, identity, '[email protected]').isReadOnly).toBe(false);
+ expect(mapBaileysGroupInfo(announceOnly, identity, '[email protected]').isReadOnly).toBe(false);
+ expect(mapBaileysGroupInfo(announceOnly, identity, '[email protected]').isReadOnly).toBe(true);
+ // The setting itself is unchanged for every caller.
+ expect(mapBaileysGroupInfo(announceOnly, identity, '[email protected]').isAnnounce).toBe(true);
+ // An open group is writable for everyone, admin or not.
+ expect(mapBaileysGroupInfo(meta({ announce: false }), identity, '[email protected]').isReadOnly).toBe(false);
+ });
+
it('maps full info incl. participants admin/superadmin', () => {
const info = mapBaileysGroupInfo(meta({ announce: true }));
expect(info.id).toBe('[email protected]');
diff --git a/src/engine/adapters/baileys-group-mapper.ts b/src/engine/adapters/baileys-group-mapper.ts
index 72ef67071..743fb6cac 100644
--- a/src/engine/adapters/baileys-group-mapper.ts
+++ b/src/engine/adapters/baileys-group-mapper.ts
@@ -50,7 +50,11 @@ export function mapBaileysGroup(
}
/** Map a Baileys GroupMetadata to the neutral {@link GroupInfo} (full participant list). */
-export function mapBaileysGroupInfo(metadata: GroupMetadata, normalizeJid: NormalizeJid = identity): GroupInfo {
+export function mapBaileysGroupInfo(
+ metadata: GroupMetadata,
+ normalizeJid: NormalizeJid = identity,
+ selfJid?: string,
+): GroupInfo {
const participants: GroupParticipant[] = metadata.participants.map(p => {
const id = preferPhoneDialect(p.id, p.phoneNumber, normalizeJid);
return {
@@ -68,9 +72,14 @@ export function mapBaileysGroupInfo(metadata: GroupMetadata, normalizeJid: Norma
owner: metadata.owner ? preferPhoneDialect(metadata.owner, metadata.ownerPn, normalizeJid) : metadata.owner,
createdAt: metadata.creation,
participants,
- // WhatsApp "announce" = only admins can post; surface as both isAnnounce and (members') isReadOnly (best-effort).
+ // WhatsApp "announce" = only admins can post. isAnnounce reports the group SETTING; isReadOnly
+ // reports what it means for THIS account, which is the question whatsapp-web.js answers with WA
+ // Web's own per-account flag. Copying announce into both told an admin of an announce-only group
+ // that they could not post, in the one field a client uses to disable its composer.
isAnnounce: metadata.announce,
- isReadOnly: metadata.announce,
+ isReadOnly: selfJid
+ ? Boolean(metadata.announce) && !isSelfAdmin(metadata, selfJid, normalizeJid)
+ : metadata.announce,
announce: metadata.announce,
locked: metadata.restrict,
ephemeralSeconds: metadata.ephemeralDuration,
diff --git a/src/engine/adapters/baileys-groups.ts b/src/engine/adapters/baileys-groups.ts
index 277d24a23..5ecb3b681 100644
--- a/src/engine/adapters/baileys-groups.ts
+++ b/src/engine/adapters/baileys-groups.ts
@@ -151,7 +151,7 @@ export class BaileysGroups {
this.queryBudgetMs,
'WhatsApp did not answer the group metadata query in time',
);
- return mapBaileysGroupInfo(metadata, jid => this.host.toNeutralJid(jid));
+ return mapBaileysGroupInfo(metadata, jid => this.host.toNeutralJid(jid), this.host.normalizedSelfJid());
} catch (err) {
// Only a SERVER refusal may become null (→ service 404): the group does not exist or the
// account cannot see it. Anything else — a dropped socket, a timeout, a protocol error —
diff --git a/src/engine/adapters/baileys-session-store.spec.ts b/src/engine/adapters/baileys-session-store.spec.ts
index fb658efb3..fc56ae60b 100644
--- a/src/engine/adapters/baileys-session-store.spec.ts
+++ b/src/engine/adapters/baileys-session-store.spec.ts
@@ -26,6 +26,22 @@ describe('BaileysSessionStore', () => {
expect(store.listContacts()).toHaveLength(1);
});
+ /**
+ * Baileys documents `name` as the one YOU saved and `notify` as the pushname the contact set
+ * themselves, so a contact carrying only `notify` is not in the addressbook. Reporting true for
+ * everyone told an automation that every chat partner was a saved contact.
+ */
+ it('reports isMyContact from the saved name, not for every contact seen', () => {
+ store.upsertContacts([
+ { id: '[email protected]', name: 'Alice', notify: 'Al' },
+ { id: '[email protected]', notify: 'Bob' },
+ ]);
+ expect(store.findContact('[email protected]')?.isMyContact).toBe(true);
+ expect(store.findContact('[email protected]')?.isMyContact).toBe(false);
+ // The pushname still surfaces either way; it is the addressbook claim that changed.
+ expect(store.findContact('[email protected]')?.pushName).toBe('Bob');
+ });
+
it('records the newest message per chat and surfaces it in getChats', () => {
store.upsertChats([{ id: '[email protected]', name: 'Alice', unreadCount: 2 }]);
store.recordMessage({
diff --git a/src/engine/adapters/baileys-session-store.ts b/src/engine/adapters/baileys-session-store.ts
index 9ba882958..92145f477 100644
--- a/src/engine/adapters/baileys-session-store.ts
+++ b/src/engine/adapters/baileys-session-store.ts
@@ -364,7 +364,11 @@ export class BaileysSessionStore {
name: c.name ?? c.verifiedName,
pushName: c.notify,
number,
- isMyContact: true, // best-effort: present in the synced address book / chat list
+ // Baileys distinguishes the two names: `name` is documented as the one YOU saved on your
+ // WhatsApp, `notify` as the pushname the contact set themselves. Reporting true for everyone
+ // told an automation that every chat partner was in the addressbook, which is what
+ // whatsapp-web.js reports honestly from the Contact model.
+ isMyContact: Boolean(c.name),
isBlocked: false, // best-effort: blocklist state is not tracked in this slice
profilePicUrl: c.imgUrl ?? undefined,
};
diff --git a/src/engine/adapters/membership-requests.spec.ts b/src/engine/adapters/membership-requests.spec.ts
index 98b3a99e2..8eb3d3dc6 100644
--- a/src/engine/adapters/membership-requests.spec.ts
+++ b/src/engine/adapters/membership-requests.spec.ts
@@ -1,4 +1,5 @@
import { WwebjsGroups, normalizeWwebjsRequestMethod } from './wwebjs-groups';
+import { GroupNotFoundError } from '../../common/errors/group-not-found.error';
import { BaileysGroups, type BaileysGroupsHost } from './baileys-groups';
import { BaileysEvents, type BaileysEventsHost } from './baileys-events';
import { EngineRefusedError } from '../../common/errors/engine-refused.error';
@@ -30,6 +31,7 @@ const logger = createLogger('membership-requests.spec');
// ---------------------------------------------------------------------------------------------
type WwebjsClientStub = {
+ getChatById: jest.Mock;
getGroupMembershipRequests: jest.Mock;
approveGroupMembershipRequests: jest.Mock;
rejectGroupMembershipRequests: jest.Mock;
@@ -37,6 +39,9 @@ type WwebjsClientStub = {
function makeWwebjsGroups(): { groups: WwebjsGroups; client: WwebjsClientStub } {
const client: WwebjsClientStub = {
+ // The list resolves the group first, like every other group operation in this adapter, so the
+ // stub has to answer that lookup or the guard reads it as "no such group".
+ getChatById: jest.fn().mockResolvedValue({ isGroup: true }),
getGroupMembershipRequests: jest.fn(),
approveGroupMembershipRequests: jest.fn(),
rejectGroupMembershipRequests: jest.fn(),
@@ -60,6 +65,16 @@ describe('WwebjsGroups membership requests', () => {
expect(normalizeWwebjsRequestMethod(undefined)).toBeUndefined();
});
+ // Every other group operation resolves the chat first; this list did not, so an unknown id or a
+ // non-group jid answered 200 with an empty array, which reads as "no pending requests" rather than
+ // "no such group". Baileys answers the refusal for the same call.
+ it('refuses an id that is not a group instead of answering an empty list', async () => {
+ const { groups, client } = makeWwebjsGroups();
+ client.getChatById.mockResolvedValue({ isGroup: false });
+ await expect(groups.getGroupMembershipRequests('[email protected]')).rejects.toBeInstanceOf(GroupNotFoundError);
+ expect(client.getGroupMembershipRequests).not.toHaveBeenCalled();
+ });
+
it('lists membership requests, reading both wid spellings and dropping unreadable requesters', async () => {
const { groups, client } = makeWwebjsGroups();
client.getGroupMembershipRequests.mockResolvedValue([
diff --git a/src/engine/adapters/wwebjs-groups.ts b/src/engine/adapters/wwebjs-groups.ts
index 8c2872b0c..7840d1a8e 100644
--- a/src/engine/adapters/wwebjs-groups.ts
+++ b/src/engine/adapters/wwebjs-groups.ts
@@ -520,7 +520,10 @@ export class WwebjsGroups {
}
async getGroupMembershipRequests(groupId: string): Promise<GroupMembershipRequest[]> {
- this.host.ensureReady();
+ // Resolved first, like every other group operation in this file. Without it an unknown id or a
+ // non-group jid answered 200 with an empty list, which reads as "this group has no pending
+ // requests" rather than "there is no such group"; Baileys answers the refusal.
+ await this.requireGroupChat(groupId);
try {
const raw = await this.client().getGroupMembershipRequests(groupId);
// Raw page-context store objects: wids can arrive as {_serialized} OR {$1} (the #747