diff --git a/src/Connection/chats.ts b/src/Connection/chats.ts index ed4a1a2..1394c90 100644 --- a/src/Connection/chats.ts +++ b/src/Connection/chats.ts @@ -1,6 +1,6 @@ import BinaryNode from "../BinaryNode"; import { EventEmitter } from 'events' -import { Chat, Contact, Presence, PresenceData, SocketConfig, WAFlag, WAMetric, WABusinessProfile, ChatModification, WAMessageKey, WAMessage } from "../Types"; +import { Chat, Contact, Presence, PresenceData, SocketConfig, WAFlag, WAMetric, WABusinessProfile, ChatModification, WAMessageKey, WAMessage, WAMessageUpdate } from "../Types"; import { debouncedTimeout, unixTimestampSeconds, whatsappID } from "../Utils/generics"; import makeAuthSocket from "./auth"; import { Attributes, BinaryNode as BinaryNodeBase } from "../BinaryNode/types"; @@ -67,14 +67,14 @@ const makeChatsSocket = (config: SocketConfig) => { case 'star': case 'unstar': const starred = updateType === 'star' - const updates: Partial[] = (node.data as BinaryNode[]).map( + const updates: WAMessageUpdate[] = (node.data as BinaryNode[]).map( ({ attributes }) => ({ key: { remoteJid: jid, id: attributes.index, fromMe: attributes.owner === 'true' }, - starred + update: { starred } }) ) ev.emit('messages.update', updates) diff --git a/src/Connection/messages.ts b/src/Connection/messages.ts index a39bd0d..3c00471 100644 --- a/src/Connection/messages.ts +++ b/src/Connection/messages.ts @@ -92,7 +92,7 @@ const makeMessagesSocket = (config: SocketConfig) => { }) Object.keys(response[1]).forEach (key => content[key] = response[1][key]) // update message - ev.emit('messages.update', [{ key: message.key, message: message.message }]) + ev.emit('messages.update', [{ key: message.key, update: { message: message.message } }]) return response } @@ -130,7 +130,13 @@ const makeMessagesSocket = (config: SocketConfig) => { case WAMessageProto.ProtocolMessage.ProtocolMessageType.REVOKE: const key = protocolMessage.key const messageStubType = WAMessageStubType.REVOKE - ev.emit('messages.update', [ { message: null, key, messageStubType } ]) + ev.emit('messages.update', [ + { + // the key of the deleted message is updated + update: { message: null, key: message.key, messageStubType }, + key + } + ]) return default: break @@ -192,7 +198,7 @@ const makeMessagesSocket = (config: SocketConfig) => { ev.emit('chats.update', [chatUpdate]) } if(type === 'update') { - ev.emit('messages.update', [message]) + ev.emit('messages.update', [ { update: message, key: message.key } ]) } else { ev.emit('messages.upsert', { messages: [message], type }) } @@ -242,7 +248,7 @@ const makeMessagesSocket = (config: SocketConfig) => { } else { const emitUpdate = (status: WAMessageStatus) => { message.status = status - ev.emit('messages.update', [ { key: message.key, status } ]) + ev.emit('messages.update', [ { key: message.key, update: { status } } ]) } promise .then(() => emitUpdate(finalState)) @@ -295,7 +301,7 @@ const makeMessagesSocket = (config: SocketConfig) => { const status = STATUS_MAP[json.type] if(status) { - ev.emit('messages.update', [ { key, status } ]) + ev.emit('messages.update', [ { key, update: { status } } ]) } else { logger.warn({ data }, 'got unknown status update for message') } diff --git a/src/Store/in-memory-store.ts b/src/Store/in-memory-store.ts index d11ecd8..af06358 100644 --- a/src/Store/in-memory-store.ts +++ b/src/Store/in-memory-store.ts @@ -131,9 +131,9 @@ export default( } }) ev.on('messages.update', updates => { - for(const update of updates) { - const list = assertMessageList(update.key.remoteJid) - const result = list.updateAssign(update) + for(const { update, key } of updates) { + const list = assertMessageList(key.remoteJid) + const result = list.updateAssign(key.id, update) if(!result) { logger.debug({ update }, `got update for non-existent message`) } @@ -201,8 +201,8 @@ export default( const cursorValue = cursorKey ? list.get(cursorKey.id) : undefined let messages: WAMessage[] - if(messages && mode ==='before' && (!cursorKey || cursorValue)) { - const msgIdx = messages.findIndex(m => m.key.id === cursorKey.id) + if(list && mode === 'before' && (!cursorKey || cursorValue)) { + const msgIdx = list.array.findIndex(m => m.key.id === cursorKey.id) messages = list.array.slice(0, msgIdx) const diff = count - messages.length diff --git a/src/Store/ordered-dictionary.ts b/src/Store/ordered-dictionary.ts index d1ce2ad..fb6cd7e 100644 --- a/src/Store/ordered-dictionary.ts +++ b/src/Store/ordered-dictionary.ts @@ -46,10 +46,12 @@ const makeOrderedDictionary = function(idGetter: (item: T) => string) { upsert, update, remove, - updateAssign: (update: Partial) => { - const item = get(idGetter(update as any)) + updateAssign: (id: string, update: Partial) => { + const item = get(id) if(item) { Object.assign(item, update) + delete dict[id] + dict[id] = item return true } return false diff --git a/src/Types/Message.ts b/src/Types/Message.ts index 0c7e52d..43905f6 100644 --- a/src/Types/Message.ts +++ b/src/Types/Message.ts @@ -147,5 +147,6 @@ export interface MessageStatusUpdate { type: WAMessageStatus } +export type WAMessageUpdate = { update: Partial, key: proto.IMessageKey } export type WAMessageCursor = { before: WAMessageKey | undefined } | { after: WAMessageKey | undefined } \ No newline at end of file diff --git a/src/Types/index.ts b/src/Types/index.ts index 7091a1c..ab1f493 100644 --- a/src/Types/index.ts +++ b/src/Types/index.ts @@ -16,7 +16,7 @@ import { Contact } from './Contact' import { ConnectionState } from './Store' import { GroupMetadata, ParticipantAction } from './GroupMetadata' -import { MessageUpdateType, WAMessage } from './Message' +import { MessageUpdateType, WAMessage, WAMessageKey, WAMessageUpdate } from './Message' /** used for binary messages */ export enum WAMetric { @@ -180,7 +180,7 @@ export type BaileysEventMap = { 'contacts.update': Partial[] 'messages.delete': { jid: string, ids: string[] } | { jid: string, all: true } - 'messages.update': Partial[] + 'messages.update': WAMessageUpdate[] 'messages.upsert': { messages: WAMessage[], type: MessageUpdateType } 'groups.update': Partial[]