|
9 | 9 | WAMessageUpdate,
|
10 | 10 | MessageUpsertType,
|
11 | 11 | WAMessageKey,
|
| 12 | + WASocket, |
12 | 13 | } from '@adiwajshing/baileys'
|
13 | 14 | import makeOrderedDictionary from '@adiwajshing/baileys/lib/Store/make-ordered-dictionary'
|
14 | 15 | import { waMessageID } from '@adiwajshing/baileys/lib/Store/make-in-memory-store'
|
@@ -97,19 +98,22 @@ const saveMedia = async (phone: string, waMessage: WAMessage) => {
|
97 | 98 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
98 | 99 | const dataStoreFile = (phone: string, config: any): DataStore => {
|
99 | 100 | const keys: Map<string, proto.IMessageKey> = new Map()
|
| 101 | + const jids: Map<string, string> = new Map() |
100 | 102 | const store = makeInMemoryStore(config)
|
101 | 103 | const dataStore = store as DataStore
|
102 | 104 | const { bind, toJSON, fromJSON } = store
|
103 | 105 | store.toJSON = () => {
|
104 | 106 | return {
|
105 | 107 | ...toJSON(),
|
106 | 108 | keys: keys.values(),
|
| 109 | + jids, |
107 | 110 | }
|
108 | 111 | }
|
109 | 112 | store.fromJSON = (json) => {
|
110 | 113 | fromJSON(json)
|
111 | 114 | const jsonData = json as {
|
112 | 115 | keys: proto.IMessageKey[]
|
| 116 | + jids: Map<string, string> |
113 | 117 | chats: Chat[]
|
114 | 118 | contacts: { [id: string]: Contact }
|
115 | 119 | messages: { [id: string]: WAMessage[] }
|
@@ -147,6 +151,20 @@ const dataStoreFile = (phone: string, config: any): DataStore => {
|
147 | 151 | dataStore.setKey = (id: string, key: WAMessageKey) => {
|
148 | 152 | return keys.set(id, key)
|
149 | 153 | }
|
| 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 | + } |
150 | 168 | dataStore.setMessage = (id: string, message: WAMessage) => {
|
151 | 169 | if (!store.messages[id]) {
|
152 | 170 | store.messages[id] = makeOrderedDictionary(waMessageID)
|
|
0 commit comments