feat: implement event buffer for offline msgs

!BREAKING_CHANGE

1. this allows all offline notifications to be combined into a batch -- to reduce the number of events being sent out
2. to enable the above, the "message.reaction" event has been made an array. Also removes the need for the "operation" field
3. have also now started processing all events under a single queue to prevent state sync problems
This commit is contained in:
Adhiraj Singh
2022-06-27 13:38:29 +05:30
parent fd80448fcb
commit 7421f55daa
18 changed files with 945 additions and 367 deletions

View File

@@ -1,12 +1,11 @@
import type { Boom } from '@hapi/boom'
import type EventEmitter from 'events'
import { proto } from '../../WAProto'
import { AuthenticationCreds } from './Auth'
import { WACallEvent } from './Call'
import { Chat, PresenceData } from './Chat'
import { Contact } from './Contact'
import { GroupMetadata, ParticipantAction } from './GroupMetadata'
import { MessageUpdateType, MessageUserReceiptUpdate, WAMessage, WAMessageKey, WAMessageUpdate } from './Message'
import { MessageUpsertType, MessageUserReceiptUpdate, WAMessage, WAMessageKey, WAMessageUpdate } from './Message'
import { ConnectionState } from './State'
export type BaileysEventMap<T> = {
@@ -39,8 +38,9 @@ export type BaileysEventMap<T> = {
* add/update the given messages. If they were received while the connection was online,
* the update will have type: "notify"
* */
'messages.upsert': { messages: WAMessage[], type: MessageUpdateType }
'messages.reaction': { key: WAMessageKey, reaction: proto.IReaction, operation: 'add' | 'remove' }
'messages.upsert': { messages: WAMessage[], type: MessageUpsertType }
/** message was reacted to. If reaction was removed -- then "reaction.text" will be falsey */
'messages.reaction': { key: WAMessageKey, reaction: proto.IReaction }[]
'message-receipt.update': MessageUserReceiptUpdate[]
@@ -55,10 +55,26 @@ export type BaileysEventMap<T> = {
'call': WACallEvent[]
}
export interface CommonBaileysEventEmitter<Creds> extends EventEmitter {
on<T extends keyof BaileysEventMap<Creds>>(event: T, listener: (arg: BaileysEventMap<Creds>[T]) => void): this
off<T extends keyof BaileysEventMap<Creds>>(event: T, listener: (arg: BaileysEventMap<Creds>[T]) => void): this
removeAllListeners<T extends keyof BaileysEventMap<Creds>>(event: T): this
export type BufferedEventData = {
chatUpserts: { [jid: string]: Chat }
chatUpdates: { [jid: string]: Partial<Chat> }
chatDeletes: Set<string>
contactUpserts: { [jid: string]: Contact }
contactUpdates: { [jid: string]: Partial<Contact> }
messageUpserts: { [key: string]: { type: MessageUpsertType, message: WAMessage } }
messageUpdates: { [key: string]: WAMessageUpdate }
messageDeletes: { [key: string]: WAMessageKey }
messageReactions: { [key: string]: { key: WAMessageKey, reactions: proto.IReaction[] } }
messageReceipts: { [key: string]: { key: WAMessageKey, userReceipt: proto.IUserReceipt[] } },
groupUpdates: { [jid: string]: Partial<GroupMetadata> }
}
export type BaileysEvent = keyof BaileysEventMap<any>
export interface CommonBaileysEventEmitter<Creds> {
on<T extends keyof BaileysEventMap<Creds>>(event: T, listener: (arg: BaileysEventMap<Creds>[T]) => void): void
off<T extends keyof BaileysEventMap<Creds>>(event: T, listener: (arg: BaileysEventMap<Creds>[T]) => void): void
removeAllListeners<T extends keyof BaileysEventMap<Creds>>(event: T): void
emit<T extends keyof BaileysEventMap<Creds>>(event: T, arg: BaileysEventMap<Creds>[T]): boolean
}

View File

@@ -186,7 +186,12 @@ export type MessageContentGenerationOptions = MediaGenerationOptions & {
}
export type MessageGenerationOptions = MessageContentGenerationOptions & MessageGenerationOptionsFromContent
export type MessageUpdateType = 'append' | 'notify' | 'replace'
/**
* Type of message upsert
* 1. notify => notify the user, this message was just received
* 2. append => append the message to the chat history, no notification required
*/
export type MessageUpsertType = 'append' | 'notify'
export type MessageUserReceipt = proto.IUserReceipt