mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 20:59:16 +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 = {
|
// const serialziedMessage = {
|
||||||
...newMessage.dataValues,
|
// ...newMessage.dataValues,
|
||||||
mediaUrl: `${
|
// mediaUrl: `${
|
||||||
message.mediaUrl
|
// message.mediaUrl
|
||||||
? `http://${process.env.HOST}:${process.env.PORT}/public/${message.mediaUrl}`
|
// ? `http://${process.env.HOST}:${process.env.PORT}/public/${message.mediaUrl}`
|
||||||
: ""
|
// : ""
|
||||||
}`,
|
// }`,
|
||||||
};
|
// };
|
||||||
|
|
||||||
io.to(ticketId).emit("appMessage", {
|
// io.to(ticketId).emit("appMessage", {
|
||||||
action: "create",
|
// action: "create",
|
||||||
message: serialziedMessage,
|
// message: serialziedMessage,
|
||||||
});
|
// });
|
||||||
|
|
||||||
await setMessagesAsRead(ticketId);
|
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 },
|
ack: { type: Sequelize.INTEGER, defaultValue: 0 },
|
||||||
read: { type: Sequelize.BOOLEAN, defaultValue: false },
|
read: { type: Sequelize.BOOLEAN, defaultValue: false },
|
||||||
|
fromMe: { type: Sequelize.BOOLEAN, defaultValue: false },
|
||||||
body: { type: Sequelize.TEXT },
|
body: { type: Sequelize.TEXT },
|
||||||
mediaUrl: { type: Sequelize.STRING },
|
mediaUrl: { type: Sequelize.STRING },
|
||||||
mediaType: { type: Sequelize.STRING },
|
mediaType: { type: Sequelize.STRING },
|
||||||
@@ -28,7 +29,6 @@ class Message extends Sequelize.Model {
|
|||||||
|
|
||||||
static associate(models) {
|
static associate(models) {
|
||||||
this.belongsTo(models.Ticket, { foreignKey: "ticketId" });
|
this.belongsTo(models.Ticket, { foreignKey: "ticketId" });
|
||||||
this.belongsTo(models.User, { foreignKey: "userId" });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const { Op } = require("sequelize");
|
const { Op } = require("sequelize");
|
||||||
const { parseISO, subHours } = require("date-fns");
|
const { subHours } = require("date-fns");
|
||||||
|
|
||||||
const Contact = require("../models/Contact");
|
const Contact = require("../models/Contact");
|
||||||
const Ticket = require("../models/Ticket");
|
const Ticket = require("../models/Ticket");
|
||||||
@@ -93,67 +93,65 @@ const handlMedia = async (msg, ticket) => {
|
|||||||
return newMessage;
|
return newMessage;
|
||||||
};
|
};
|
||||||
|
|
||||||
const wbotMessageListener = () => {
|
const handleMessage = async (msg, ticket, contact) => {
|
||||||
const io = getIO();
|
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 wbot = getWbot();
|
||||||
|
const io = getIO();
|
||||||
|
|
||||||
wbot.on("message", async msg => {
|
wbot.on("message_create", async msg => {
|
||||||
// console.log(msg);
|
// console.log(msg);
|
||||||
|
|
||||||
let newMessage;
|
|
||||||
|
|
||||||
if (msg.from === "status@broadcast" || msg.type === "location") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const msgContact = await msg.getContact();
|
let msgContact;
|
||||||
const profilePicUrl = await msgContact.getProfilePicUrl();
|
|
||||||
|
|
||||||
const contact = await verifyContact(msgContact, profilePicUrl);
|
if (msg.fromMe) {
|
||||||
|
msgContact = await wbot.getContactById(msg.to);
|
||||||
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);
|
|
||||||
} else {
|
} else {
|
||||||
newMessage = await ticket.createMessage({
|
msgContact = await msg.getContact();
|
||||||
id: msg.id.id,
|
|
||||||
body: msg.body,
|
|
||||||
});
|
|
||||||
await ticket.update({ lastMessage: msg.body });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const serializedMessage = {
|
const profilePicUrl = await msgContact.getProfilePicUrl();
|
||||||
...newMessage.dataValues,
|
const contact = await verifyContact(msgContact, profilePicUrl);
|
||||||
mediaUrl: `${
|
const ticket = await verifyTicket(contact);
|
||||||
newMessage.mediaUrl
|
|
||||||
? `http://${process.env.HOST}:${process.env.PORT}/public/${newMessage.mediaUrl}`
|
|
||||||
: ""
|
|
||||||
}`,
|
|
||||||
};
|
|
||||||
|
|
||||||
const serializaedTicket = {
|
await handleMessage(msg, ticket, contact);
|
||||||
...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();
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
@@ -165,7 +163,7 @@ const wbotMessageListener = () => {
|
|||||||
where: { id: msg.id.id },
|
where: { id: msg.id.id },
|
||||||
});
|
});
|
||||||
if (!messageToUpdate) {
|
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(
|
const error = new Error(
|
||||||
"Erro ao alterar o ack da mensagem no banco de dados"
|
"Erro ao alterar o ack da mensagem no banco de dados"
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ const useStyles = makeStyles(theme => ({
|
|||||||
const MessageInput = ({ searchParam }) => {
|
const MessageInput = ({ searchParam }) => {
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
const { ticketId } = useParams();
|
const { ticketId } = useParams();
|
||||||
const userId = localStorage.getItem("userId");
|
// const userId = localStorage.getItem("userId");
|
||||||
const username = localStorage.getItem("username");
|
const username = localStorage.getItem("username");
|
||||||
|
|
||||||
const mediaInitialState = { preview: "", raw: "", name: "" };
|
const mediaInitialState = { preview: "", raw: "", name: "" };
|
||||||
@@ -157,7 +157,7 @@ const MessageInput = ({ searchParam }) => {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append("media", media.raw);
|
formData.append("media", media.raw);
|
||||||
formData.append("userId", userId);
|
formData.append("fromMe", true);
|
||||||
formData.append("body", media.name);
|
formData.append("body", media.name);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -175,7 +175,7 @@ const MessageInput = ({ searchParam }) => {
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
const message = {
|
const message = {
|
||||||
read: 1,
|
read: 1,
|
||||||
userId: userId,
|
fromMe: true,
|
||||||
mediaUrl: "",
|
mediaUrl: "",
|
||||||
body: `${username}: ${inputMessage.trim()}`,
|
body: `${username}: ${inputMessage.trim()}`,
|
||||||
};
|
};
|
||||||
@@ -220,7 +220,7 @@ const MessageInput = ({ searchParam }) => {
|
|||||||
console.log(blob);
|
console.log(blob);
|
||||||
formData.append("media", blob, filename);
|
formData.append("media", blob, filename);
|
||||||
formData.append("body", filename);
|
formData.append("body", filename);
|
||||||
formData.append("userId", userId);
|
formData.append("fromMe", true);
|
||||||
try {
|
try {
|
||||||
await api.post(`/messages/${ticketId}`, formData);
|
await api.post(`/messages/${ticketId}`, formData);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -441,7 +441,7 @@ const MessagesList = () => {
|
|||||||
const renderMessages = () => {
|
const renderMessages = () => {
|
||||||
if (messagesList.length > 0) {
|
if (messagesList.length > 0) {
|
||||||
const viewMessagesList = messagesList.map((message, index) => {
|
const viewMessagesList = messagesList.map((message, index) => {
|
||||||
if (!message.userId) {
|
if (!message.fromMe) {
|
||||||
return (
|
return (
|
||||||
<LinkifyWithTargetBlank key={message.id}>
|
<LinkifyWithTargetBlank key={message.id}>
|
||||||
{renderDailyTimestamps(message, index)}
|
{renderDailyTimestamps(message, index)}
|
||||||
|
|||||||
@@ -259,7 +259,7 @@ const TicketsList = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on("appMessage", data => {
|
socket.on("appMessage", data => {
|
||||||
if (data.action === "create") {
|
if (data.action === "create" && !data.message.fromMe) {
|
||||||
updateUnreadMessagesCount(data);
|
updateUnreadMessagesCount(data);
|
||||||
if (
|
if (
|
||||||
(ticketId &&
|
(ticketId &&
|
||||||
|
|||||||
Reference in New Issue
Block a user