mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 03:39:29 +00:00
major: change message logic and breaks oldticket
This commit is contained in:
@@ -129,25 +129,27 @@ exports.store = async (req, res, next) => {
|
||||
);
|
||||
}
|
||||
|
||||
message.id = sentMessage.id.id;
|
||||
// CHANGED MESSAGE CREATION LOGIC TO wbotMessageListener, to handle both send from app and cellphone
|
||||
// THIS SHOULD BE DELETED IN FUTURE VERSION
|
||||
// message.id = sentMessage.id.id;
|
||||
|
||||
const newMessage = await ticket.createMessage(message);
|
||||
// const newMessage = await ticket.createMessage(message);
|
||||
|
||||
const serialziedMessage = {
|
||||
...newMessage.dataValues,
|
||||
mediaUrl: `${
|
||||
message.mediaUrl
|
||||
? `http://${process.env.HOST}:${process.env.PORT}/public/${message.mediaUrl}`
|
||||
: ""
|
||||
}`,
|
||||
};
|
||||
// const serialziedMessage = {
|
||||
// ...newMessage.dataValues,
|
||||
// mediaUrl: `${
|
||||
// message.mediaUrl
|
||||
// ? `http://${process.env.HOST}:${process.env.PORT}/public/${message.mediaUrl}`
|
||||
// : ""
|
||||
// }`,
|
||||
// };
|
||||
|
||||
io.to(ticketId).emit("appMessage", {
|
||||
action: "create",
|
||||
message: serialziedMessage,
|
||||
});
|
||||
// io.to(ticketId).emit("appMessage", {
|
||||
// action: "create",
|
||||
// message: serialziedMessage,
|
||||
// });
|
||||
|
||||
await setMessagesAsRead(ticketId);
|
||||
|
||||
return res.json({ message: "Mensagem enviada", newMessage, ticket });
|
||||
return res.json({ message: "Mensagem enviada", ticket });
|
||||
};
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = {
|
||||
up: queryInterface => {
|
||||
return queryInterface.removeColumn("Messages", "userId");
|
||||
},
|
||||
|
||||
down: (queryInterface, Sequelize) => {
|
||||
return queryInterface.addColumn("Messages", "userId", {
|
||||
type: Sequelize.INTEGER,
|
||||
references: { model: "Users", key: "id" },
|
||||
onUpdate: "CASCADE",
|
||||
onDelete: "SET NULL",
|
||||
});
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = {
|
||||
up: (queryInterface, Sequelize) => {
|
||||
return queryInterface.addColumn("Messages", "fromMe", {
|
||||
type: Sequelize.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: false,
|
||||
});
|
||||
},
|
||||
|
||||
down: queryInterface => {
|
||||
return queryInterface.removeColumn("Messages", "fromMe");
|
||||
},
|
||||
};
|
||||
@@ -6,6 +6,7 @@ class Message extends Sequelize.Model {
|
||||
{
|
||||
ack: { type: Sequelize.INTEGER, defaultValue: 0 },
|
||||
read: { type: Sequelize.BOOLEAN, defaultValue: false },
|
||||
fromMe: { type: Sequelize.BOOLEAN, defaultValue: false },
|
||||
body: { type: Sequelize.TEXT },
|
||||
mediaUrl: { type: Sequelize.STRING },
|
||||
mediaType: { type: Sequelize.STRING },
|
||||
@@ -28,7 +29,6 @@ class Message extends Sequelize.Model {
|
||||
|
||||
static associate(models) {
|
||||
this.belongsTo(models.Ticket, { foreignKey: "ticketId" });
|
||||
this.belongsTo(models.User, { foreignKey: "userId" });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const { Op } = require("sequelize");
|
||||
const { parseISO, subHours } = require("date-fns");
|
||||
const { subHours } = require("date-fns");
|
||||
|
||||
const Contact = require("../models/Contact");
|
||||
const Ticket = require("../models/Ticket");
|
||||
@@ -93,67 +93,65 @@ const handlMedia = async (msg, ticket) => {
|
||||
return newMessage;
|
||||
};
|
||||
|
||||
const wbotMessageListener = () => {
|
||||
const handleMessage = async (msg, ticket, contact) => {
|
||||
const io = getIO();
|
||||
let newMessage;
|
||||
|
||||
if (msg.hasMedia) {
|
||||
newMessage = await handlMedia(msg, ticket);
|
||||
} else {
|
||||
newMessage = await ticket.createMessage({
|
||||
id: msg.id.id,
|
||||
body: msg.body,
|
||||
fromMe: msg.fromMe,
|
||||
});
|
||||
await ticket.update({ lastMessage: msg.body });
|
||||
}
|
||||
|
||||
const serializedMessage = {
|
||||
...newMessage.dataValues,
|
||||
mediaUrl: `${
|
||||
newMessage.mediaUrl
|
||||
? `http://${process.env.HOST}:${process.env.PORT}/public/${newMessage.mediaUrl}`
|
||||
: ""
|
||||
}`,
|
||||
};
|
||||
|
||||
const serializaedTicket = {
|
||||
...ticket.dataValues,
|
||||
unreadMessages: 1,
|
||||
lastMessage: newMessage.body,
|
||||
contact: contact,
|
||||
};
|
||||
|
||||
io.to(ticket.id).to("notification").emit("appMessage", {
|
||||
action: "create",
|
||||
message: serializedMessage,
|
||||
ticket: serializaedTicket,
|
||||
contact: contact,
|
||||
});
|
||||
};
|
||||
|
||||
const wbotMessageListener = () => {
|
||||
const wbot = getWbot();
|
||||
const io = getIO();
|
||||
|
||||
wbot.on("message", async msg => {
|
||||
wbot.on("message_create", async msg => {
|
||||
// console.log(msg);
|
||||
|
||||
let newMessage;
|
||||
|
||||
if (msg.from === "status@broadcast" || msg.type === "location") {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const msgContact = await msg.getContact();
|
||||
const profilePicUrl = await msgContact.getProfilePicUrl();
|
||||
let msgContact;
|
||||
|
||||
const contact = await verifyContact(msgContact, profilePicUrl);
|
||||
|
||||
const ticket = await verifyTicket(contact);
|
||||
|
||||
// if (msg.hasQuotedMsg) {
|
||||
// const quotedMessage = await msg.getQuotedMessage();
|
||||
// console.log("quoted", quotedMessage);
|
||||
// }
|
||||
|
||||
if (msg.hasMedia) {
|
||||
newMessage = await handlMedia(msg, ticket);
|
||||
if (msg.fromMe) {
|
||||
msgContact = await wbot.getContactById(msg.to);
|
||||
} else {
|
||||
newMessage = await ticket.createMessage({
|
||||
id: msg.id.id,
|
||||
body: msg.body,
|
||||
});
|
||||
await ticket.update({ lastMessage: msg.body });
|
||||
msgContact = await msg.getContact();
|
||||
}
|
||||
|
||||
const serializedMessage = {
|
||||
...newMessage.dataValues,
|
||||
mediaUrl: `${
|
||||
newMessage.mediaUrl
|
||||
? `http://${process.env.HOST}:${process.env.PORT}/public/${newMessage.mediaUrl}`
|
||||
: ""
|
||||
}`,
|
||||
};
|
||||
const profilePicUrl = await msgContact.getProfilePicUrl();
|
||||
const contact = await verifyContact(msgContact, profilePicUrl);
|
||||
const ticket = await verifyTicket(contact);
|
||||
|
||||
const serializaedTicket = {
|
||||
...ticket.dataValues,
|
||||
unreadMessages: 1,
|
||||
lastMessage: newMessage.body,
|
||||
contact: contact,
|
||||
};
|
||||
|
||||
io.to(ticket.id).to("notification").emit("appMessage", {
|
||||
action: "create",
|
||||
message: serializedMessage,
|
||||
ticket: serializaedTicket,
|
||||
contact: contact,
|
||||
});
|
||||
|
||||
let chat = await msg.getChat();
|
||||
chat.sendSeen();
|
||||
await handleMessage(msg, ticket, contact);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
@@ -165,7 +163,7 @@ const wbotMessageListener = () => {
|
||||
where: { id: msg.id.id },
|
||||
});
|
||||
if (!messageToUpdate) {
|
||||
// will throw an error is msg wasn't sent from app
|
||||
// will throw an error in frist ack if msg wast sent from cellphone
|
||||
const error = new Error(
|
||||
"Erro ao alterar o ack da mensagem no banco de dados"
|
||||
);
|
||||
|
||||
@@ -105,7 +105,7 @@ const useStyles = makeStyles(theme => ({
|
||||
const MessageInput = ({ searchParam }) => {
|
||||
const classes = useStyles();
|
||||
const { ticketId } = useParams();
|
||||
const userId = localStorage.getItem("userId");
|
||||
// const userId = localStorage.getItem("userId");
|
||||
const username = localStorage.getItem("username");
|
||||
|
||||
const mediaInitialState = { preview: "", raw: "", name: "" };
|
||||
@@ -157,7 +157,7 @@ const MessageInput = ({ searchParam }) => {
|
||||
e.preventDefault();
|
||||
const formData = new FormData();
|
||||
formData.append("media", media.raw);
|
||||
formData.append("userId", userId);
|
||||
formData.append("fromMe", true);
|
||||
formData.append("body", media.name);
|
||||
|
||||
try {
|
||||
@@ -175,7 +175,7 @@ const MessageInput = ({ searchParam }) => {
|
||||
setLoading(true);
|
||||
const message = {
|
||||
read: 1,
|
||||
userId: userId,
|
||||
fromMe: true,
|
||||
mediaUrl: "",
|
||||
body: `${username}: ${inputMessage.trim()}`,
|
||||
};
|
||||
@@ -220,7 +220,7 @@ const MessageInput = ({ searchParam }) => {
|
||||
console.log(blob);
|
||||
formData.append("media", blob, filename);
|
||||
formData.append("body", filename);
|
||||
formData.append("userId", userId);
|
||||
formData.append("fromMe", true);
|
||||
try {
|
||||
await api.post(`/messages/${ticketId}`, formData);
|
||||
} catch (err) {
|
||||
|
||||
@@ -441,7 +441,7 @@ const MessagesList = () => {
|
||||
const renderMessages = () => {
|
||||
if (messagesList.length > 0) {
|
||||
const viewMessagesList = messagesList.map((message, index) => {
|
||||
if (!message.userId) {
|
||||
if (!message.fromMe) {
|
||||
return (
|
||||
<LinkifyWithTargetBlank key={message.id}>
|
||||
{renderDailyTimestamps(message, index)}
|
||||
|
||||
@@ -259,7 +259,7 @@ const TicketsList = () => {
|
||||
});
|
||||
|
||||
socket.on("appMessage", data => {
|
||||
if (data.action === "create") {
|
||||
if (data.action === "create" && !data.message.fromMe) {
|
||||
updateUnreadMessagesCount(data);
|
||||
if (
|
||||
(ticketId &&
|
||||
|
||||
Reference in New Issue
Block a user