2022-01-06 01:26:15 +00:00
|
|
|
import { Server as SocketIO } from "socket.io";
|
|
|
|
import { Server } from "http";
|
|
|
|
import AppError from "../errors/AppError";
|
|
|
|
import { logger } from "../utils/logger";
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2022-05-06 22:49:45 +00:00
|
|
|
import ListUserParamiterService from "../services/UserServices/ListUserParamiterService";
|
2023-08-14 09:48:39 +00:00
|
|
|
import {
|
|
|
|
addHours,
|
|
|
|
addMinutes,
|
|
|
|
addSeconds,
|
|
|
|
intervalToDuration,
|
|
|
|
add
|
|
|
|
} from "date-fns";
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
let io: SocketIO;
|
|
|
|
|
2022-05-03 21:20:58 +00:00
|
|
|
//test del
|
2022-05-06 22:49:45 +00:00
|
|
|
import createOrUpdateOnlineUserService from "../services/UserServices/CreateOrUpdateOnlineUserService";
|
|
|
|
import { splitDateTime } from "../helpers/SplitDateTime";
|
2023-08-14 09:48:39 +00:00
|
|
|
import format from "date-fns/format";
|
|
|
|
import ptBR from "date-fns/locale/pt-BR";
|
2022-05-06 22:49:45 +00:00
|
|
|
import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService";
|
2023-08-14 09:48:39 +00:00
|
|
|
import {
|
|
|
|
handleMessage,
|
|
|
|
handleMsgAck
|
|
|
|
} from "../services/WbotServices/wbotMessageListener";
|
2023-01-25 20:44:02 +00:00
|
|
|
import { join } from "path";
|
2023-02-07 15:47:40 +00:00
|
|
|
import Whatsapp from "../models/Whatsapp";
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
let count: number = 0;
|
|
|
|
let listOnline: any[] = [];
|
|
|
|
let listOnlineAux: any[] = [];
|
|
|
|
let countOnline: number = 0;
|
|
|
|
let obj: any = { listOnline: [], uuid: null, listOnlineAux: [] };
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
let lstOnline: any[] = [];
|
|
|
|
let lstOnlineAux: any[] = [];
|
|
|
|
let lstTry: any[] = [];
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
let dateTime = splitDateTime(
|
|
|
|
new Date(format(new Date(), "yyyy-MM-dd HH:mm:ss", { locale: ptBR }))
|
|
|
|
);
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
export const initIO = (httpServer: Server): SocketIO => {
|
|
|
|
io = new SocketIO(httpServer, {
|
|
|
|
cors: {
|
|
|
|
origin: process.env.FRONTEND_URL
|
2023-08-14 09:48:39 +00:00
|
|
|
},
|
2023-02-07 15:47:40 +00:00
|
|
|
maxHttpBufferSize: 1e8
|
2022-05-06 22:49:45 +00:00
|
|
|
});
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
io.on("connection", socket => {
|
2022-05-06 22:49:45 +00:00
|
|
|
logger.info("Client Connected");
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
socket.on("joinWhatsSession", (whatsappId: string) => {
|
|
|
|
logger.info(`A client joined a joinWhatsSession channel: ${whatsappId}`);
|
|
|
|
socket.join(`session_${whatsappId}`);
|
|
|
|
});
|
2023-01-25 20:44:02 +00:00
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
socket.on("message_from_client", () => {
|
2023-08-14 09:48:39 +00:00
|
|
|
socket.emit("message_from_server", "Sent an event from the server!");
|
|
|
|
});
|
2023-01-25 20:44:02 +00:00
|
|
|
|
|
|
|
socket.on("message_create", async (data: any) => {
|
2023-08-14 09:48:39 +00:00
|
|
|
handleMessage(data.msg, data);
|
2023-01-25 20:44:02 +00:00
|
|
|
});
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
socket.on("media_uploaded", async (data: any) => {
|
2023-02-07 15:47:40 +00:00
|
|
|
handleMessage(data.msg, data);
|
|
|
|
});
|
2023-01-25 20:44:02 +00:00
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
socket.on("message_ack", async (data: any) => {
|
2023-08-14 09:48:39 +00:00
|
|
|
handleMsgAck(data.id, data.ack);
|
2023-02-07 15:47:40 +00:00
|
|
|
});
|
2023-01-25 20:44:02 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
socket.on("campaign_message_sent", async (data: any) => {
|
2023-08-14 18:39:05 +00:00
|
|
|
// console.log("campaign_message_sent: ", data);
|
2023-08-14 09:48:39 +00:00
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
|
|
|
|
let campaign = {};
|
|
|
|
|
|
|
|
if (data?.read) {
|
|
|
|
campaign = {
|
|
|
|
id: data.id,
|
|
|
|
read: data.read
|
|
|
|
};
|
|
|
|
} else if (data?.sent) {
|
|
|
|
campaign = {
|
|
|
|
id: data.id,
|
|
|
|
sent: data.sent
|
|
|
|
};
|
|
|
|
} else if (data?.status) {
|
|
|
|
campaign = {
|
|
|
|
id: data.id,
|
|
|
|
status: data.status
|
|
|
|
};
|
|
|
|
}
|
2023-01-25 20:44:02 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
io.emit("campaign", {
|
|
|
|
action: "update",
|
|
|
|
campaign
|
|
|
|
});
|
|
|
|
});
|
2023-01-25 20:44:02 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
socket.on("online", (userId: any) => {
|
2022-12-27 14:25:50 +00:00
|
|
|
// console.log('userId: ', userId)
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
obj.uuid = uuidv4();
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
if (userId.logoutUserId) {
|
2023-08-14 09:48:39 +00:00
|
|
|
let index = lstOnline.findIndex(
|
|
|
|
(x: any) => x.id == userId.logoutUserId
|
|
|
|
);
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
if (index != -1) {
|
2023-08-14 09:48:39 +00:00
|
|
|
lstOnline.splice(index, 1);
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
2022-04-27 17:28:31 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
index = lstOnlineAux.findIndex((x: any) => x.id == userId.logoutUserId);
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
if (index != -1) {
|
2023-08-14 09:48:39 +00:00
|
|
|
lstOnlineAux.splice(index, 1);
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
return;
|
2022-05-06 22:49:45 +00:00
|
|
|
}
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
if (lstOnline.length == 0) {
|
2023-08-14 09:48:39 +00:00
|
|
|
const index = listOnlineAux.findIndex((e: any) => e.id == userId);
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
if (index == -1) {
|
2023-08-14 09:48:39 +00:00
|
|
|
listOnlineAux.push({ id: userId });
|
|
|
|
} else {
|
|
|
|
return;
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
lstOnline.push({ id: userId, status: "online", try: 0 });
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
lstOnlineAux.push({ id: userId });
|
2024-01-29 11:48:20 +00:00
|
|
|
console.log(" 1 - PUSHED NEW USER ID 1: ", userId);
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
obj.listOnline = lstOnline;
|
|
|
|
} else {
|
|
|
|
const indexAux = lstOnlineAux.findIndex((e: any) => e.id == userId);
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
if (indexAux == -1) {
|
2023-08-14 09:48:39 +00:00
|
|
|
lstOnlineAux.push({ id: userId });
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
const index = lstOnline.findIndex((e: any) => e.id == userId);
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2022-05-16 02:48:06 +00:00
|
|
|
if (index == -1) {
|
2023-08-14 09:48:39 +00:00
|
|
|
lstOnline.push({ id: userId, status: "online", try: 0 });
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2024-01-29 11:48:20 +00:00
|
|
|
console.log(" 2 - PUSHED NEW USER ID: ", userId);
|
2022-05-06 22:49:45 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
obj.listOnline = lstOnline;
|
|
|
|
} else {
|
|
|
|
if (countOnline > lstOnline.length - 1) {
|
2022-05-16 02:48:06 +00:00
|
|
|
lstOnline.forEach((x: any) => {
|
|
|
|
if (lstOnlineAux.map((e: any) => e.id).includes(x.id)) {
|
2023-08-14 09:48:39 +00:00
|
|
|
x.try = 0;
|
|
|
|
x.status = "online";
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
2023-08-14 09:48:39 +00:00
|
|
|
});
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
var difference = lstOnline.filter(
|
|
|
|
(x: any) => !lstOnlineAux.map((e: any) => e.id).includes(x.id)
|
|
|
|
);
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
if (difference.length > 0) {
|
2023-08-14 09:48:39 +00:00
|
|
|
difference.forEach(e => {
|
|
|
|
e.try += 1;
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
if (e.try > 1) {
|
2023-08-14 09:48:39 +00:00
|
|
|
e.status = "waiting...";
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (e.try > 3) {
|
2023-08-14 09:48:39 +00:00
|
|
|
const index = lstOnline.findIndex((x: any) => x.id == e.id);
|
2022-05-16 02:48:06 +00:00
|
|
|
|
|
|
|
if (index != -1) {
|
2023-08-14 09:48:39 +00:00
|
|
|
lstOnline.splice(index, 1);
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-14 09:48:39 +00:00
|
|
|
});
|
2022-05-06 22:49:45 +00:00
|
|
|
}
|
2023-08-14 09:48:39 +00:00
|
|
|
obj.listOnline = lstOnline;
|
|
|
|
obj.listOnlineAux = lstOnlineAux;
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
lstOnlineAux = [];
|
|
|
|
listOnlineAux = [];
|
2022-05-16 02:48:06 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
countOnline = -1;
|
2022-05-16 02:48:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
countOnline++;
|
2022-05-06 22:49:45 +00:00
|
|
|
}
|
2022-05-03 21:20:58 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
exports.ob = obj;
|
2022-05-06 22:49:45 +00:00
|
|
|
});
|
2022-04-27 17:28:31 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
socket.on("joinChatBox", (ticketId: string) => {
|
|
|
|
logger.info("A client joined a ticket channel");
|
|
|
|
socket.join(ticketId);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("joinNotification", () => {
|
|
|
|
logger.info("A client joined notification channel");
|
|
|
|
socket.join("notification");
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("joinTickets", (status: string) => {
|
|
|
|
logger.info(`A client joined to ${status} tickets channel.`);
|
|
|
|
socket.join(status);
|
|
|
|
});
|
|
|
|
|
2023-02-07 15:47:40 +00:00
|
|
|
socket.on("disconnect", (data: any) => {
|
|
|
|
logger.info(`Client disconnected socket: ${data}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("disconnecting", async () => {
|
2023-08-14 09:48:39 +00:00
|
|
|
console.log("socket.rooms: ", socket.rooms); // the Set contains at least the socket ID
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
let rooms = socket.rooms;
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
console.log("rooms: ", rooms, " | rooms.size: ", rooms.size);
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
if (rooms && rooms.size == 1) return;
|
|
|
|
if (rooms && rooms.size == 2 && ![...rooms][1].startsWith("session_"))
|
|
|
|
return;
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
let whatsappIds: any = await Whatsapp.findAll({
|
|
|
|
attributes: ["id"],
|
|
|
|
raw: true
|
|
|
|
});
|
2023-02-07 15:47:40 +00:00
|
|
|
|
|
|
|
if (whatsappIds && whatsappIds.length > 0) {
|
2023-08-14 09:48:39 +00:00
|
|
|
whatsappIds = whatsappIds.map((e: any) => `${e.id}`);
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
console.log(
|
|
|
|
"whatsappIds whatsappIds whatsappIds whatsappIds whatsappIds: ",
|
|
|
|
whatsappIds
|
|
|
|
);
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
if (
|
|
|
|
rooms &&
|
|
|
|
rooms.size == 2 &&
|
|
|
|
[...rooms][1].startsWith("session_") &&
|
|
|
|
whatsappIds.includes([...rooms][1].replace("session_", ""))
|
|
|
|
) {
|
|
|
|
console.log([...rooms][1]);
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
let whatsappId = [...rooms][1].replace("session_", "");
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2023-08-14 09:48:39 +00:00
|
|
|
const whatsapp = await Whatsapp.findByPk(whatsappId, {});
|
2023-02-07 15:47:40 +00:00
|
|
|
|
|
|
|
if (whatsapp) {
|
2023-08-14 09:48:39 +00:00
|
|
|
await whatsapp.update({ status: "OPENING" });
|
2023-02-07 15:47:40 +00:00
|
|
|
|
|
|
|
io.emit("whatsappSession", {
|
|
|
|
action: "update",
|
|
|
|
session: whatsapp
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 01:26:15 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return io;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getIO = (): SocketIO => {
|
|
|
|
if (!io) {
|
|
|
|
throw new AppError("Socket IO not initialized");
|
|
|
|
}
|
|
|
|
return io;
|
|
|
|
};
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2023-01-25 20:44:02 +00:00
|
|
|
function writeFileAsync(arg0: any, data: any, arg2: string) {
|
|
|
|
throw new Error("Function not implemented.");
|
|
|
|
}
|
2023-02-07 15:47:40 +00:00
|
|
|
|
2022-05-06 22:49:45 +00:00
|
|
|
// exports.listOnlineUsers = listUserId
|
2023-08-14 09:48:39 +00:00
|
|
|
// exports.listUserId
|