Skip to content

Commit

Permalink
authState.ts: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Jul 12, 2024
1 parent 0d62afe commit 18ead81
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions backend/src/helpers/authState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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])) {
Expand All @@ -82,7 +93,6 @@ const authState = async (
}
saveState();
}

} else {
creds = initAuthCreds();
}
Expand All @@ -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}`);
}
}

Expand All @@ -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)
);
}
}

Expand Down

0 comments on commit 18ead81

Please sign in to comment.