update proto + message info

This commit is contained in:
Adhiraj Singh
2021-07-29 13:34:02 +05:30
parent 5f8106d4e0
commit 5c4a317213
6 changed files with 41116 additions and 5681 deletions

11489
WAMessage/WAMessage.d.ts vendored

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,10 +1,11 @@
import BinaryNode from "../BinaryNode";
import { Boom } from '@hapi/boom'
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 makeChatsSocket from "./chats";
import { WA_DEFAULT_EPHEMERAL } from "../Defaults";
import { Attributes } from "../BinaryNode/types";
const STATUS_MAP = {
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,,received', onMessageStatusUpdate)
socketEvents.on('CB:Msg', onMessageInfoUpdate)
socketEvents.on('CB:MsgInfo', onMessageInfoUpdate)
return {
...sock,
relayWAMessage,
@@ -325,15 +359,18 @@ const makeMessagesSocket = (config: SocketConfig) => {
expect200: true,
requiresPhoneConnection: true
})
const info: MessageInfo = {reads: [], deliveries: []}
const info: MessageInfo = { reads: {}, deliveries: {} }
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) {
case 'read':
info.reads.push(attributes as any)
info.reads[jid] = date
break
case 'delivery':
info.deliveries.push(attributes as any)
info.deliveries[jid] = date
break
}
}

View File

@@ -2,7 +2,7 @@ import type KeyedDB from "@adiwajshing/keyed-db"
import type { Comparable } from "@adiwajshing/keyed-db/lib/Types"
import type { Logger } from "pino"
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 makeOrderedDictionary from "./ordered-dictionary"
@@ -28,6 +28,7 @@ export default(
const messages: { [_: string]: ReturnType<typeof makeMessagesDictionary> } = {}
const contacts: { [_: string]: Contact } = {}
const groupMetadata: { [_: string]: GroupMetadata } = {}
const messageInfos: { [id: string]: MessageInfo } = { }
const state: ConnectionState = {
connection: 'close',
phoneConnected: false
@@ -101,10 +102,14 @@ export default(
const list = assertMessageList(jid)
list.upsert(msg, 'append')
if(type === 'notify' && !chats.get(jid)) {
ev.emit('chats.upsert', [
{ jid, t: toNumber(msg.messageTimestamp), count: 1 }
])
if(type === 'notify') {
if(!chats.get(jid)) {
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
@@ -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 {
chats,
contacts,
messages,
groupMetadata,
messageInfos,
state,
listen,
loadMessages: async(jid: string, count: number, cursor: WAMessageCursor, sock: Connection | undefined) => {
@@ -261,6 +280,12 @@ export default(
groupMetadata[jid] = await sock?.getBroadcastListInfo(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!]
}
}
}

View File

@@ -129,24 +129,14 @@ export type MessageGenerationOptions = MessageContentGenerationOptions & Message
export type MessageUpdateType = 'prepend' | 'append' | 'notify' | 'last'
export type MessageInfoEventMap = { [jid: string]: Date }
export interface MessageInfo {
reads: {jid: string, t: string}[]
deliveries: {jid: string, t: string}[]
}
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
reads: MessageInfoEventMap
deliveries: MessageInfoEventMap
}
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> }

View File

@@ -16,7 +16,8 @@ import { Contact } from './Contact'
import { ConnectionState } from './Store'
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 */
export enum WAMetric {
@@ -183,6 +184,8 @@ export type BaileysEventMap = {
'messages.update': WAMessageUpdate[]
'messages.upsert': { messages: WAMessage[], type: MessageUpdateType }
'message-info.update': MessageInfoUpdate[]
'groups.update': Partial<GroupMetadata>[]
'group-participants.update': { jid: string, participants: string[], action: ParticipantAction }