From 8a014df1c57c552bd178bd427000c0590763df8f Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Tue, 24 Aug 2021 11:00:37 +0530 Subject: [PATCH] separate data structure for presence --- src/Connection/chats.ts | 33 +++++++++++---------------------- src/Connection/messages.ts | 11 +++++++---- src/Store/in-memory-store.ts | 14 ++++++++++---- src/Types/Chat.ts | 6 +----- src/Types/index.ts | 4 +++- 5 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/Connection/chats.ts b/src/Connection/chats.ts index b752018..136a7bd 100644 --- a/src/Connection/chats.ts +++ b/src/Connection/chats.ts @@ -1,5 +1,5 @@ import BinaryNode from "../BinaryNode"; -import { Chat, Contact, Presence, PresenceData, SocketConfig, WAFlag, WAMetric, WABusinessProfile, ChatModification, WAMessageKey, WAMessage, WAMessageUpdate } from "../Types"; +import { Chat, Contact, Presence, PresenceData, SocketConfig, WAFlag, WAMetric, WABusinessProfile, ChatModification, WAMessageKey, WAMessage, WAMessageUpdate, BaileysEventMap } from "../Types"; import { debouncedTimeout, unixTimestampSeconds, whatsappID } from "../Utils/generics"; import makeAuthSocket from "./auth"; import { Attributes, BinaryNode as BinaryNodeBase } from "../BinaryNode/types"; @@ -91,26 +91,15 @@ const makeChatsSocket = (config: SocketConfig) => { } } - const applyingPresenceUpdate = (update: Attributes, chat: Partial) => { - chat.jid = whatsappID(update.id) - const jid = whatsappID(update.participant || update.id) + const applyingPresenceUpdate = (update: Attributes): BaileysEventMap['presence.update'] => { + const jid = whatsappID(update.id) + const participant = whatsappID(update.participant || update.id) - if (jid.endsWith('@s.whatsapp.net')) { // if its a single chat - chat.presences = chat.presences || {} - - const presence = { } as PresenceData - - if(update.t) { - presence.lastSeen = +update.t - } - presence.lastKnownPresence = update.type as Presence - chat.presences[jid] = presence - - chat.presences = { - [jid]: presence - } - } - return chat + const presence: PresenceData = { + lastSeen: update.t ? +update.t : undefined, + lastKnownPresence: update.type as Presence + } + return { jid, presences: { [participant]: presence } } } ev.on('connection.update', async({ connection }) => { @@ -239,8 +228,8 @@ const makeChatsSocket = (config: SocketConfig) => { // presence updates socketEvents.on('CB:Presence', json => { - const chat = applyingPresenceUpdate(json[1], { }) - ev.emit('chats.update', [ chat ]) + const update = applyingPresenceUpdate(json[1]) + ev.emit('presence.update', update) }) // blocklist updates diff --git a/src/Connection/messages.ts b/src/Connection/messages.ts index 7d99b11..aff4cb9 100644 --- a/src/Connection/messages.ts +++ b/src/Connection/messages.ts @@ -119,11 +119,14 @@ const makeMessagesSocket = (config: SocketConfig) => { if(!message.key.fromMe && message.message) { chatUpdate.count = 1 const participant = whatsappID(message.participant || jid) - chatUpdate.presences = { - [participant]: { - lastKnownPresence: Presence.available + + ev.emit( + 'presence.update', + { + jid, + presences: { [participant]: { lastKnownPresence: Presence.available } } } - } + ) } const ephemeralProtocolMsg = message.message?.ephemeralMessage?.message?.protocolMessage diff --git a/src/Store/in-memory-store.ts b/src/Store/in-memory-store.ts index 564ad8e..8e4ac32 100644 --- a/src/Store/in-memory-store.ts +++ b/src/Store/in-memory-store.ts @@ -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 const chats = new KeyedDBConstructor(chatKey, c => c.jid) - const messages: { [_: string]: ReturnType } = {} - const contacts: { [_: string]: Contact } = {} - const groupMetadata: { [_: string]: GroupMetadata } = {} + const messages: { [_: string]: ReturnType } = { } + 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) diff --git a/src/Types/Chat.ts b/src/Types/Chat.ts index d35b929..5815b5a 100644 --- a/src/Types/Chat.ts +++ b/src/Types/Chat.ts @@ -8,9 +8,8 @@ export enum Presence { } export interface PresenceData { - lastKnownPresence?: Presence + lastKnownPresence: Presence lastSeen?: number - name?: string } export interface Chat { @@ -31,9 +30,6 @@ export interface Chat { eph_setting_ts?: string /** how long each message lasts for */ ephemeral?: string - - // Baileys added properties - presences?: { [k: string]: PresenceData } } export type ChatModification = diff --git a/src/Types/index.ts b/src/Types/index.ts index 0a32093..5b0008f 100644 --- a/src/Types/index.ts +++ b/src/Types/index.ts @@ -11,7 +11,7 @@ import type { Logger } from "pino" import type { URL } from "url" import type BinaryNode from "../BinaryNode" import { AnyAuthenticationCredentials, AuthenticationCredentials } from './Auth' -import { Chat } from './Chat' +import { Chat, PresenceData } from './Chat' import { Contact } from './Contact' import { ConnectionState } from './Store' @@ -176,6 +176,8 @@ export type BaileysEventMap = { 'chats.update': Partial[] 'chats.delete': string[] + 'presence.update': { jid: string, presences: { [participant: string]: PresenceData } } + 'contacts.set': { contacts: Contact[] } 'contacts.upsert': Contact[] 'contacts.update': Partial[]