mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 04:09:26 +00:00
added new "last message" logic, before useReducer
This commit is contained in:
@@ -12,6 +12,7 @@ const wbotMonitor = require("./controllers/wbotMonitor");
|
||||
const messageRoutes = require("./routes/message");
|
||||
const ContactRoutes = require("./routes/contacts");
|
||||
const AuthRoutes = require("./routes/auth");
|
||||
const WhatsRoutes = require("./routes/whatsapp");
|
||||
|
||||
const app = express();
|
||||
|
||||
@@ -31,6 +32,7 @@ app.use("/public", express.static(path.join(__dirname, "public")));
|
||||
|
||||
app.use(messageRoutes);
|
||||
app.use(ContactRoutes);
|
||||
app.use(WhatsRoutes);
|
||||
app.use("/auth", AuthRoutes);
|
||||
|
||||
app.use((error, req, res, next) => {
|
||||
|
||||
@@ -7,7 +7,7 @@ exports.getContacts = async (req, res) => {
|
||||
const contacts = await Contact.findAll({
|
||||
include: {
|
||||
model: Message,
|
||||
attributes: ["messageBody", "createdAt"],
|
||||
attributes: [],
|
||||
},
|
||||
attributes: {
|
||||
include: [
|
||||
|
||||
@@ -13,80 +13,91 @@ const wbotMessageListener = () => {
|
||||
|
||||
wbot.on("message", async msg => {
|
||||
let newMessage;
|
||||
console.log(msg);
|
||||
// console.log(msg);
|
||||
if (msg.from === "status@broadcast") {
|
||||
return;
|
||||
}
|
||||
const msgContact = await msg.getContact();
|
||||
const imageUrl = await msgContact.getProfilePicUrl();
|
||||
try {
|
||||
let contact = await Contact.findOne({
|
||||
where: { number: msgContact.number },
|
||||
});
|
||||
const msgContact = await msg.getContact();
|
||||
const imageUrl = await msgContact.getProfilePicUrl();
|
||||
try {
|
||||
let contact = await Contact.findOne({
|
||||
where: { number: msgContact.number },
|
||||
});
|
||||
|
||||
if (contact) {
|
||||
await contact.update({ imageURL: imageUrl });
|
||||
}
|
||||
|
||||
if (!contact) {
|
||||
try {
|
||||
contact = await Contact.create({
|
||||
name: msgContact.pushname || msgContact.number.toString(),
|
||||
number: msgContact.number,
|
||||
imageURL: imageUrl,
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
if (contact) {
|
||||
await contact.update({ imageURL: imageUrl });
|
||||
}
|
||||
}
|
||||
if (msg.hasMedia) {
|
||||
const media = await msg.downloadMedia();
|
||||
|
||||
if (media) {
|
||||
if (!media.filename) {
|
||||
let ext = media.mimetype.split("/")[1].split(";")[0];
|
||||
media.filename = `${new Date().getTime()}.${ext}`;
|
||||
if (!contact) {
|
||||
try {
|
||||
contact = await Contact.create({
|
||||
name: msgContact.pushname || msgContact.number.toString(),
|
||||
number: msgContact.number,
|
||||
imageURL: imageUrl,
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
if (msg.hasMedia) {
|
||||
const media = await msg.downloadMedia();
|
||||
|
||||
fs.writeFile(
|
||||
path.join(__dirname, "..", "public", media.filename),
|
||||
media.data,
|
||||
"base64",
|
||||
err => {
|
||||
console.log(err);
|
||||
if (media) {
|
||||
if (!media.filename) {
|
||||
let ext = media.mimetype.split("/")[1].split(";")[0];
|
||||
media.filename = `${new Date().getTime()}.${ext}`;
|
||||
}
|
||||
);
|
||||
|
||||
fs.writeFile(
|
||||
path.join(__dirname, "..", "public", media.filename),
|
||||
media.data,
|
||||
"base64",
|
||||
err => {
|
||||
console.log(err);
|
||||
}
|
||||
);
|
||||
|
||||
newMessage = await contact.createMessage({
|
||||
id: msg.id.id,
|
||||
messageBody: msg.body || media.filename,
|
||||
mediaUrl: media.filename,
|
||||
mediaType: media.mimetype.split("/")[0],
|
||||
});
|
||||
await contact.update({ lastMessage: msg.body || media.filename });
|
||||
}
|
||||
} else {
|
||||
newMessage = await contact.createMessage({
|
||||
id: msg.id.id,
|
||||
messageBody: msg.body || media.filename,
|
||||
mediaUrl: media.filename,
|
||||
mediaType: media.mimetype.split("/")[0],
|
||||
messageBody: msg.body,
|
||||
});
|
||||
await contact.update({ lastMessage: msg.body });
|
||||
}
|
||||
} else {
|
||||
newMessage = await contact.createMessage({
|
||||
id: msg.id.id,
|
||||
messageBody: msg.body,
|
||||
});
|
||||
|
||||
io.to(contact.id)
|
||||
.to("notification")
|
||||
.emit("appMessage", {
|
||||
action: "create",
|
||||
message: {
|
||||
...newMessage.dataValues,
|
||||
mediaUrl: `${
|
||||
newMessage.mediaUrl
|
||||
? `http://localhost:8080/public/${newMessage.mediaUrl}`
|
||||
: ""
|
||||
}`,
|
||||
},
|
||||
contact: {
|
||||
...contact.dataValues,
|
||||
unreadMessages: 1,
|
||||
lastMessage: newMessage.messageBody,
|
||||
},
|
||||
});
|
||||
|
||||
let chat = await msg.getChat();
|
||||
chat.sendSeen();
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
io.to(contact.id)
|
||||
.to("notification")
|
||||
.emit("appMessage", {
|
||||
action: "create",
|
||||
message: {
|
||||
...newMessage.dataValues,
|
||||
mediaUrl: `${
|
||||
newMessage.mediaUrl
|
||||
? `http://localhost:8080/public/${newMessage.mediaUrl}`
|
||||
: ""
|
||||
}`,
|
||||
},
|
||||
});
|
||||
|
||||
let chat = await msg.getChat();
|
||||
chat.sendSeen();
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ const wbotMonitor = () => {
|
||||
const wbot = getWbot();
|
||||
|
||||
wbot.on("change_state", newState => {
|
||||
console.log(newState);
|
||||
console.log("monitor", newState);
|
||||
});
|
||||
|
||||
wbot.on("change_battery", batteryInfo => {
|
||||
@@ -27,7 +27,10 @@ const wbotMonitor = () => {
|
||||
wbot.destroy();
|
||||
setTimeout(() =>
|
||||
init()
|
||||
.then(res => wbotMessageListener(), 2000)
|
||||
.then(res => {
|
||||
wbotMessageListener();
|
||||
wbotMonitor();
|
||||
})
|
||||
.catch(err => console.log(err))
|
||||
);
|
||||
});
|
||||
|
||||
15
backend/controllers/whatsapp.js
Normal file
15
backend/controllers/whatsapp.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const Whatsapp = require("../models/whatsapp");
|
||||
|
||||
exports.getSession = async (req, res, next) => {
|
||||
try {
|
||||
const dbSession = await Whatsapp.findOne({ where: { id: 1 } });
|
||||
|
||||
if (!dbSession) {
|
||||
return res.status(200).json({ message: "Session not found" });
|
||||
}
|
||||
|
||||
return res.status(200).json(dbSession);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
};
|
||||
@@ -8,7 +8,7 @@ const Contact = sequelize.define("contact", {
|
||||
name: { type: Sequelize.STRING(100), allowNull: false },
|
||||
number: { type: Sequelize.STRING(15), allowNull: false },
|
||||
imageURL: { type: Sequelize.STRING(200) },
|
||||
ateraId: Sequelize.INTEGER(),
|
||||
lastMessage: { type: Sequelize.TEXT },
|
||||
});
|
||||
|
||||
Contact.hasMany(Message, {
|
||||
|
||||
@@ -13,7 +13,7 @@ const Message = sequelize.define("message", {
|
||||
},
|
||||
userId: { type: Sequelize.INTEGER, defaultValue: 0 },
|
||||
ack: { type: Sequelize.INTEGER, defaultValue: 0 },
|
||||
messageBody: { type: Sequelize.STRING(250), allowNull: false },
|
||||
messageBody: { type: Sequelize.TEXT, allowNull: false },
|
||||
read: { type: Sequelize.BOOLEAN, defaultValue: false },
|
||||
mediaUrl: { type: Sequelize.STRING(250) },
|
||||
mediaType: { type: Sequelize.STRING(250) },
|
||||
|
||||
15
backend/routes/whatsapp.js
Normal file
15
backend/routes/whatsapp.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const express = require("express");
|
||||
const isAuth = require("../middleware/is-auth");
|
||||
|
||||
const WhatsappController = require("../controllers/whatsapp");
|
||||
|
||||
const routes = express.Router();
|
||||
|
||||
routes.get("/whatsapp/session", isAuth, WhatsappController.getSession);
|
||||
// routes.post(
|
||||
// "/messages/:contactId",
|
||||
// isAuth,
|
||||
// WhatsappController.postCreateContactMessage
|
||||
// );
|
||||
|
||||
module.exports = routes;
|
||||
Reference in New Issue
Block a user