feat(worker): WhatsAppSessionPool + group-sync accepts accountId
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { WASocket } from '@whiskeysockets/baileys';
|
||||
import { NormalizedMessage, NormalizedReaction } from '@tower/types';
|
||||
import { createWhatsAppSession } from './session';
|
||||
import { createLogger } from '@tower/logger';
|
||||
|
||||
const logger = createLogger('session-pool');
|
||||
|
||||
// Callbacks the pool exposes — accountId injected by pool, not caller
|
||||
export type PoolMessageCallback = (msg: NormalizedMessage, accountId: string) => Promise<void> | void;
|
||||
export type PoolReactionCallback = (reaction: NormalizedReaction, accountId: string) => Promise<void> | void;
|
||||
// groups typed as `any` to avoid leaking GroupMetadata (Baileys type) into main.ts
|
||||
export type PoolGroupsCallback = (groups: any, accountId: string) => Promise<void> | void;
|
||||
|
||||
export class WhatsAppSessionPool {
|
||||
private sessions = new Map<string, WASocket>();
|
||||
|
||||
async add(
|
||||
accountId: string,
|
||||
sessionPath: string,
|
||||
onMessage: PoolMessageCallback,
|
||||
onReaction: PoolReactionCallback,
|
||||
onGroups: PoolGroupsCallback,
|
||||
): Promise<void> {
|
||||
logger.info({ accountId }, 'Starting session');
|
||||
const sock = await createWhatsAppSession(
|
||||
accountId,
|
||||
sessionPath,
|
||||
(msg) => onMessage(msg, accountId),
|
||||
(reaction) => onReaction(reaction, accountId),
|
||||
(groups) => onGroups(groups, accountId),
|
||||
);
|
||||
this.sessions.set(accountId, sock);
|
||||
}
|
||||
|
||||
get(accountId: string): WASocket | undefined {
|
||||
return this.sessions.get(accountId);
|
||||
}
|
||||
|
||||
getAll(): Map<string, WASocket> {
|
||||
return this.sessions;
|
||||
}
|
||||
|
||||
async sendMessage(accountId: string, groupJid: string, text: string): Promise<void> {
|
||||
const sock = this.sessions.get(accountId);
|
||||
if (!sock) throw new Error(`No active session for account ${accountId}`);
|
||||
await sock.sendMessage(groupJid, { text });
|
||||
}
|
||||
|
||||
async remove(accountId: string): Promise<void> {
|
||||
const sock = this.sessions.get(accountId);
|
||||
if (sock) {
|
||||
await sock.logout().catch(() => {});
|
||||
this.sessions.delete(accountId);
|
||||
logger.info({ accountId }, 'Session removed');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user