fix: disable initial syncing messages after connection

This commit is contained in:
canove
2020-11-19 21:43:53 -03:00
parent 96bdb2aea6
commit 36098bf12d

View File

@@ -3,7 +3,7 @@ import { Client } from "whatsapp-web.js";
import { getIO } from "./socket"; import { getIO } from "./socket";
import Whatsapp from "../models/Whatsapp"; import Whatsapp from "../models/Whatsapp";
import AppError from "../errors/AppError"; import AppError from "../errors/AppError";
import { handleMessage } from "../services/WbotServices/wbotMessageListener"; // import { handleMessage } from "../services/WbotServices/wbotMessageListener";
interface Session extends Client { interface Session extends Client {
id?: number; id?: number;
@@ -11,20 +11,20 @@ interface Session extends Client {
const sessions: Session[] = []; const sessions: Session[] = [];
const syncUnreadMessages = async (wbot: Session) => { // const syncUnreadMessages = async (wbot: Session) => {
const chats = await wbot.getChats(); // const chats = await wbot.getChats();
chats.forEach(async chat => { // chats.forEach(async chat => {
if (chat.unreadCount > 0) { // if (chat.unreadCount > 0) {
const unreadMessages = await chat.fetchMessages({ // const unreadMessages = await chat.fetchMessages({
limit: chat.unreadCount // limit: chat.unreadCount
}); // });
unreadMessages.forEach(msg => { // unreadMessages.forEach(msg => {
handleMessage(msg, wbot); // handleMessage(msg, wbot);
}); // });
} // }
}); // });
}; // };
export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => { export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@@ -105,7 +105,7 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
wbot.on("ready", async () => { wbot.on("ready", async () => {
console.log("Session:", sessionName, "READY"); console.log("Session:", sessionName, "READY");
syncUnreadMessages(wbot); // syncUnreadMessages(wbot);
await whatsapp.update({ await whatsapp.update({
status: "CONNECTED", status: "CONNECTED",