diff --git a/backend/src/helpers/authState.ts b/backend/src/helpers/authState.ts index 07182f74..24cb6dad 100644 --- a/backend/src/helpers/authState.ts +++ b/backend/src/helpers/authState.ts @@ -15,34 +15,45 @@ const authState = async ( const whatsappId = whatsapp.id; const saveKey = async (type: string, key: string, value: any) => { - logger.debug(`Storing key whatsappId: ${whatsappId} type: ${type} key: ${key}`); - return BaileysKeys.upsert({ whatsappId, type, key, value: JSON.stringify(value)}); - } - + logger.debug( + `Storing key whatsappId: ${whatsappId} type: ${type} key: ${key}` + ); + return BaileysKeys.upsert({ + whatsappId, + type, + key, + value: JSON.stringify(value) + }); + }; + const getKey = async (type: string, key: string) => { const baileysKey = await BaileysKeys.findOne({ where: { whatsappId, type, - key, + key } }); - logger.debug(`${baileysKey ? "Successfull" : "Failed"} recover of key whatsappId: ${whatsappId} type: ${type} key: ${key}`); + logger.debug( + `${ + baileysKey ? "Successfull" : "Failed" + } recover of key whatsappId: ${whatsappId} type: ${type} key: ${key}` + ); return baileysKey?.value ? JSON.parse(baileysKey.value) : null; - } - + }; + const removeKey = async (type: string, key: string) => { - logger.debug({type, key}, "Deleting key"); + logger.debug({ type, key }, "Deleting key"); return BaileysKeys.destroy({ where: { whatsappId, type, - key, + key } }); - } + }; const saveState = async () => { try { @@ -57,23 +68,23 @@ const authState = async ( if (whatsapp.session && whatsapp.session !== null) { const result = JSON.parse(whatsapp.session, BufferJSON.reviver); creds = result.creds; - const {keys} = result; - + const { keys } = result; + // conversion from old format (remove in the future) if (Object.keys(keys).length) { logger.debug("Starting conversion of keys to new format"); const TYPE_MAP = { - "preKeys": "pre-key", - "sessions": "session", - "senderKeys": "sender-key", - "appStateSyncKeys": "app-state-sync-key", - "appStateVersions": "app-state-sync-version", - "senderKeyMemory": "sender-key-memory" + preKeys: "pre-key", + sessions: "session", + senderKeys: "sender-key", + appStateSyncKeys: "app-state-sync-key", + appStateVersions: "app-state-sync-version", + senderKeyMemory: "sender-key-memory" }; - + // eslint-disable-next-line no-restricted-syntax for await (const oldType of Object.keys(keys)) { - const newType=TYPE_MAP[oldType]; + const newType = TYPE_MAP[oldType]; logger.debug(`Converting keys of type ${oldType} to ${newType}`); // eslint-disable-next-line no-restricted-syntax for await (const key of Object.keys(keys[oldType])) { @@ -82,7 +93,6 @@ const authState = async ( } saveState(); } - } else { creds = initAuthCreds(); } @@ -103,12 +113,8 @@ const authState = async ( } data[id] = value; } catch (error) { - logger.error( - `authState (69) -> error: ${error.message}` - ); - logger.error( - `authState (72) -> stack: ${error.stack}` - ); + logger.error(`authState (69) -> error: ${error.message}`); + logger.error(`authState (72) -> stack: ${error.stack}`); } } @@ -121,7 +127,9 @@ const authState = async ( // eslint-disable-next-line no-restricted-syntax, guard-for-in for (const id in data[category]) { const value = data[category][id]; - tasks.push(value ? saveKey(category, id, value) : removeKey(category, id)); + tasks.push( + value ? saveKey(category, id, value) : removeKey(category, id) + ); } }