patch messages.update

This commit is contained in:
Adhiraj Singh
2021-07-18 21:41:50 +05:30
parent 6751b5cc73
commit 8a5e6489c0
6 changed files with 26 additions and 17 deletions

View File

@@ -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<WAMessage>[] = (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)

View File

@@ -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')
}

View File

@@ -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

View File

@@ -46,10 +46,12 @@ const makeOrderedDictionary = function<T>(idGetter: (item: T) => string) {
upsert,
update,
remove,
updateAssign: (update: Partial<T>) => {
const item = get(idGetter(update as any))
updateAssign: (id: string, update: Partial<T>) => {
const item = get(id)
if(item) {
Object.assign(item, update)
delete dict[id]
dict[id] = item
return true
}
return false

View File

@@ -147,5 +147,6 @@ export interface MessageStatusUpdate {
type: WAMessageStatus
}
export type WAMessageUpdate = { update: Partial<WAMessage>, key: proto.IMessageKey }
export type WAMessageCursor = { before: WAMessageKey | undefined } | { after: WAMessageKey | undefined }

View File

@@ -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<Contact>[]
'messages.delete': { jid: string, ids: string[] } | { jid: string, all: true }
'messages.update': Partial<WAMessage>[]
'messages.update': WAMessageUpdate[]
'messages.upsert': { messages: WAMessage[], type: MessageUpdateType }
'groups.update': Partial<GroupMetadata>[]