separate data structure for presence

This commit is contained in:
Adhiraj Singh
2021-08-24 11:00:37 +05:30
parent 48f477e0c1
commit 8a014df1c5
5 changed files with 32 additions and 36 deletions

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, MessageInfo, WAMessage, WAMessageCursor, WAMessageKey } from "../Types"
import type { BaileysEventEmitter, Chat, ConnectionState, Contact, GroupMetadata, MessageInfo, PresenceData, WAMessage, WAMessageCursor, WAMessageKey } from "../Types"
import { toNumber } from "../Utils"
import makeOrderedDictionary from "./ordered-dictionary"
@@ -25,10 +25,11 @@ export default(
) => {
const KeyedDBConstructor = require('@adiwajshing/keyed-db').default as new (...args: any[]) => KeyedDB<Chat, string>
const chats = new KeyedDBConstructor(chatKey, c => c.jid)
const messages: { [_: string]: ReturnType<typeof makeMessagesDictionary> } = {}
const contacts: { [_: string]: Contact } = {}
const groupMetadata: { [_: string]: GroupMetadata } = {}
const messages: { [_: string]: ReturnType<typeof makeMessagesDictionary> } = { }
const contacts: { [_: string]: Contact } = { }
const groupMetadata: { [_: string]: GroupMetadata } = { }
const messageInfos: { [id: string]: MessageInfo } = { }
const presences: { [id: string]: { [participant: string]: PresenceData } } = { }
const state: ConnectionState = {
connection: 'close',
phoneConnected: false
@@ -88,6 +89,10 @@ export default(
}
}
})
ev.on('presence.update', ({ jid, presences: update }) => {
presences[jid] = presences[jid] || {}
Object.assign(presences[jid], update)
})
ev.on('chats.delete', deletions => {
for(const item of deletions) {
chats.deleteById(item)
@@ -207,6 +212,7 @@ export default(
groupMetadata,
messageInfos,
state,
presences,
listen,
loadMessages: async(jid: string, count: number, cursor: WAMessageCursor, sock: Connection | undefined) => {
const list = assertMessageList(jid)