diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index ca14a72..b4ae750 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -28,6 +28,7 @@ export const DEFAULT_CONNECTION_CONFIG: SocketConfig = { keepAliveIntervalMs: 25_000, logger: P().child({ class: 'baileys' }), printQRInTerminal: false, + emitOwnEvents: true } export const MEDIA_PATH_MAP: { [T in MediaType]: string } = { diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index fa4a320..28fb5cd 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -298,7 +298,7 @@ export const makeChatsSocket = (config: SocketConfig) => { } const processSyncActions = (actions: ChatMutation[]) => { - + const updates: { [jid: string]: Partial } = {} const contactUpdates: { [jid: string]: Partial } = {} const msgDeletes: proto.IMessageKey[] = [] @@ -360,7 +360,7 @@ export const makeChatsSocket = (config: SocketConfig) => { ) const initial = await authState.keys.getAppStateSyncVersion(name) // temp: verify it was encoded correctly - await decodePatches({ syncds: [{ ...patch, version: { version: state.version }, }], name }, initial, authState) + const result = await decodePatches({ syncds: [{ ...patch, version: { version: state.version }, }], name }, initial, authState) const node: BinaryNode = { tag: 'iq', @@ -397,6 +397,9 @@ export const makeChatsSocket = (config: SocketConfig) => { await authState.keys.setAppStateSyncVersion(name, state) ev.emit('auth-state.update', authState) + if(config.emitOwnEvents) { + processSyncActions(result.newMutations) + } } const chatModify = (mod: ChatModification, jid: string, lastMessages: Pick[]) => { diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index 392db09..1ce0cc5 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -394,9 +394,11 @@ export const makeMessagesSocket = (config: SocketConfig) => { } ) await relayMessage(jid, fullMsg.message, { messageId: fullMsg.key.id! }) - process.nextTick(() => { - ev.emit('messages.upsert', { messages: [fullMsg], type: 'append' }) - }) + if(config.emitOwnEvents) { + process.nextTick(() => { + ev.emit('messages.upsert', { messages: [fullMsg], type: 'append' }) + }) + } return fullMsg } } diff --git a/src/Types/index.ts b/src/Types/index.ts index 351bb6a..470dcc7 100644 --- a/src/Types/index.ts +++ b/src/Types/index.ts @@ -43,6 +43,8 @@ export type SocketConfig = { fetchAgent?: Agent /** should the QR be printed in the terminal */ printQRInTerminal: boolean + /** should events be emitted for actions done by this socket connection */ + emitOwnEvents: boolean } export enum DisconnectReason {