Skip to content

Commit e1fb44f

Browse files
committed
cache jids
1 parent ca074f2 commit e1fb44f

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/services/client_baileys.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,11 @@ class ClientBaileys implements Client {
215215
private async toJid(phoneNumber: string) {
216216
const bindJid: string = phoneNumberToJid(phoneNumber)
217217
if (isIndividualJid(bindJid)) {
218-
const results = await this.sock?.onWhatsApp(bindJid)
219-
const result = results && results[0]
220-
if (result && result.exists) {
221-
console.debug(`${bindJid} exists on WhatsApp, as jid: ${result.jid}`)
222-
return result.jid
218+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
219+
const jid = await this.store?.dataStore?.getJid(bindJid, this.sock!)
220+
if (jid) {
221+
console.debug(`${bindJid} exists on WhatsApp, as jid: ${jid}`)
222+
return jid
223223
} else {
224224
console.warn(`${bindJid} not exists on WhatsApp`)
225225
return ''

src/services/data_store.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeInMemoryStore, WAMessage, WAMessageKey } from '@adiwajshing/baileys'
1+
import { makeInMemoryStore, WAMessage, WAMessageKey, WASocket } from '@adiwajshing/baileys'
22

33
export const dataStores: Map<string, DataStore> = new Map()
44

@@ -9,6 +9,7 @@ export interface getDataStore {
99
export type DataStore = ReturnType<typeof makeInMemoryStore> & {
1010
loadKey: (id: string) => WAMessageKey | undefined
1111
setKey: (id: string, key: WAMessageKey) => void
12+
getJid: (phone: string, sock: Partial<WASocket>) => Promise<string>
1213
setMessage: (id: string, message: WAMessage) => void
1314
saveMedia: (waMessage: WAMessage) => Promise<void>
1415
cleanSession: () => Promise<void>

src/services/data_store_file.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
WAMessageUpdate,
1010
MessageUpsertType,
1111
WAMessageKey,
12+
WASocket,
1213
} from '@adiwajshing/baileys'
1314
import makeOrderedDictionary from '@adiwajshing/baileys/lib/Store/make-ordered-dictionary'
1415
import { waMessageID } from '@adiwajshing/baileys/lib/Store/make-in-memory-store'
@@ -97,19 +98,22 @@ const saveMedia = async (phone: string, waMessage: WAMessage) => {
9798
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9899
const dataStoreFile = (phone: string, config: any): DataStore => {
99100
const keys: Map<string, proto.IMessageKey> = new Map()
101+
const jids: Map<string, string> = new Map()
100102
const store = makeInMemoryStore(config)
101103
const dataStore = store as DataStore
102104
const { bind, toJSON, fromJSON } = store
103105
store.toJSON = () => {
104106
return {
105107
...toJSON(),
106108
keys: keys.values(),
109+
jids,
107110
}
108111
}
109112
store.fromJSON = (json) => {
110113
fromJSON(json)
111114
const jsonData = json as {
112115
keys: proto.IMessageKey[]
116+
jids: Map<string, string>
113117
chats: Chat[]
114118
contacts: { [id: string]: Contact }
115119
messages: { [id: string]: WAMessage[] }
@@ -147,6 +151,20 @@ const dataStoreFile = (phone: string, config: any): DataStore => {
147151
dataStore.setKey = (id: string, key: WAMessageKey) => {
148152
return keys.set(id, key)
149153
}
154+
dataStore.getJid = async (phoneOrJid: string, sock: Partial<WASocket>) => {
155+
if (!jids.has(phoneOrJid)) {
156+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
157+
const results = await sock.onWhatsApp!(phoneOrJid)
158+
const result = results && results[0]
159+
if (result && result.exists) {
160+
console.debug(`${phoneOrJid} exists on WhatsApp, as jid: ${result.jid}`)
161+
jids.set(phoneOrJid, result.jid)
162+
} else {
163+
console.warn(`${phoneOrJid} not exists on WhatsApp`)
164+
}
165+
}
166+
return jids.get(phoneOrJid) || ''
167+
}
150168
dataStore.setMessage = (id: string, message: WAMessage) => {
151169
if (!store.messages[id]) {
152170
store.messages[id] = makeOrderedDictionary(waMessageID)

0 commit comments

Comments
 (0)