diff --git a/backend/src/database/migrations/20201004955719-remove-vcardContactId-column-to-messages.ts b/backend/src/database/migrations/20201004955719-remove-vcardContactId-column-to-messages.ts new file mode 100644 index 0000000..dac0046 --- /dev/null +++ b/backend/src/database/migrations/20201004955719-remove-vcardContactId-column-to-messages.ts @@ -0,0 +1,16 @@ +import { QueryInterface, DataTypes } from "sequelize"; + +module.exports = { + up: (queryInterface: QueryInterface) => { + return queryInterface.removeColumn("Messages", "vcardContactId"); + }, + + down: (queryInterface: QueryInterface) => { + return queryInterface.addColumn("Messages", "vcardContactId", { + type: DataTypes.INTEGER, + references: { model: "Contacts", key: "id" }, + onUpdate: "CASCADE", + onDelete: "CASCADE" + }); + } +}; diff --git a/backend/src/helpers/ParseVcardToJson.ts b/backend/src/helpers/ParseVcardToJson.ts deleted file mode 100644 index 4045334..0000000 --- a/backend/src/helpers/ParseVcardToJson.ts +++ /dev/null @@ -1,59 +0,0 @@ -interface JCard { - [key: string]: any; -} - -interface Meta { - [key: string]: string; -} - -const ParseVcardToJson = (input: string): JCard => { - const Re1 = /^(version|fn|title|org):(.+)$/i; - const Re2 = /^([^:;]+);([^:]+):(.+)$/; - const ReKey = /item\d{1,2}\./; - const fields = {} as JCard; - - input.split(/\r\n|\r|\n/).forEach(line => { - let results; - let key; - - if (Re1.test(line)) { - results = line.match(Re1); - if (results) { - key = results[1].toLowerCase(); - const [, , res] = results; - fields[key] = res; - } - } else if (Re2.test(line)) { - results = line.match(Re2); - if (results) { - key = results[1].replace(ReKey, "").toLowerCase(); - - const meta = {} as Meta; - results[2] - .split(";") - .map((p, i) => { - const match = p.match(/([a-z]+)=(.*)/i); - if (match) { - return [match[1], match[2]]; - } - return [`TYPE${i === 0 ? "" : i}`, p]; - }) - .forEach(p => { - const [, m] = p; - meta[p[0]] = m; - }); - - if (!fields[key]) fields[key] = []; - - fields[key].push({ - meta, - value: results[3].split(";") - }); - } - } - }); - - return fields; -}; - -export default ParseVcardToJson; diff --git a/backend/src/models/Message.ts b/backend/src/models/Message.ts index abcd04f..1ced76f 100644 --- a/backend/src/models/Message.ts +++ b/backend/src/models/Message.ts @@ -72,13 +72,6 @@ class Message extends Model { @BelongsTo(() => Contact, "contactId") contact: Contact; - - @ForeignKey(() => Contact) - @Column - vcardContactId: number; - - @BelongsTo(() => Contact, "vcardContactId") - vcardContact: Contact; } export default Message; diff --git a/backend/src/services/MessageServices/CreateMessageService.ts b/backend/src/services/MessageServices/CreateMessageService.ts index 7ba9c1c..e5abb19 100644 --- a/backend/src/services/MessageServices/CreateMessageService.ts +++ b/backend/src/services/MessageServices/CreateMessageService.ts @@ -7,7 +7,6 @@ interface MessageData { ticketId: number; body: string; contactId?: number; - vcardContactId?: number; fromMe?: boolean; read?: boolean; mediaType?: string; @@ -29,7 +28,7 @@ const CreateMessageService = async ({ await Message.upsert(messageData); const message = await Message.findByPk(messageData.id, { - include: ["contact", "vcardContact"] + include: ["contact"] }); if (!message) { diff --git a/backend/src/services/MessageServices/ListMessagesService.ts b/backend/src/services/MessageServices/ListMessagesService.ts index 362cc5a..75d4d8d 100644 --- a/backend/src/services/MessageServices/ListMessagesService.ts +++ b/backend/src/services/MessageServices/ListMessagesService.ts @@ -44,7 +44,7 @@ const ListMessagesService = async ({ const { count, rows: messages } = await Message.findAndCountAll({ where: whereCondition, limit, - include: ["contact", "vcardContact"], + include: ["contact"], offset, order: [["createdAt", "DESC"]] }); diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index f2db32c..4dd28fb 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -19,7 +19,6 @@ import { getIO } from "../../libs/socket"; import { getWbot } from "../../libs/wbot"; import AppError from "../../errors/AppError"; import ShowTicketService from "../TicketServices/ShowTicketService"; -import ParseVcardToJson from "../../helpers/ParseVcardToJson"; import CreateMessageService from "../MessageServices/CreateMessageService"; const writeFileAsync = promisify(writeFile); @@ -181,8 +180,7 @@ const handlMedia = async ( const handleMessage = async ( msg: WbotMessage, ticket: Ticket, - contact: Contact, - vcardContact?: Contact + contact: Contact ) => { let newMessage: Message | null; @@ -193,7 +191,6 @@ const handleMessage = async ( id: msg.id.id, ticketId: ticket.id, contactId: msg.fromMe ? undefined : contact.id, - vcardContactId: vcardContact ? vcardContact.id : undefined, body: msg.body, fromMe: msg.fromMe, mediaType: msg.type, @@ -246,7 +243,6 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => { try { let msgContact: WbotContact; let groupContact: Contact | undefined; - let vcardContact: Contact | undefined; if (msg.fromMe) { msgContact = await wbot.getContactById(msg.to); @@ -259,27 +255,25 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => { msgContact = await msg.getContact(); } - // if message has an author, its a gruop message. + const chat = await msg.getChat(); + + if (chat.isGroup) { + let msgGroupContact; + + if (msg.fromMe) { + msgGroupContact = await wbot.getContactById(msg.to); + } else { + msgGroupContact = await wbot.getContactById(msg.from); + } - if (msg.author) { - const msgGroupContact = await wbot.getContactById(msg.from); groupContact = await verifyGroup(msgGroupContact); } - if (msg.type === "vcard") { - const { tel } = ParseVcardToJson(msg.body); - const vcardWaid = tel[0]?.meta?.waid; - const vcardMsgContact = await wbot.getContactById(`${vcardWaid}@c.us`); - const profilePicUrl = await vcardMsgContact.getProfilePicUrl(); - - vcardContact = await verifyContact(vcardMsgContact, profilePicUrl); - } - const profilePicUrl = await msgContact.getProfilePicUrl(); const contact = await verifyContact(msgContact, profilePicUrl); const ticket = await verifyTicket(contact, whatsappId, groupContact); - await handleMessage(msg, ticket, contact, vcardContact); + await handleMessage(msg, ticket, contact); } catch (err) { Sentry.captureException(err); console.log(err); @@ -311,7 +305,7 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => { try { const messageToUpdate = await Message.findByPk(msg.id.id, { - include: ["contact", "vcardContact"] + include: ["contact"] }); if (!messageToUpdate) { return; diff --git a/frontend/src/components/Ticket/index.js b/frontend/src/components/Ticket/index.js index 1396f14..6b88b5a 100644 --- a/frontend/src/components/Ticket/index.js +++ b/frontend/src/components/Ticket/index.js @@ -14,13 +14,7 @@ import DoneIcon from "@material-ui/icons/Done"; import DoneAllIcon from "@material-ui/icons/DoneAll"; import Paper from "@material-ui/core/Paper"; -import { - Avatar, - Card, - CardActions, - CardHeader, - IconButton, -} from "@material-ui/core"; +import { IconButton } from "@material-ui/core"; import { Block, ExpandMore } from "@material-ui/icons"; import api from "../../services/api"; @@ -449,27 +443,6 @@ const Ticket = () => { controls /> ); - } - if (message.mediaType === "vcard") { - return ( - - - } - title={message.vcardContact?.name} - subheader={message.vcardContact?.number} - /> - - {/* */} - - - ); } else { return ( @@ -559,15 +532,14 @@ const Ticket = () => { {renderDailyTimestamps(message, index)} {renderMessageDivider(message, index)}
- {(message.mediaUrl || message.mediaType === "vcard") && - checkMessageMedia(message)} + {message.mediaUrl && checkMessageMedia(message)} {ticket.isGroup && ( {message.contact?.name} )}
- {message.mediaType !== "vcard" && message.body} + {message.body} {format(parseISO(message.createdAt), "HH:mm")}