mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 12:19:16 +00:00
feat: support displaing quoted messages
This commit is contained in:
@@ -28,7 +28,7 @@ const CreateMessageService = async ({
|
||||
await Message.upsert(messageData);
|
||||
|
||||
const message = await Message.findByPk(messageData.id, {
|
||||
include: ["contact"]
|
||||
include: ["contact", "quotedMsg"]
|
||||
});
|
||||
|
||||
if (!message) {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { where, fn, col } from "sequelize";
|
||||
import AppError from "../../errors/AppError";
|
||||
import Message from "../../models/Message";
|
||||
import Ticket from "../../models/Ticket";
|
||||
@@ -6,7 +5,6 @@ import ShowTicketService from "../TicketServices/ShowTicketService";
|
||||
|
||||
interface Request {
|
||||
ticketId: string;
|
||||
searchParam?: string;
|
||||
pageNumber?: string;
|
||||
}
|
||||
|
||||
@@ -18,7 +16,6 @@ interface Response {
|
||||
}
|
||||
|
||||
const ListMessagesService = async ({
|
||||
searchParam = "",
|
||||
pageNumber = "1",
|
||||
ticketId
|
||||
}: Request): Promise<Response> => {
|
||||
@@ -28,23 +25,14 @@ const ListMessagesService = async ({
|
||||
throw new AppError("ERR_NO_TICKET_FOUND", 404);
|
||||
}
|
||||
|
||||
const whereCondition = {
|
||||
body: where(
|
||||
fn("LOWER", col("body")),
|
||||
"LIKE",
|
||||
`%${searchParam.toLowerCase()}%`
|
||||
),
|
||||
ticketId
|
||||
};
|
||||
|
||||
// await setMessagesAsRead(ticket);
|
||||
const limit = 20;
|
||||
const offset = limit * (+pageNumber - 1);
|
||||
|
||||
const { count, rows: messages } = await Message.findAndCountAll({
|
||||
where: whereCondition,
|
||||
where: { ticketId },
|
||||
limit,
|
||||
include: ["contact"],
|
||||
include: ["contact", "quotedMsg"],
|
||||
offset,
|
||||
order: [["createdAt", "DESC"]]
|
||||
});
|
||||
|
||||
@@ -149,12 +149,20 @@ const handlMedia = async (
|
||||
ticket: Ticket,
|
||||
contact: Contact
|
||||
): Promise<Message> => {
|
||||
let quotedMsg: Message | null = null;
|
||||
|
||||
const media = await msg.downloadMedia();
|
||||
|
||||
if (!media) {
|
||||
throw new AppError("ERR_WAPP_DOWNLOAD_MEDIA");
|
||||
}
|
||||
|
||||
if (msg.hasQuotedMsg) {
|
||||
const wbotQuotedMsg = await msg.getQuotedMessage();
|
||||
|
||||
quotedMsg = await Message.findByPk(wbotQuotedMsg.id.id);
|
||||
}
|
||||
|
||||
if (!media.filename) {
|
||||
const ext = media.mimetype.split("/")[1].split(";")[0];
|
||||
media.filename = `${new Date().getTime()}.${ext}`;
|
||||
@@ -178,7 +186,8 @@ const handlMedia = async (
|
||||
fromMe: msg.fromMe,
|
||||
read: msg.fromMe,
|
||||
mediaUrl: media.filename,
|
||||
mediaType: media.mimetype.split("/")[0]
|
||||
mediaType: media.mimetype.split("/")[0],
|
||||
quotedMsgId: quotedMsg?.id
|
||||
};
|
||||
|
||||
const newMessage = await CreateMessageService({ messageData });
|
||||
@@ -193,6 +202,13 @@ const handleMessage = async (
|
||||
contact: Contact
|
||||
) => {
|
||||
let newMessage: Message | null;
|
||||
let quotedMsg: Message | null = null;
|
||||
|
||||
if (msg.hasQuotedMsg) {
|
||||
const wbotQuotedMsg = await msg.getQuotedMessage();
|
||||
|
||||
quotedMsg = await Message.findByPk(wbotQuotedMsg.id.id);
|
||||
}
|
||||
|
||||
if (msg.hasMedia) {
|
||||
newMessage = await handlMedia(msg, ticket, contact);
|
||||
@@ -204,7 +220,8 @@ const handleMessage = async (
|
||||
body: msg.body,
|
||||
fromMe: msg.fromMe,
|
||||
mediaType: msg.type,
|
||||
read: msg.fromMe
|
||||
read: msg.fromMe,
|
||||
quotedMsgId: quotedMsg?.id
|
||||
};
|
||||
|
||||
newMessage = await CreateMessageService({ messageData });
|
||||
|
||||
Reference in New Issue
Block a user