mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
refactor: use the proto UserReceipt instead of custom MessageInfo
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Boom } from '@hapi/boom'
|
import { Boom } from '@hapi/boom'
|
||||||
import { proto } from '../../WAProto'
|
import { proto } from '../../WAProto'
|
||||||
import { WA_DEFAULT_EPHEMERAL } from '../Defaults'
|
import { WA_DEFAULT_EPHEMERAL } from '../Defaults'
|
||||||
import { AnyMessageContent, Chat, GroupMetadata, LegacySocketConfig, MediaConnInfo, MessageInfo, MessageInfoUpdate, MessageUpdateType, MiscMessageGenerationOptions, ParticipantAction, WAFlag, WAMessage, WAMessageCursor, WAMessageKey, WAMessageStatus, WAMessageStubType, WAMessageUpdate, WAMetric, WAUrlInfo } from '../Types'
|
import { AnyMessageContent, Chat, GroupMetadata, LegacySocketConfig, MediaConnInfo, MessageUpdateType, MessageUserReceipt, MessageUserReceiptUpdate, MiscMessageGenerationOptions, ParticipantAction, WAFlag, WAMessage, WAMessageCursor, WAMessageKey, WAMessageStatus, WAMessageStubType, WAMessageUpdate, WAMetric, WAUrlInfo } from '../Types'
|
||||||
import { decryptMediaMessageBuffer, extractMessageContent, generateWAMessage, getWAUploadToServer, toNumber } from '../Utils'
|
import { decryptMediaMessageBuffer, extractMessageContent, generateWAMessage, getWAUploadToServer, toNumber } from '../Utils'
|
||||||
import { areJidsSameUser, BinaryNode, getBinaryNodeMessages, isJidGroup, jidNormalizedUser } from '../WABinary'
|
import { areJidsSameUser, BinaryNode, getBinaryNodeMessages, isJidGroup, jidNormalizedUser } from '../WABinary'
|
||||||
import makeChatsSocket from './chats'
|
import makeChatsSocket from './chats'
|
||||||
@@ -353,13 +353,16 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
|
|||||||
ids = [ids]
|
ids = [ids]
|
||||||
}
|
}
|
||||||
|
|
||||||
let updateKey: keyof MessageInfoUpdate['update']
|
let updateKey: keyof MessageUserReceipt
|
||||||
switch (attributes.ack.toString()) {
|
switch (attributes.ack.toString()) {
|
||||||
case '2':
|
case '2':
|
||||||
updateKey = 'deliveries'
|
updateKey = 'receiptTimestamp'
|
||||||
break
|
break
|
||||||
case '3':
|
case '3':
|
||||||
updateKey = 'reads'
|
updateKey = 'readTimestamp'
|
||||||
|
break
|
||||||
|
case '4':
|
||||||
|
updateKey = 'playedTimestamp'
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
logger.warn({ attributes }, 'received unknown message info update')
|
logger.warn({ attributes }, 'received unknown message info update')
|
||||||
@@ -370,13 +373,17 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
|
|||||||
remoteJid: jidNormalizedUser(attributes.to),
|
remoteJid: jidNormalizedUser(attributes.to),
|
||||||
fromMe: areJidsSameUser(attributes.from, state.legacy?.user?.id || ''),
|
fromMe: areJidsSameUser(attributes.from, state.legacy?.user?.id || ''),
|
||||||
}
|
}
|
||||||
const updates = ids.map<MessageInfoUpdate>(id => ({
|
|
||||||
|
const userJid = jidNormalizedUser(attributes.participant || attributes.to)
|
||||||
|
|
||||||
|
const updates = ids.map<MessageUserReceiptUpdate>(id => ({
|
||||||
key: { ...keyPartial, id },
|
key: { ...keyPartial, id },
|
||||||
update: {
|
receipt: {
|
||||||
[updateKey]: { [jidNormalizedUser(attributes.participant || attributes.to)]: new Date(+attributes.t) }
|
userJid,
|
||||||
|
[updateKey]: +attributes.t
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
ev.emit('message-info.update', updates)
|
ev.emit('message-receipt.update', updates)
|
||||||
// for individual messages
|
// for individual messages
|
||||||
// it means the message is marked read/delivered
|
// it means the message is marked read/delivered
|
||||||
if(!isJidGroup(keyPartial.remoteJid)) {
|
if(!isJidGroup(keyPartial.remoteJid)) {
|
||||||
@@ -384,7 +391,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
|
|||||||
{
|
{
|
||||||
key: { ...keyPartial, id },
|
key: { ...keyPartial, id },
|
||||||
update: {
|
update: {
|
||||||
status: updateKey === 'deliveries' ? WAMessageStatus.DELIVERY_ACK : WAMessageStatus.READ
|
status: updateKey === 'receiptTimestamp' ? WAMessageStatus.DELIVERY_ACK : WAMessageStatus.READ
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)))
|
)))
|
||||||
@@ -416,24 +423,28 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
|
|||||||
expect200: true,
|
expect200: true,
|
||||||
requiresPhoneConnection: true
|
requiresPhoneConnection: true
|
||||||
})
|
})
|
||||||
const info: MessageInfo = { reads: {}, deliveries: {} }
|
const info: { [jid: string]: MessageUserReceipt } = { }
|
||||||
if(Array.isArray(content)) {
|
if(Array.isArray(content)) {
|
||||||
for(const { tag, content: innerData } of content) {
|
for(const { tag, content: innerData } of content) {
|
||||||
const [{ attrs }] = (innerData as BinaryNode[])
|
const [{ attrs }] = (innerData as BinaryNode[])
|
||||||
|
|
||||||
const jid = jidNormalizedUser(attrs.jid)
|
const jid = jidNormalizedUser(attrs.jid)
|
||||||
const date = new Date(+attrs.t * 1000)
|
const recp = info[jid] || { userJid: jid }
|
||||||
|
const date = +attrs.t
|
||||||
switch (tag) {
|
switch (tag) {
|
||||||
case 'read':
|
case 'read':
|
||||||
info.reads[jid] = date
|
recp.readTimestamp = date
|
||||||
break
|
break
|
||||||
case 'delivery':
|
case 'delivery':
|
||||||
info.deliveries[jid] = date
|
recp.receiptTimestamp = date
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
info[jid] = recp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return info
|
return Object.values(info)
|
||||||
},
|
},
|
||||||
downloadMediaMessage: async(message: WAMessage, type: 'buffer' | 'stream' = 'buffer') => {
|
downloadMediaMessage: async(message: WAMessage, type: 'buffer' | 'stream' = 'buffer') => {
|
||||||
const downloadMediaMessage = async() => {
|
const downloadMediaMessage = async() => {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { proto } from '../../WAProto'
|
|||||||
import { DEFAULT_CONNECTION_CONFIG } from '../Defaults'
|
import { DEFAULT_CONNECTION_CONFIG } from '../Defaults'
|
||||||
import type makeLegacySocket from '../LegacySocket'
|
import type makeLegacySocket from '../LegacySocket'
|
||||||
import type makeMDSocket from '../Socket'
|
import type makeMDSocket from '../Socket'
|
||||||
import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata, MessageInfo, PresenceData, WAMessage, WAMessageCursor, WAMessageKey } from '../Types'
|
import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata, PresenceData, WAMessage, WAMessageCursor, WAMessageKey } from '../Types'
|
||||||
import { toNumber } from '../Utils'
|
import { toNumber } from '../Utils'
|
||||||
import { jidNormalizedUser } from '../WABinary'
|
import { jidNormalizedUser } from '../WABinary'
|
||||||
import makeOrderedDictionary from './make-ordered-dictionary'
|
import makeOrderedDictionary from './make-ordered-dictionary'
|
||||||
@@ -38,7 +38,6 @@ export default (
|
|||||||
const messages: { [_: string]: ReturnType<typeof makeMessagesDictionary> } = { }
|
const messages: { [_: string]: ReturnType<typeof makeMessagesDictionary> } = { }
|
||||||
const contacts: { [_: string]: Contact } = { }
|
const contacts: { [_: string]: Contact } = { }
|
||||||
const groupMetadata: { [_: string]: GroupMetadata } = { }
|
const groupMetadata: { [_: string]: GroupMetadata } = { }
|
||||||
const messageInfos: { [id: string]: MessageInfo } = { }
|
|
||||||
const presences: { [id: string]: { [participant: string]: PresenceData } } = { }
|
const presences: { [id: string]: { [participant: string]: PresenceData } } = { }
|
||||||
const state: ConnectionState = { connection: 'close' }
|
const state: ConnectionState = { connection: 'close' }
|
||||||
|
|
||||||
@@ -159,9 +158,6 @@ export default (
|
|||||||
}
|
}
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
// add message infos if required
|
|
||||||
messageInfos[msg.key.id!] = messageInfos[msg.key.id!] || { reads: {}, deliveries: {} }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,13 +220,17 @@ export default (
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ev.on('message-info.update', updates => {
|
ev.on('message-receipt.update', updates => {
|
||||||
for(const { key, update } of updates) {
|
for(const { key, receipt } of updates) {
|
||||||
const obj = messageInfos[key.id!]
|
const obj = messages[key.remoteJid!]
|
||||||
if(obj) {
|
const msg = obj?.get(key.id)
|
||||||
// add reads/deliveries
|
if(msg) {
|
||||||
for(const key in update) {
|
msg.userReceipt = msg.userReceipt || []
|
||||||
Object.assign(obj[key], update[key])
|
const recp = msg.userReceipt.find(m => m.userJid === receipt.userJid)
|
||||||
|
if(recp) {
|
||||||
|
Object.assign(recp, receipt)
|
||||||
|
} else {
|
||||||
|
msg.userReceipt.push(receipt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -260,7 +260,6 @@ export default (
|
|||||||
contacts,
|
contacts,
|
||||||
messages,
|
messages,
|
||||||
groupMetadata,
|
groupMetadata,
|
||||||
messageInfos,
|
|
||||||
state,
|
state,
|
||||||
presences,
|
presences,
|
||||||
bind,
|
bind,
|
||||||
@@ -348,12 +347,18 @@ export default (
|
|||||||
|
|
||||||
return groupMetadata[jid]
|
return groupMetadata[jid]
|
||||||
},
|
},
|
||||||
fetchMessageInfo: async({ remoteJid, id }: WAMessageKey, sock: LegacyWASocket | undefined) => {
|
fetchMessageReceipts: async({ remoteJid, id }: WAMessageKey, sock: LegacyWASocket | undefined) => {
|
||||||
if(!messageInfos[id!]) {
|
const list = messages[remoteJid]
|
||||||
messageInfos[id!] = await sock?.messageInfo(remoteJid, id)
|
const msg = list?.get(id)
|
||||||
|
let receipts = msg.userReceipt
|
||||||
|
if(!receipts) {
|
||||||
|
receipts = await sock?.messageInfo(remoteJid, id)
|
||||||
|
if(msg) {
|
||||||
|
msg.userReceipt = receipts
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return messageInfos[id!]
|
return receipts
|
||||||
},
|
},
|
||||||
toJSON,
|
toJSON,
|
||||||
fromJSON,
|
fromJSON,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { AuthenticationCreds } from './Auth'
|
|||||||
import { Chat, PresenceData } from './Chat'
|
import { Chat, PresenceData } from './Chat'
|
||||||
import { Contact } from './Contact'
|
import { Contact } from './Contact'
|
||||||
import { GroupMetadata, ParticipantAction } from './GroupMetadata'
|
import { GroupMetadata, ParticipantAction } from './GroupMetadata'
|
||||||
import { MessageInfoUpdate, MessageUpdateType, WAMessage, WAMessageKey, WAMessageUpdate } from './Message'
|
import { MessageUpdateType, MessageUserReceiptUpdate, WAMessage, WAMessageKey, WAMessageUpdate } from './Message'
|
||||||
import { ConnectionState } from './State'
|
import { ConnectionState } from './State'
|
||||||
|
|
||||||
export type BaileysEventMap<T> = {
|
export type BaileysEventMap<T> = {
|
||||||
@@ -37,7 +37,7 @@ export type BaileysEventMap<T> = {
|
|||||||
* */
|
* */
|
||||||
'messages.upsert': { messages: WAMessage[], type: MessageUpdateType }
|
'messages.upsert': { messages: WAMessage[], type: MessageUpdateType }
|
||||||
|
|
||||||
'message-info.update': MessageInfoUpdate[]
|
'message-receipt.update': MessageUserReceiptUpdate[]
|
||||||
|
|
||||||
'groups.upsert': GroupMetadata[]
|
'groups.upsert': GroupMetadata[]
|
||||||
'groups.update': Partial<GroupMetadata>[]
|
'groups.update': Partial<GroupMetadata>[]
|
||||||
|
|||||||
@@ -166,14 +166,10 @@ export type MessageGenerationOptions = MessageContentGenerationOptions & Message
|
|||||||
|
|
||||||
export type MessageUpdateType = 'append' | 'notify' | 'replace'
|
export type MessageUpdateType = 'append' | 'notify' | 'replace'
|
||||||
|
|
||||||
export type MessageInfoEventMap = { [jid: string]: Date }
|
export type MessageUserReceipt = proto.IUserReceipt
|
||||||
export interface MessageInfo {
|
|
||||||
reads: MessageInfoEventMap
|
|
||||||
deliveries: MessageInfoEventMap
|
|
||||||
}
|
|
||||||
|
|
||||||
export type WAMessageUpdate = { update: Partial<WAMessage>, key: proto.IMessageKey }
|
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 }
|
||||||
|
|
||||||
export type MessageInfoUpdate = { key: proto.IMessageKey, update: Partial<MessageInfo> }
|
export type MessageUserReceiptUpdate = { key: proto.IMessageKey, receipt: MessageUserReceipt }
|
||||||
|
|||||||
Reference in New Issue
Block a user