diff --git a/apps/server/whatsapp-common/src/firebaseRD.creds.store.ts b/apps/server/whatsapp-common/src/firebaseRD.creds.store.ts new file mode 100644 index 00000000..435502a3 --- /dev/null +++ b/apps/server/whatsapp-common/src/firebaseRD.creds.store.ts @@ -0,0 +1,94 @@ +/* eslint-disable no-console */ +/* eslint-disable @typescript-eslint/no-explicit-any */ +// import generalConfig from "./assets/general.config.json" +import { + AuthenticationCreds, + AuthenticationState, + BufferJSON, + initAuthCreds, + proto, +} from '@whiskeysockets/baileys'; +import { firebaseDb } from './firebase.app'; +export const useFireBaseRealTimeDatabaseStoreAuthState = async ( + collectionName: string, + serverName: string, +): Promise<{ state: AuthenticationState; saveCreds: () => Promise }> => { + const collection = firebaseDb.ref(collectionName).child(serverName); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const writeData = async (data: any, file: string) => { + data = JSON.stringify(data, BufferJSON.replacer); + console.log('Writing data type ', typeof data); + console.log('Writing data ', data); + collection.child(file).set(data); + return; + }; + + const readData = async (file: string) => { + try { + console.log('Reading ', file); + const data = await collection.child(file).get(); + if (!data.exists()) { + return null; + } + const cc = data.val(); + let finalRead = JSON.parse(cc, BufferJSON.reviver); + if (typeof finalRead === 'string') { + finalRead = JSON.parse(finalRead, BufferJSON.reviver); + } + console.log('final REad ', finalRead); + console.log(typeof finalRead); + return finalRead; + } catch (error) { + return null; + } + }; + + const removeData = async (file: string) => { + try { + await collection.child(file).set(null); + } catch { + // + } + }; + + const creds: AuthenticationCreds = + (await readData('creds')) || initAuthCreds(); + + return { + state: { + creds, + keys: { + get: async (type: any, ids: string[]) => { + const data: { [_: string]: any } = {}; + await Promise.all( + ids.map(async (id) => { + let value = await readData(`${type}-${id}`); + if (type === 'app-state-sync-key' && value) { + value = proto.Message.AppStateSyncKeyData.fromObject(value); + } + + data[id] = value; + }), + ); + + return data; + }, + set: async (data: any) => { + const tasks: Promise[] = []; + for (const category in data) { + for (const id in data[category]) { + const value = data[category][id]; + const file = `${category}-${id}`; + tasks.push(value ? writeData(value, file) : removeData(file)); + } + } + + await Promise.all(tasks); + }, + }, + }, + saveCreds: () => { + return writeData(creds, 'creds'); + }, + }; +}; diff --git a/apps/server/whatsapp-common/src/firestore.creds.store.ts b/apps/server/whatsapp-common/src/firestore.creds.store.ts index da307afd..7a394ec5 100644 --- a/apps/server/whatsapp-common/src/firestore.creds.store.ts +++ b/apps/server/whatsapp-common/src/firestore.creds.store.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ /* eslint-disable @typescript-eslint/no-explicit-any */ // import generalConfig from "./assets/general.config.json" import { @@ -22,7 +23,7 @@ export const useFireStoreAuthState = async ( data = JSON.stringify(data, BufferJSON.replacer); collection.doc(serverName).update( { - [file]: JSON.stringify(data, BufferJSON.replacer), + [file]: data, }, { // exists:true @@ -40,10 +41,15 @@ export const useFireStoreAuthState = async ( return null; } const cc = data.data(); - const finalRead = cc?.[file] + let finalRead = cc?.[file] ? JSON.parse(cc?.[file], BufferJSON.reviver) : null; - console.log('final REad ', finalRead); + if (typeof finalRead === 'string') { + finalRead = JSON.parse(finalRead, BufferJSON.reviver); + } + // debugger + // console.log('final REad ', finalRead); + // console.log(typeof finalRead) return finalRead; } catch (error) { return null; diff --git a/apps/server/whatsapp-common/src/main.ts b/apps/server/whatsapp-common/src/main.ts index ef6056af..2abbd72a 100644 --- a/apps/server/whatsapp-common/src/main.ts +++ b/apps/server/whatsapp-common/src/main.ts @@ -3,7 +3,8 @@ import { Boom } from '@hapi/boom'; import makeWASocket, { DisconnectReason } from '@whiskeysockets/baileys'; import _generalConfig from './assets/general.config.json'; -import { useFireStoreAuthState } from './firestore.creds.store'; +import { useFireBaseRealTimeDatabaseStoreAuthState } from './firebaseRD.creds.store'; +import pin from 'pino'; const ServerConfig = { whatsappLoggedIn: false, @@ -13,12 +14,17 @@ const ServerConfig = { console.log(ServerConfig); async function connectToWhatsApp() { - const { state, saveCreds } = await useFireStoreAuthState( + const { state, saveCreds } = await useFireBaseRealTimeDatabaseStoreAuthState( _generalConfig.storeCredCollectionName, _generalConfig.serverName, ); + // const { state, saveCreds } = await useFireStoreAuthState( + // _generalConfig.storeCredCollectionName, + // _generalConfig.serverName, + // ); const sock = makeWASocket({ auth: state, + logger: pin({ level: 'info' }), // logger: pin({ level: 'debug' }), // can provide additional config here printQRInTerminal: true, @@ -46,7 +52,9 @@ async function connectToWhatsApp() { console.log(connection); } }); - // sock.ev.on('co') + sock.ev.on('presence.update', (update) => { + console.log(update.id, 'Is Online', update.presences); + }); } // run in main file connectToWhatsApp();