mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
patch messages.update
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import BinaryNode from "../BinaryNode";
|
import BinaryNode from "../BinaryNode";
|
||||||
import { EventEmitter } from 'events'
|
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 { debouncedTimeout, unixTimestampSeconds, whatsappID } from "../Utils/generics";
|
||||||
import makeAuthSocket from "./auth";
|
import makeAuthSocket from "./auth";
|
||||||
import { Attributes, BinaryNode as BinaryNodeBase } from "../BinaryNode/types";
|
import { Attributes, BinaryNode as BinaryNodeBase } from "../BinaryNode/types";
|
||||||
@@ -67,14 +67,14 @@ const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
case 'star':
|
case 'star':
|
||||||
case 'unstar':
|
case 'unstar':
|
||||||
const starred = updateType === 'star'
|
const starred = updateType === 'star'
|
||||||
const updates: Partial<WAMessage>[] = (node.data as BinaryNode[]).map(
|
const updates: WAMessageUpdate[] = (node.data as BinaryNode[]).map(
|
||||||
({ attributes }) => ({
|
({ attributes }) => ({
|
||||||
key: {
|
key: {
|
||||||
remoteJid: jid,
|
remoteJid: jid,
|
||||||
id: attributes.index,
|
id: attributes.index,
|
||||||
fromMe: attributes.owner === 'true'
|
fromMe: attributes.owner === 'true'
|
||||||
},
|
},
|
||||||
starred
|
update: { starred }
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
ev.emit('messages.update', updates)
|
ev.emit('messages.update', updates)
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
})
|
})
|
||||||
Object.keys(response[1]).forEach (key => content[key] = response[1][key]) // update message
|
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
|
return response
|
||||||
}
|
}
|
||||||
@@ -130,7 +130,13 @@ const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
case WAMessageProto.ProtocolMessage.ProtocolMessageType.REVOKE:
|
case WAMessageProto.ProtocolMessage.ProtocolMessageType.REVOKE:
|
||||||
const key = protocolMessage.key
|
const key = protocolMessage.key
|
||||||
const messageStubType = WAMessageStubType.REVOKE
|
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
|
return
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
@@ -192,7 +198,7 @@ const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
ev.emit('chats.update', [chatUpdate])
|
ev.emit('chats.update', [chatUpdate])
|
||||||
}
|
}
|
||||||
if(type === 'update') {
|
if(type === 'update') {
|
||||||
ev.emit('messages.update', [message])
|
ev.emit('messages.update', [ { update: message, key: message.key } ])
|
||||||
} else {
|
} else {
|
||||||
ev.emit('messages.upsert', { messages: [message], type })
|
ev.emit('messages.upsert', { messages: [message], type })
|
||||||
}
|
}
|
||||||
@@ -242,7 +248,7 @@ const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
} else {
|
} else {
|
||||||
const emitUpdate = (status: WAMessageStatus) => {
|
const emitUpdate = (status: WAMessageStatus) => {
|
||||||
message.status = status
|
message.status = status
|
||||||
ev.emit('messages.update', [ { key: message.key, status } ])
|
ev.emit('messages.update', [ { key: message.key, update: { status } } ])
|
||||||
}
|
}
|
||||||
promise
|
promise
|
||||||
.then(() => emitUpdate(finalState))
|
.then(() => emitUpdate(finalState))
|
||||||
@@ -295,7 +301,7 @@ const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
const status = STATUS_MAP[json.type]
|
const status = STATUS_MAP[json.type]
|
||||||
|
|
||||||
if(status) {
|
if(status) {
|
||||||
ev.emit('messages.update', [ { key, status } ])
|
ev.emit('messages.update', [ { key, update: { status } } ])
|
||||||
} else {
|
} else {
|
||||||
logger.warn({ data }, 'got unknown status update for message')
|
logger.warn({ data }, 'got unknown status update for message')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -131,9 +131,9 @@ export default(
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
ev.on('messages.update', updates => {
|
ev.on('messages.update', updates => {
|
||||||
for(const update of updates) {
|
for(const { update, key } of updates) {
|
||||||
const list = assertMessageList(update.key.remoteJid)
|
const list = assertMessageList(key.remoteJid)
|
||||||
const result = list.updateAssign(update)
|
const result = list.updateAssign(key.id, update)
|
||||||
if(!result) {
|
if(!result) {
|
||||||
logger.debug({ update }, `got update for non-existent message`)
|
logger.debug({ update }, `got update for non-existent message`)
|
||||||
}
|
}
|
||||||
@@ -201,8 +201,8 @@ export default(
|
|||||||
const cursorValue = cursorKey ? list.get(cursorKey.id) : undefined
|
const cursorValue = cursorKey ? list.get(cursorKey.id) : undefined
|
||||||
|
|
||||||
let messages: WAMessage[]
|
let messages: WAMessage[]
|
||||||
if(messages && mode ==='before' && (!cursorKey || cursorValue)) {
|
if(list && mode === 'before' && (!cursorKey || cursorValue)) {
|
||||||
const msgIdx = messages.findIndex(m => m.key.id === cursorKey.id)
|
const msgIdx = list.array.findIndex(m => m.key.id === cursorKey.id)
|
||||||
messages = list.array.slice(0, msgIdx)
|
messages = list.array.slice(0, msgIdx)
|
||||||
|
|
||||||
const diff = count - messages.length
|
const diff = count - messages.length
|
||||||
|
|||||||
@@ -46,10 +46,12 @@ const makeOrderedDictionary = function<T>(idGetter: (item: T) => string) {
|
|||||||
upsert,
|
upsert,
|
||||||
update,
|
update,
|
||||||
remove,
|
remove,
|
||||||
updateAssign: (update: Partial<T>) => {
|
updateAssign: (id: string, update: Partial<T>) => {
|
||||||
const item = get(idGetter(update as any))
|
const item = get(id)
|
||||||
if(item) {
|
if(item) {
|
||||||
Object.assign(item, update)
|
Object.assign(item, update)
|
||||||
|
delete dict[id]
|
||||||
|
dict[id] = item
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -147,5 +147,6 @@ export interface MessageStatusUpdate {
|
|||||||
type: WAMessageStatus
|
type: WAMessageStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type WAMessageUpdate = { update: Partial<WAMessage>, key: proto.IMessageKey }
|
||||||
|
|
||||||
export type WAMessageCursor = { before: WAMessageKey | undefined } | { after: WAMessageKey | undefined }
|
export type WAMessageCursor = { before: WAMessageKey | undefined } | { after: WAMessageKey | undefined }
|
||||||
@@ -16,7 +16,7 @@ import { Contact } from './Contact'
|
|||||||
import { ConnectionState } from './Store'
|
import { ConnectionState } from './Store'
|
||||||
|
|
||||||
import { GroupMetadata, ParticipantAction } from './GroupMetadata'
|
import { GroupMetadata, ParticipantAction } from './GroupMetadata'
|
||||||
import { MessageUpdateType, WAMessage } from './Message'
|
import { MessageUpdateType, WAMessage, WAMessageKey, WAMessageUpdate } from './Message'
|
||||||
|
|
||||||
/** used for binary messages */
|
/** used for binary messages */
|
||||||
export enum WAMetric {
|
export enum WAMetric {
|
||||||
@@ -180,7 +180,7 @@ export type BaileysEventMap = {
|
|||||||
'contacts.update': Partial<Contact>[]
|
'contacts.update': Partial<Contact>[]
|
||||||
|
|
||||||
'messages.delete': { jid: string, ids: string[] } | { jid: string, all: true }
|
'messages.delete': { jid: string, ids: string[] } | { jid: string, all: true }
|
||||||
'messages.update': Partial<WAMessage>[]
|
'messages.update': WAMessageUpdate[]
|
||||||
'messages.upsert': { messages: WAMessage[], type: MessageUpdateType }
|
'messages.upsert': { messages: WAMessage[], type: MessageUpdateType }
|
||||||
|
|
||||||
'groups.update': Partial<GroupMetadata>[]
|
'groups.update': Partial<GroupMetadata>[]
|
||||||
|
|||||||
Reference in New Issue
Block a user