mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
update proto + message info
This commit is contained in:
11489
WAMessage/WAMessage.d.ts
vendored
11489
WAMessage/WAMessage.d.ts
vendored
File diff suppressed because it is too large
Load Diff
35199
WAMessage/WAMessage.js
35199
WAMessage/WAMessage.js
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,11 @@
|
|||||||
import BinaryNode from "../BinaryNode";
|
import BinaryNode from "../BinaryNode";
|
||||||
import { Boom } from '@hapi/boom'
|
import { Boom } from '@hapi/boom'
|
||||||
import { EventEmitter } from 'events'
|
import { EventEmitter } from 'events'
|
||||||
import { Chat, Presence, WAMessageCursor, SocketConfig, WAMessage, WAMessageKey, ParticipantAction, WAMessageProto, WAMessageStatus, WAMessageStubType, GroupMetadata, AnyMessageContent, MiscMessageGenerationOptions, WAFlag, WAMetric, WAUrlInfo, MediaConnInfo, MessageUpdateType, MessageInfo } from "../Types";
|
import { Chat, Presence, WAMessageCursor, SocketConfig, WAMessage, WAMessageKey, ParticipantAction, WAMessageProto, WAMessageStatus, WAMessageStubType, GroupMetadata, AnyMessageContent, MiscMessageGenerationOptions, WAFlag, WAMetric, WAUrlInfo, MediaConnInfo, MessageUpdateType, MessageInfo, MessageInfoUpdate } from "../Types";
|
||||||
import { isGroupID, toNumber, whatsappID, generateWAMessage, decryptMediaMessageBuffer } from "../Utils";
|
import { isGroupID, toNumber, whatsappID, generateWAMessage, decryptMediaMessageBuffer } from "../Utils";
|
||||||
import makeChatsSocket from "./chats";
|
import makeChatsSocket from "./chats";
|
||||||
import { WA_DEFAULT_EPHEMERAL } from "../Defaults";
|
import { WA_DEFAULT_EPHEMERAL } from "../Defaults";
|
||||||
|
import { Attributes } from "../BinaryNode/types";
|
||||||
|
|
||||||
const STATUS_MAP = {
|
const STATUS_MAP = {
|
||||||
read: WAMessageStatus.READ,
|
read: WAMessageStatus.READ,
|
||||||
@@ -308,9 +309,42 @@ const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const onMessageInfoUpdate = ([,attributes]: [string,{[_: string]: any}]) => {
|
||||||
|
let ids = attributes.id as string[] | string
|
||||||
|
if(typeof ids === 'string') {
|
||||||
|
ids = [ids]
|
||||||
|
}
|
||||||
|
let updateKey: keyof MessageInfoUpdate['update']
|
||||||
|
switch(attributes.ack.toString()) {
|
||||||
|
case '2':
|
||||||
|
updateKey = 'deliveries'
|
||||||
|
break
|
||||||
|
case '3':
|
||||||
|
updateKey = 'reads'
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
logger.warn({ attributes }, `received unknown message info update`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const updates = ids.map<MessageInfoUpdate>(id => ({
|
||||||
|
key: {
|
||||||
|
remoteJid: whatsappID(attributes.to),
|
||||||
|
id,
|
||||||
|
fromMe: whatsappID(attributes.from) === getState().user?.jid,
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
[updateKey]: { [whatsappID(attributes.participant)]: new Date(+attributes.t) }
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
ev.emit('message-info.update', updates)
|
||||||
|
}
|
||||||
|
|
||||||
socketEvents.on('CB:action,add:relay,received', onMessageStatusUpdate)
|
socketEvents.on('CB:action,add:relay,received', onMessageStatusUpdate)
|
||||||
socketEvents.on('CB:action,,received', onMessageStatusUpdate)
|
socketEvents.on('CB:action,,received', onMessageStatusUpdate)
|
||||||
|
|
||||||
|
socketEvents.on('CB:Msg', onMessageInfoUpdate)
|
||||||
|
socketEvents.on('CB:MsgInfo', onMessageInfoUpdate)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...sock,
|
...sock,
|
||||||
relayWAMessage,
|
relayWAMessage,
|
||||||
@@ -325,15 +359,18 @@ const makeMessagesSocket = (config: SocketConfig) => {
|
|||||||
expect200: true,
|
expect200: true,
|
||||||
requiresPhoneConnection: true
|
requiresPhoneConnection: true
|
||||||
})
|
})
|
||||||
const info: MessageInfo = {reads: [], deliveries: []}
|
const info: MessageInfo = { reads: {}, deliveries: {} }
|
||||||
if(Array.isArray(data)) {
|
if(Array.isArray(data)) {
|
||||||
for(const { header, attributes } of data) {
|
for(const { header, data: innerData } of data) {
|
||||||
|
const [{ attributes }] = (innerData as BinaryNode[])
|
||||||
|
const jid = whatsappID(attributes.jid)
|
||||||
|
const date = new Date(+attributes.t * 1000)
|
||||||
switch(header) {
|
switch(header) {
|
||||||
case 'read':
|
case 'read':
|
||||||
info.reads.push(attributes as any)
|
info.reads[jid] = date
|
||||||
break
|
break
|
||||||
case 'delivery':
|
case 'delivery':
|
||||||
info.deliveries.push(attributes as any)
|
info.deliveries[jid] = date
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import type KeyedDB from "@adiwajshing/keyed-db"
|
|||||||
import type { Comparable } from "@adiwajshing/keyed-db/lib/Types"
|
import type { Comparable } from "@adiwajshing/keyed-db/lib/Types"
|
||||||
import type { Logger } from "pino"
|
import type { Logger } from "pino"
|
||||||
import type { Connection } from "../Connection"
|
import type { Connection } from "../Connection"
|
||||||
import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata, WAMessage, WAMessageCursor } from "../Types"
|
import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata, MessageInfo, WAMessage, WAMessageCursor, WAMessageKey } from "../Types"
|
||||||
import { toNumber } from "../Utils"
|
import { toNumber } from "../Utils"
|
||||||
import makeOrderedDictionary from "./ordered-dictionary"
|
import makeOrderedDictionary from "./ordered-dictionary"
|
||||||
|
|
||||||
@@ -28,6 +28,7 @@ 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 state: ConnectionState = {
|
const state: ConnectionState = {
|
||||||
connection: 'close',
|
connection: 'close',
|
||||||
phoneConnected: false
|
phoneConnected: false
|
||||||
@@ -101,10 +102,14 @@ export default(
|
|||||||
const list = assertMessageList(jid)
|
const list = assertMessageList(jid)
|
||||||
list.upsert(msg, 'append')
|
list.upsert(msg, 'append')
|
||||||
|
|
||||||
if(type === 'notify' && !chats.get(jid)) {
|
if(type === 'notify') {
|
||||||
ev.emit('chats.upsert', [
|
if(!chats.get(jid)) {
|
||||||
{ jid, t: toNumber(msg.messageTimestamp), count: 1 }
|
ev.emit('chats.upsert', [
|
||||||
])
|
{ jid, t: toNumber(msg.messageTimestamp), count: 1 }
|
||||||
|
])
|
||||||
|
}
|
||||||
|
// add message infos if required
|
||||||
|
messageInfos[msg.key.id!] = messageInfos[msg.key.id!] || { reads: {}, deliveries: {} }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
@@ -181,13 +186,27 @@ export default(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
ev.on('message-info.update', updates => {
|
||||||
|
for(const { key, update } of updates) {
|
||||||
|
const obj = messageInfos[key.id!]
|
||||||
|
if(obj) {
|
||||||
|
// add reads/deliveries
|
||||||
|
for(const key in update) {
|
||||||
|
Object.assign(obj[key], update[key])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
chats,
|
chats,
|
||||||
contacts,
|
contacts,
|
||||||
messages,
|
messages,
|
||||||
groupMetadata,
|
groupMetadata,
|
||||||
|
messageInfos,
|
||||||
state,
|
state,
|
||||||
listen,
|
listen,
|
||||||
loadMessages: async(jid: string, count: number, cursor: WAMessageCursor, sock: Connection | undefined) => {
|
loadMessages: async(jid: string, count: number, cursor: WAMessageCursor, sock: Connection | undefined) => {
|
||||||
@@ -261,6 +280,12 @@ export default(
|
|||||||
groupMetadata[jid] = await sock?.getBroadcastListInfo(jid)
|
groupMetadata[jid] = await sock?.getBroadcastListInfo(jid)
|
||||||
}
|
}
|
||||||
return groupMetadata[jid]
|
return groupMetadata[jid]
|
||||||
|
},
|
||||||
|
fetchMessageInfo: async({remoteJid, id}: WAMessageKey, sock: Connection | undefined) => {
|
||||||
|
if(!messageInfos[id!]) {
|
||||||
|
messageInfos[id!] = await sock?.messageInfo(remoteJid, id)
|
||||||
|
}
|
||||||
|
return messageInfos[id!]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -129,24 +129,14 @@ export type MessageGenerationOptions = MessageContentGenerationOptions & Message
|
|||||||
|
|
||||||
export type MessageUpdateType = 'prepend' | 'append' | 'notify' | 'last'
|
export type MessageUpdateType = 'prepend' | 'append' | 'notify' | 'last'
|
||||||
|
|
||||||
|
export type MessageInfoEventMap = { [jid: string]: Date }
|
||||||
export interface MessageInfo {
|
export interface MessageInfo {
|
||||||
reads: {jid: string, t: string}[]
|
reads: MessageInfoEventMap
|
||||||
deliveries: {jid: string, t: string}[]
|
deliveries: MessageInfoEventMap
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
export interface MessageStatusUpdate {
|
|
||||||
from: string
|
|
||||||
to: string
|
|
||||||
/** Which participant caused the update (only for groups) */
|
|
||||||
participant?: string
|
|
||||||
timestamp: Date
|
|
||||||
/** Message IDs read/delivered */
|
|
||||||
ids: string[]
|
|
||||||
/** Status of the Message IDs */
|
|
||||||
type: WAMessageStatus
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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> }
|
||||||
@@ -16,7 +16,8 @@ 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, WAMessageKey, WAMessageUpdate } from './Message'
|
import { MessageInfo, MessageInfoUpdate, MessageUpdateType, WAMessage, WAMessageKey, WAMessageUpdate } from './Message'
|
||||||
|
import { proto } from '../../WAMessage/WAMessage'
|
||||||
|
|
||||||
/** used for binary messages */
|
/** used for binary messages */
|
||||||
export enum WAMetric {
|
export enum WAMetric {
|
||||||
@@ -183,6 +184,8 @@ export type BaileysEventMap = {
|
|||||||
'messages.update': WAMessageUpdate[]
|
'messages.update': WAMessageUpdate[]
|
||||||
'messages.upsert': { messages: WAMessage[], type: MessageUpdateType }
|
'messages.upsert': { messages: WAMessage[], type: MessageUpdateType }
|
||||||
|
|
||||||
|
'message-info.update': MessageInfoUpdate[]
|
||||||
|
|
||||||
'groups.update': Partial<GroupMetadata>[]
|
'groups.update': Partial<GroupMetadata>[]
|
||||||
'group-participants.update': { jid: string, participants: string[], action: ParticipantAction }
|
'group-participants.update': { jid: string, participants: string[], action: ParticipantAction }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user