Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
refactor(engine): delegate baileys contact and chat operations to BaileysContacts
Yudhi Armyndharisa8975654a747
diff --git a/src/engine/adapters/baileys-contacts.ts b/src/engine/adapters/baileys-contacts.tsnew file mode 100644index 000000000..ac2b7e7c0--- /dev/null+++ b/src/engine/adapters/baileys-contacts.ts@@ -0,0 +1,137 @@+import type { WAMessageKey, WASocket } from '@whiskeysockets/baileys';+import { ChatSummary, Contact, MediaInput } from '../interfaces/whatsapp-engine.interface';+import { resolveMediaBuffer } from './baileys-messaging';+import { type createLogger } from '../../common/services/logger.service';++/**+ * Contacts/profile/chats-domain operations extracted from BaileysAdapter. The adapter keeps the+ * public methods as thin forwarders and injects this narrow host surface via closures, so the+ * delegate never touches lifecycle state directly.+ */+export interface BaileysContactsHost {+ ensureReady(): void;+ /** Post-ensureReady socket handle — call host.ensureReady() first. */+ getSocket(): WASocket;+ readonly logger: ReturnType<typeof createLogger>;+ normalizedSelfJid(): string;+ listContacts(): Contact[];+ findContact(contactId: string): Contact | null;+ resolvePhone(contactId: string): string | null;+ listChats(): ChatSummary[];+ /** The chat's last known message (the handle readMessages/chatModify need), or null when none. */+ lastMessage(chatId: string): { key: WAMessageKey; timestamp: number } | null;+}++export class BaileysContacts {+ constructor(private readonly host: BaileysContactsHost) {}++ /** Post-ensureReady socket handle. */+ private sock(): WASocket {+ return this.host.getSocket();+ }++ async getProfilePicture(contactId: string): Promise<string | null> {+ this.host.ensureReady();+ try {+ return (await this.sock().profilePictureUrl(contactId, 'image')) ?? null;+ } catch (err) {+ this.host.logger.debug('profilePictureUrl failed; no picture or hidden', {+ contactId,+ error: err instanceof Error ? err.message : String(err),+ });+ return null; // no picture set, or hidden by privacy+ }+ }++ async blockContact(contactId: string): Promise<void> {+ this.host.ensureReady();+ await this.sock().updateBlockStatus(contactId, 'block');+ }++ async unblockContact(contactId: string): Promise<void> {+ this.host.ensureReady();+ await this.sock().updateBlockStatus(contactId, 'unblock');+ }++ async setProfileName(name: string): Promise<void> {+ this.host.ensureReady();+ await this.sock().updateProfileName(name);+ }++ async setProfileStatus(status: string): Promise<void> {+ this.host.ensureReady();+ await this.sock().updateProfileStatus(status);+ }++ async setProfilePicture(media: MediaInput): Promise<void> {+ this.host.ensureReady();+ const selfJid = this.host.normalizedSelfJid();+ if (!selfJid) {+ throw new Error('cannot set the profile picture: the own JID is not known yet');+ }+ // updateProfilePicture takes a WAMediaUpload; resolveMediaBuffer covers Buffer | base64 | URL,+ // the same conversion the media sends use.+ const { data } = await resolveMediaBuffer(media);+ await this.sock().updateProfilePicture(selfJid, data);+ }++ // eslint-disable-next-line @typescript-eslint/require-await+ async getContacts(): Promise<Contact[]> {+ this.host.ensureReady();+ return this.host.listContacts();+ }++ // eslint-disable-next-line @typescript-eslint/require-await+ async getContactById(contactId: string): Promise<Contact | null> {+ this.host.ensureReady();+ return this.host.findContact(contactId);+ }++ // eslint-disable-next-line @typescript-eslint/require-await+ async resolveContactPhone(contactId: string): Promise<string | null> {+ this.host.ensureReady();+ return this.host.resolvePhone(contactId);+ }++ // eslint-disable-next-line @typescript-eslint/require-await+ async getChats(): Promise<ChatSummary[]> {+ this.host.ensureReady();+ return this.host.listChats();+ }++ async sendSeen(chatId: string): Promise<boolean> {+ this.host.ensureReady();+ const last = this.host.lastMessage(chatId);+ if (!last) {+ return false; // nothing known to mark read+ }+ await this.sock().readMessages([last.key]);+ return true;+ }++ async markUnread(chatId: string): Promise<boolean> {+ this.host.ensureReady();+ const last = this.host.lastMessage(chatId);+ if (!last) {+ return false; // Baileys' unread toggle needs the last message; can't synthesize it+ }+ await this.sock().chatModify(+ { markRead: false, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },+ chatId,+ );+ return true;+ }++ async deleteChat(chatId: string): Promise<boolean> {+ this.host.ensureReady();+ const last = this.host.lastMessage(chatId);+ if (!last) {+ return false; // Baileys' delete needs the last message; can't synthesize it+ }+ await this.sock().chatModify(+ { delete: true, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },+ chatId,+ );+ return true;+ }+}diff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.tsindex b666c85e9..74f222d39 100644--- a/src/engine/adapters/baileys.adapter.ts+++ b/src/engine/adapters/baileys.adapter.ts@@ -8,6 +8,7 @@ import type * as BaileysLib from '@whiskeysockets/baileys';import type { AnyMessageContent, WACallEvent, WAMessage, WASocket } from '@whiskeysockets/baileys';import { buildIncomingMessageFromBaileys, extractBaileysBody, mapBaileysStatus } from './baileys-message-mapper';import { buildEditedMessage } from './message-mapper';+import { BaileysContacts } from './baileys-contacts';import { BaileysGroups } from './baileys-groups';import { BaileysMessaging, resolveMediaBuffer } from './baileys-messaging';import type { ILogger } from '@whiskeysockets/baileys/lib/Utils/logger.js';@@ -170,6 +171,7 @@ export class BaileysAdapter implements IWhatsAppEngine {private readonly sessionStore: BaileysSessionStore;private readonly groups: BaileysGroups;private readonly messaging: BaileysMessaging;+ private readonly contacts: BaileysContacts;private sock: WASocket | null = null;private status: EngineStatus = EngineStatus.DISCONNECTED;private qrCode: string | null = null;@@ -224,6 +226,17 @@ export class BaileysAdapter implements IWhatsAppEngine {getOnMessageCreate: () => this.callbacks.onMessageCreate,mapMessage: (msg, contentType, opts) => this.mapMessage(msg, contentType, opts),});+ this.contacts = new BaileysContacts({+ ensureReady: () => this.ensureReady(),+ getSocket: () => this.sock!,+ logger: this.logger,+ normalizedSelfJid: () => this.normalizedSelfJid(),+ listContacts: () => this.sessionStore.listContacts(),+ findContact: contactId => this.sessionStore.findContact(contactId),+ resolvePhone: contactId => this.sessionStore.resolvePhone(contactId),+ listChats: () => this.sessionStore.listChats(),+ lastMessage: chatId => this.sessionStore.lastMessage(chatId),+ });}// ----- Lifecycle -----@@ -913,112 +926,59 @@ export class BaileysAdapter implements IWhatsAppEngine {}async getProfilePicture(contactId: string): Promise<string | null> {- this.ensureReady();- try {- return (await this.sock!.profilePictureUrl(contactId, 'image')) ?? null;- } catch (err) {- this.logger.debug('profilePictureUrl failed; no picture or hidden', {- contactId,- error: err instanceof Error ? err.message : String(err),- });- return null; // no picture set, or hidden by privacy- }+ return this.contacts.getProfilePicture(contactId);}async blockContact(contactId: string): Promise<void> {- this.ensureReady();- await this.sock!.updateBlockStatus(contactId, 'block');+ return this.contacts.blockContact(contactId);}async unblockContact(contactId: string): Promise<void> {- this.ensureReady();- await this.sock!.updateBlockStatus(contactId, 'unblock');+ return this.contacts.unblockContact(contactId);}// ----- Profile (own account) -----async setProfileName(name: string): Promise<void> {- this.ensureReady();- await this.sock!.updateProfileName(name);+ return this.contacts.setProfileName(name);}async setProfileStatus(status: string): Promise<void> {- this.ensureReady();- await this.sock!.updateProfileStatus(status);+ return this.contacts.setProfileStatus(status);}async setProfilePicture(media: MediaInput): Promise<void> {- this.ensureReady();- const selfJid = this.normalizedSelfJid();- if (!selfJid) {- throw new Error('cannot set the profile picture: the own JID is not known yet');- }- // updateProfilePicture takes a WAMediaUpload; resolveMediaBuffer covers Buffer | base64 | URL,- // the same conversion the media sends use.- const { data } = await resolveMediaBuffer(media);- await this.sock!.updateProfilePicture(selfJid, data);+ return this.contacts.setProfilePicture(media);}// ----- Contacts & chats ------ // eslint-disable-next-line @typescript-eslint/require-awaitasync getContacts(): Promise<Contact[]> {- this.ensureReady();- return this.sessionStore.listContacts();+ return this.contacts.getContacts();}- // eslint-disable-next-line @typescript-eslint/require-awaitasync getContactById(contactId: string): Promise<Contact | null> {- this.ensureReady();- return this.sessionStore.findContact(contactId);+ return this.contacts.getContactById(contactId);}- // eslint-disable-next-line @typescript-eslint/require-awaitasync resolveContactPhone(contactId: string): Promise<string | null> {- this.ensureReady();- return this.sessionStore.resolvePhone(contactId);+ return this.contacts.resolveContactPhone(contactId);}- // eslint-disable-next-line @typescript-eslint/require-awaitasync getChats(): Promise<ChatSummary[]> {- this.ensureReady();- return this.sessionStore.listChats();+ return this.contacts.getChats();}async sendSeen(chatId: string): Promise<boolean> {- this.ensureReady();- const last = this.sessionStore.lastMessage(chatId);- if (!last) {- return false; // nothing known to mark read- }- await this.sock!.readMessages([last.key]);- return true;+ return this.contacts.sendSeen(chatId);}async markUnread(chatId: string): Promise<boolean> {- this.ensureReady();- const last = this.sessionStore.lastMessage(chatId);- if (!last) {- return false; // Baileys' unread toggle needs the last message; can't synthesize it- }- await this.sock!.chatModify(- { markRead: false, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },- chatId,- );- return true;+ return this.contacts.markUnread(chatId);}async deleteChat(chatId: string): Promise<boolean> {- this.ensureReady();- const last = this.sessionStore.lastMessage(chatId);- if (!last) {- return false; // Baileys' delete needs the last message; can't synthesize it- }- await this.sock!.chatModify(- { delete: true, lastMessages: [{ key: last.key, messageTimestamp: last.timestamp }] },- chatId,- );- return true;+ return this.contacts.deleteChat(chatId);}// ----- Gated: not supported by this minimal slice (no store) -----