feat: added new method to fetch old whatsapp messages

This commit is contained in:
canove
2020-10-30 10:37:57 -03:00
parent 5a51581bd3
commit 099a3354ca
14 changed files with 116 additions and 36 deletions

View File

@@ -13,19 +13,32 @@ export const GetWbotMessage = async (
`${ticket.contact.number}@${ticket.isGroup ? "g" : "c"}.us`
);
try {
const chatMessages = await wbotChat.fetchMessages({ limit: 20 });
let limit = 20;
const msgToDelete = chatMessages.find(msg => msg.id.id === messageId);
const fetchWbotMessagesGradually = async (): Promise<void | WbotMessage> => {
const chatMessages = await wbotChat.fetchMessages({ limit });
if (!msgToDelete) {
throw new Error();
const msgFound = chatMessages.find(msg => msg.id.id === messageId);
if (!msgFound && limit < 100) {
limit += 20;
return fetchWbotMessagesGradually();
}
return msgToDelete;
return msgFound;
};
try {
const msgFound = await fetchWbotMessagesGradually();
if (!msgFound) {
throw new Error("Cannot found message within 100 last messages");
}
return msgFound;
} catch (err) {
console.log(err);
throw new AppError("ERR_DELETE_WAPP_MSG");
throw new AppError("ERR_FETCH_WAPP_MSG");
}
};

View File

@@ -0,0 +1,12 @@
import Message from "../models/Message";
import Ticket from "../models/Ticket";
const SerializeWbotMsgId = (ticket: Ticket, message: Message): string => {
const serializedMsgId = `${message.fromMe}_${ticket.contact.number}@${
ticket.isGroup ? "g" : "c"
}.us_${message.id}`;
return serializedMsgId;
};
export default SerializeWbotMsgId;